You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "jayzhan211 (via GitHub)" <gi...@apache.org> on 2024/01/30 12:53:57 UTC
[PR] Unnest with single expression [arrow-datafusion]
jayzhan211 opened a new pull request, #9069:
URL: https://github.com/apache/arrow-datafusion/pull/9069
## Which issue does this PR close?
Revisit from #6796, only single expression included in this PR.
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Closes #.
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
Ref #6555
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
Two types of unnest expressions
1. `select unnest([1,2,3])`
2. `select unnest(column)`
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
3. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Unnest with single expression [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #9069:
URL: https://github.com/apache/arrow-datafusion/pull/9069#discussion_r1473523399
##########
datafusion/sqllogictest/test_files/unnest.slt:
##########
@@ -0,0 +1,59 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+############################
+# Unnest Expressions Tests #
+############################
+
+statement ok
+CREATE TABLE unnest_table
+AS VALUES
+ ([1,2,3]),
+ ([4,5]),
+ ([6])
+;
+
+query I
+select unnest([1,2,3]);
+----
+1
+2
+3
+
+query ?
+select unnest([]);
+----
+
+query ?
+select column1 from unnest_table;
+----
+[1, 2, 3]
+[4, 5]
+[6]
+
+query I
+select unnest(column1) from unnest_table;
+----
+1
+2
+3
+4
+5
+6
+
+statement ok
+drop table unnest_table;
Review Comment:
Awesome -- can you please also add tests for:
1. to unnest two columns (it is fine to error, but I think that behavior should be tested)
2. trying to unnest a scalar value `1` rather than `[1]`
3. to unnest zero columns (again should be an error)
##########
datafusion/sql/src/select.rs:
##########
@@ -275,6 +274,39 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
Ok(plan)
}
+ // Try converting Expr::Unnest to LogicalPlan::Unnest if possible, otherwise do the final projection
+ fn try_process_unnest(
+ &self,
+ input: LogicalPlan,
+ select_exprs: Vec<Expr>,
+ ) -> Result<LogicalPlan> {
+ let mut array_exprs_to_unnest = vec![];
+
+ for expr in select_exprs.iter() {
+ if let Expr::Unnest(Unnest { exprs: array_expr }) = expr {
+ array_exprs_to_unnest.push(array_expr[0].clone());
+ }
+ }
+
+ // Do the final projection
+ if array_exprs_to_unnest.is_empty() {
+ LogicalPlanBuilder::from(input)
+ .project(select_exprs)?
+ .build()
+ } else {
+ // Only support single unnest expression for now
+ assert_eq!(array_exprs_to_unnest.len(), 1);
Review Comment:
Could we please make this return `NotYetImplemented` rather than panic?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Unnest with single expression [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #9069:
URL: https://github.com/apache/arrow-datafusion/pull/9069#discussion_r1475329818
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -82,6 +82,13 @@ impl ExprSchemable for Expr {
Expr::Case(case) => case.when_then_expr[0].1.get_type(schema),
Expr::Cast(Cast { data_type, .. })
| Expr::TryCast(TryCast { data_type, .. }) => Ok(data_type.clone()),
+ Expr::Unnest(Unnest { exprs }) => {
+ let arg_data_types = exprs
Review Comment:
I think we can keep this, since there will be multiple exprs in the future
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Unnest with single expression [arrow-datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #9069:
URL: https://github.com/apache/arrow-datafusion/pull/9069#discussion_r1476521715
##########
datafusion/sql/src/expr/function.rs:
##########
@@ -70,6 +70,50 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
return Ok(Expr::ScalarFunction(ScalarFunction::new_udf(fm, args)));
}
+ // Build Unnest expression
+ if name.eq("unnest") {
+ let exprs =
+ self.function_args_to_expr(args.clone(), schema, planner_context)?;
+
+ match exprs.len() {
+ 0 => {
+ return exec_err!("unnest() requires at least one argument");
+ }
+ 1 => {
+ if let Expr::ScalarFunction(ScalarFunction {
+ func_def:
+ ScalarFunctionDefinition::BuiltIn(
+ BuiltinScalarFunction::MakeArray,
+ ),
+ ..
+ }) = exprs[0]
+ {
+ // valid
+ } else if let Expr::Column(_) = exprs[0] {
+ // valid
+ } else if let Expr::ScalarFunction(ScalarFunction {
+ func_def:
+ ScalarFunctionDefinition::BuiltIn(BuiltinScalarFunction::Struct),
+ ..
+ }) = exprs[0]
+ {
+ return not_impl_err!("unnest() does not support struct yet");
+ } else {
+ return plan_err!(
+ "unnest() can only be applied to array and structs and null"
Review Comment:
thats the confusing part. `not_impl_err!("unnest() does not support struct yet");` - here the user can get the struct is not supported by unnest.
```plan_err!("unnest() can only be applied to array and structs and null") ``` here the message says that unnest support ONLY array, structs and nulls which confronts with first err message
Maybe I'm missing something?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Unnest with single expression [arrow-datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #9069:
URL: https://github.com/apache/arrow-datafusion/pull/9069#discussion_r1474725958
##########
datafusion/sqllogictest/test_files/unnest.slt:
##########
@@ -0,0 +1,72 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+############################
+# Unnest Expressions Tests #
+############################
+
+statement ok
+CREATE TABLE unnest_table
+AS VALUES
+ ([1,2,3], [7], 1),
+ ([4,5], [8,9,10], 2),
+ ([6], [11,12], 3)
+;
+
+query I
+select unnest([1,2,3]);
+----
+1
+2
+3
+
+query ?
+select unnest([]);
+----
+
+query ?
+select column1 from unnest_table;
Review Comment:
do we need this test?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Unnest with single expression [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #9069:
URL: https://github.com/apache/arrow-datafusion/pull/9069#discussion_r1475328551
##########
datafusion/sql/src/expr/function.rs:
##########
@@ -70,6 +70,50 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
return Ok(Expr::ScalarFunction(ScalarFunction::new_udf(fm, args)));
}
+ // Build Unnest expression
+ if name.eq("unnest") {
+ let exprs =
+ self.function_args_to_expr(args.clone(), schema, planner_context)?;
+
+ match exprs.len() {
+ 0 => {
+ return exec_err!("unnest() requires at least one argument");
+ }
+ 1 => {
+ if let Expr::ScalarFunction(ScalarFunction {
+ func_def:
+ ScalarFunctionDefinition::BuiltIn(
+ BuiltinScalarFunction::MakeArray,
+ ),
+ ..
+ }) = exprs[0]
+ {
+ // valid
+ } else if let Expr::Column(_) = exprs[0] {
+ // valid
+ } else if let Expr::ScalarFunction(ScalarFunction {
+ func_def:
+ ScalarFunctionDefinition::BuiltIn(BuiltinScalarFunction::Struct),
+ ..
+ }) = exprs[0]
+ {
+ return not_impl_err!("unnest() does not support struct yet");
+ } else {
+ return plan_err!(
+ "unnest() can only be applied to array and structs and null"
Review Comment:
first line is struct nyi error.
second line is to tell you that only those 3 are valid.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Unnest with single expression [arrow-datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #9069:
URL: https://github.com/apache/arrow-datafusion/pull/9069#discussion_r1474726470
##########
datafusion/sqllogictest/test_files/unnest.slt:
##########
@@ -0,0 +1,72 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+############################
+# Unnest Expressions Tests #
+############################
+
+statement ok
+CREATE TABLE unnest_table
+AS VALUES
+ ([1,2,3], [7], 1),
Review Comment:
please include nulls, empty arrays into the table
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Unnest with single expression [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #9069:
URL: https://github.com/apache/arrow-datafusion/pull/9069#issuecomment-1925749051
Thanks again @jayzhan211 -- this is really nice 👌
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Unnest with single expression [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #9069:
URL: https://github.com/apache/arrow-datafusion/pull/9069#discussion_r1473682907
##########
datafusion/sql/src/expr/function.rs:
##########
@@ -70,6 +70,50 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
return Ok(Expr::ScalarFunction(ScalarFunction::new_udf(fm, args)));
}
+ // Build Unnest expression
+ if name.eq("unnest") {
+ let exprs =
+ self.function_args_to_expr(args.clone(), schema, planner_context)?;
+
+ match exprs.len() {
+ 0 => {
+ return exec_err!("unnest() requires at least one argument");
+ }
+ 1 => {
+ if let Expr::ScalarFunction(ScalarFunction {
+ func_def:
+ ScalarFunctionDefinition::BuiltIn(
+ BuiltinScalarFunction::MakeArray,
+ ),
+ ..
+ }) = exprs[0]
+ {
+ // valid
+ } else if let Expr::Column(_) = exprs[0] {
Review Comment:
arguments handling in Column seems not possible to handle anywhere before transforming to LogicalPlan. We should validate arguments inside UnnestExec or elsewhere downstream.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Unnest with single expression [arrow-datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #9069:
URL: https://github.com/apache/arrow-datafusion/pull/9069#discussion_r1474719227
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -82,6 +82,13 @@ impl ExprSchemable for Expr {
Expr::Case(case) => case.when_then_expr[0].1.get_type(schema),
Expr::Cast(Cast { data_type, .. })
| Expr::TryCast(TryCast { data_type, .. }) => Ok(data_type.clone()),
+ Expr::Unnest(Unnest { exprs }) => {
+ let arg_data_types = exprs
Review Comment:
do we need iter here as we refer for the first element only?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Unnest with single expression [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #9069:
URL: https://github.com/apache/arrow-datafusion/pull/9069
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Unnest with single expression [arrow-datafusion]
Posted by "jayzhan211 (via GitHub)" <gi...@apache.org>.
jayzhan211 commented on code in PR #9069:
URL: https://github.com/apache/arrow-datafusion/pull/9069#discussion_r1475329818
##########
datafusion/expr/src/expr_schema.rs:
##########
@@ -82,6 +82,13 @@ impl ExprSchemable for Expr {
Expr::Case(case) => case.when_then_expr[0].1.get_type(schema),
Expr::Cast(Cast { data_type, .. })
| Expr::TryCast(TryCast { data_type, .. }) => Ok(data_type.clone()),
+ Expr::Unnest(Unnest { exprs }) => {
+ let arg_data_types = exprs
Review Comment:
I think we can keep this, since they will be more in the future
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Unnest with single expression [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #9069:
URL: https://github.com/apache/arrow-datafusion/pull/9069#discussion_r1477301725
##########
datafusion/sqllogictest/test_files/unnest.slt:
##########
@@ -0,0 +1,72 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+############################
+# Unnest Expressions Tests #
+############################
+
+statement ok
+CREATE TABLE unnest_table
+AS VALUES
+ ([1,2,3], [7], 1),
Review Comment:
I added nulls in 79eb32d61 to keep this PR moving
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Unnest with single expression [arrow-datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #9069:
URL: https://github.com/apache/arrow-datafusion/pull/9069#discussion_r1474714641
##########
datafusion/core/src/physical_planner.rs:
##########
@@ -124,6 +124,9 @@ fn physical_name(e: &Expr) -> Result<String> {
fn create_physical_name(e: &Expr, is_first_expr: bool) -> Result<String> {
match e {
+ Expr::Unnest(_) => {
+ internal_err!("Expr::Unnest should have been converted to LogicalPlan::Unest")
Review Comment:
```suggestion
internal_err!("Expr::Unnest should have been converted to LogicalPlan::Unnest")
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Unnest with single expression [arrow-datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #9069:
URL: https://github.com/apache/arrow-datafusion/pull/9069#discussion_r1474725586
##########
datafusion/sqllogictest/test_files/unnest.slt:
##########
@@ -0,0 +1,72 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+############################
+# Unnest Expressions Tests #
+############################
+
+statement ok
+CREATE TABLE unnest_table
+AS VALUES
+ ([1,2,3], [7], 1),
+ ([4,5], [8,9,10], 2),
+ ([6], [11,12], 3)
+;
+
+query I
Review Comment:
please add some test description
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Unnest with single expression [arrow-datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on code in PR #9069:
URL: https://github.com/apache/arrow-datafusion/pull/9069#discussion_r1474723294
##########
datafusion/sql/src/expr/function.rs:
##########
@@ -70,6 +70,50 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
return Ok(Expr::ScalarFunction(ScalarFunction::new_udf(fm, args)));
}
+ // Build Unnest expression
+ if name.eq("unnest") {
+ let exprs =
+ self.function_args_to_expr(args.clone(), schema, planner_context)?;
+
+ match exprs.len() {
+ 0 => {
+ return exec_err!("unnest() requires at least one argument");
+ }
+ 1 => {
+ if let Expr::ScalarFunction(ScalarFunction {
+ func_def:
+ ScalarFunctionDefinition::BuiltIn(
+ BuiltinScalarFunction::MakeArray,
+ ),
+ ..
+ }) = exprs[0]
+ {
+ // valid
+ } else if let Expr::Column(_) = exprs[0] {
+ // valid
+ } else if let Expr::ScalarFunction(ScalarFunction {
+ func_def:
+ ScalarFunctionDefinition::BuiltIn(BuiltinScalarFunction::Struct),
+ ..
+ }) = exprs[0]
+ {
+ return not_impl_err!("unnest() does not support struct yet");
+ } else {
+ return plan_err!(
+ "unnest() can only be applied to array and structs and null"
Review Comment:
this line says unnest supports struct but line above says the opposite
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org