kosiew commented on code in PR #27329:
URL: https://github.com/apache/datafusion/pull/27329#discussion_r4767777026


##########
datafusion/core/src/physical_planner.rs:
##########
@@ -847,6 +847,40 @@ impl DefaultPhysicalPlanner {
                     plan
                 }
             }
+            // A values list is evaluated while it is planned, before the 
scalar
+            // subqueries have run, so rows with a scalar subquery are 
projected
+            // from a placeholder row at execution time instead.
+            LogicalPlan::Values(Values { values, schema })
+                if values.iter().flatten().any(contains_scalar_subquery) =>
+            {
+                let placeholder: Arc<dyn ExecutionPlan> =
+                    
Arc::new(PlaceholderRowExec::new(Arc::new(Schema::empty())));
+                let rows = values
+                    .iter()
+                    .map(|row| {
+                        let exprs = row
+                            .iter()
+                            .zip(schema.fields())
+                            .map(|(expr, field)| {
+                                Ok(ProjectionExpr {
+                                    expr: create_physical_expr(
+                                        expr,
+                                        schema,
+                                        execution_props,
+                                        planning_ctx,
+                                    )?,
+                                    alias: field.name().clone(),
+                                })
+                            })
+                            .collect::<Result<Vec<_>>>()?;
+                        Ok(Arc::new(ProjectionExec::try_new(

Review Comment:
   ProjectionExec re-infers fields, dropping metadata supplied through 
values_with_schema and causing grouping to fail. Please preserve Values.schema 
and add a regression verifying physical and batch schemas, including metadata, 
and successful grouping.



##########
datafusion/core/src/physical_planner.rs:
##########
@@ -847,6 +847,40 @@ impl DefaultPhysicalPlanner {
                     plan
                 }
             }
+            // A values list is evaluated while it is planned, before the 
scalar
+            // subqueries have run, so rows with a scalar subquery are 
projected
+            // from a placeholder row at execution time instead.
+            LogicalPlan::Values(Values { values, schema })
+                if values.iter().flatten().any(contains_scalar_subquery) =>

Review Comment:
   Could we reuse Expr::contains_scalar_subquery here? It already traverses 
nested expressions, making the new private helper unnecessary.



##########
datafusion/core/src/physical_planner.rs:
##########
@@ -847,6 +847,40 @@ impl DefaultPhysicalPlanner {
                     plan
                 }
             }
+            // A values list is evaluated while it is planned, before the 
scalar
+            // subqueries have run, so rows with a scalar subquery are 
projected
+            // from a placeholder row at execution time instead.
+            LogicalPlan::Values(Values { values, schema })
+                if values.iter().flatten().any(contains_scalar_subquery) =>
+            {
+                let placeholder: Arc<dyn ExecutionPlan> =
+                    
Arc::new(PlaceholderRowExec::new(Arc::new(Schema::empty())));
+                let rows = values
+                    .iter()
+                    .map(|row| {
+                        let exprs = row
+                            .iter()
+                            .zip(schema.fields())
+                            .map(|(expr, field)| {
+                                Ok(ProjectionExpr {
+                                    expr: create_physical_expr(
+                                        expr,
+                                        schema,
+                                        execution_props,
+                                        planning_ctx,
+                                    )?,
+                                    alias: field.name().clone(),
+                                })
+                            })
+                            .collect::<Result<Vec<_>>>()?;
+                        Ok(Arc::new(ProjectionExec::try_new(
+                            exprs,
+                            Arc::clone(&placeholder),
+                        )?) as Arc<dyn ExecutionPlan>)
+                    })
+                    .collect::<Result<Vec<_>>>()?;
+                UnionExec::try_new(rows)?

Review Comment:
   A mixed 16,000-row list produces 16,000 partitions and one-row batches, with 
quadratic UnionExec dispatch overhead. Please batch deferred rows in a single 
source and test bounded partition count, multi-row batches, and result 
correctness.



##########
datafusion/sqllogictest/test_files/subquery.slt:
##########
@@ -2675,6 +2675,18 @@ SELECT (SELECT count(*) FROM sq_empty), (SELECT v FROM 
sq_values);
 ## Uncorrelated scalar subqueries in various expression contexts
 #############
 
+# VALUES rows with uncorrelated scalar subqueries
+query II
+VALUES ((SELECT min(v) FROM sq_values), 5);
+----
+1 5
+
+query II rowsort
+SELECT * FROM (VALUES ((SELECT max(v) FROM sq_values), 5), (7, (SELECT min(v) 
FROM sq_values))) AS t(a, b);
+----
+3 5
+7 1

Review Comment:
   Could we add an INSERT and readback regression using (SELECT 3) + 2 with a 
SMALLINT target? This would cover nested subquery detection, target-column 
coercion, and persistence.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to