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


##########
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:
   Thanks for catching that. Replaced the per-row `ProjectionExec` + 
`UnionExec` with a single-partition `LazyMemoryExec` (`ValuesBatchGenerator`), 
which keeps rows in order, and added an ordered `VALUES` test in `subquery.slt`.



##########
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:
   Replaced `ProjectionExec` + `UnionExec` with `LazyMemoryExec` using 
`Values.schema`, preserving field metadata on both the physical plan and output 
batches. Added `values_scalar_subquery_preserves_schema_metadata_and_grouping` 
in `physical_planner.rs`.



##########
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:
   Batched deferred evaluation in `ValuesBatchGenerator` inside a 
single-partition `LazyMemoryExec` using `execution.batch_size`, and added 
`values_scalar_subquery_batches_large_mixed_rows` covering a 16,000-row mixed 
list.



##########
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:
   Removed the private helper and reused `Expr::contains_scalar_subquery`.



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