alamb commented on code in PR #9400:
URL: https://github.com/apache/arrow-datafusion/pull/9400#discussion_r1508247005


##########
datafusion/sqllogictest/test_files/unnest.slt:
##########
@@ -91,6 +93,44 @@ NULL
 42
 NULL
 
+## Unnest with additional column
+## Issue: https://github.com/apache/arrow-datafusion/issues/9349
+query II
+select unnest(column1), column3 from unnest_table;
+----
+1 1
+2 1
+3 1
+4 2
+5 2
+6 3
+12 NULL

Review Comment:
   💯  for testing nulls



##########
datafusion/sql/src/select.rs:
##########
@@ -282,30 +282,38 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
         input: LogicalPlan,
         select_exprs: Vec<Expr>,
     ) -> Result<LogicalPlan> {
-        let mut exprs_to_unnest = vec![];
-
-        for expr in select_exprs.iter() {
-            if let Expr::Unnest(Unnest { exprs }) = expr {
-                exprs_to_unnest.push(exprs[0].clone());
-            }
-        }
+        let mut unnest_columns = vec![];
+        // Map unnest expressions to their argument
+        let projection_exprs = select_exprs
+            .into_iter()
+            .map(|expr| {
+                if let Expr::Unnest(Unnest { ref exprs }) = expr {
+                    let column_name = expr.display_name()?;
+                    unnest_columns.push(column_name.clone());
+                    // Add alias for the argument expression, to avoid naming 
conflicts with other expressions
+                    // in the select list. For example: `select unnest(col1), 
col1 from t`.
+                    Ok(exprs[0].clone().alias(column_name))
+                } else {
+                    Ok(expr)
+                }
+            })
+            .collect::<Result<Vec<_>>>()?;
 
         // Do the final projection
-        if exprs_to_unnest.is_empty() {
+        if unnest_columns.is_empty() {
             LogicalPlanBuilder::from(input)
-                .project(select_exprs)?
+                .project(projection_exprs)?
                 .build()
         } else {
-            if exprs_to_unnest.len() > 1 {
+            if unnest_columns.len() > 1 {
                 return not_impl_err!("Only support single unnest expression 
for now");
             }
-
-            let expr = exprs_to_unnest[0].clone();
-            let column = expr.display_name()?;
-
+            let unnest_column = unnest_columns.pop().unwrap();
+            // Set preserve_nulls to false to ensure compatibility with DuckDB 
and PostgreSQL

Review Comment:
   👍 



-- 
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