Apache DataFusion SQL Query Engine
fix: update schema's data type for `LogicalPlan::Values` after placeholder substitution (#18740)
Khanh Duong82181ac48c1c
diff --git a/datafusion/expr/src/logical_plan/plan.rs b/datafusion/expr/src/logical_plan/plan.rsindex 64db994c22..ad9a46b004 100644--- a/datafusion/expr/src/logical_plan/plan.rs+++ b/datafusion/expr/src/logical_plan/plan.rs@@ -1476,13 +1476,29 @@ impl LogicalPlan {Ok(transformed_expr.update_data(|expr| original_name.restore(expr)))}})?- // always recompute the schema to ensure the changed in the schema's field should be- // poplulated to the plan's parent- .map_data(|plan| plan.recompute_schema())+ .map_data(|plan| plan.update_schema_data_type())}).map(|res| res.data)}+ /// Recompute schema fields' data type after replacing params, ensuring fields data type can be+ /// updated according to the new parameters.+ ///+ /// Unlike `recompute_schema()`, this method rebuilds VALUES plans entirely to properly infer+ /// types types from literal values after placeholder substitution.+ fn update_schema_data_type(self) -> Result<LogicalPlan> {+ match self {+ // Build `LogicalPlan::Values` from the values for type inference.+ // We can't use `recompute_schema` because it skips recomputing for+ // `LogicalPlan::Values`.+ LogicalPlan::Values(Values { values, schema: _ }) => {+ LogicalPlanBuilder::values(values)?.build()+ }+ // other plans can just use `recompute_schema` directly.+ plan => plan.recompute_schema(),+ }+ }+/// Walk the logical plan, find any `Placeholder` tokens, and return a set of their names.pub fn get_parameter_names(&self) -> Result<HashSet<String>> {let mut param_names = HashSet::new();diff --git a/datafusion/sqllogictest/test_files/prepare.slt b/datafusion/sqllogictest/test_files/prepare.sltindex 9727f4d33a..8e8b1cd8e6 100644--- a/datafusion/sqllogictest/test_files/prepare.slt+++ b/datafusion/sqllogictest/test_files/prepare.slt@@ -361,3 +361,15 @@ SET datafusion.explain.logical_plan_only=false;statement okDEALLOCATE my_plan+++statement ok+PREPARE my_plan AS SELECT a, b FROM (VALUES ($1, $2)) AS t(a, b);++query II+EXECUTE my_plan(1, 2)+----+1 2++statement ok+DEALLOCATE my_plan