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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to