Skip to content

Apache DataFusion SQL Query Engine

fix: `with_param_values` on `LogicalPlan::EmptyRelation` returns incorrect schema (#18286)

Khanh Duong92a3a3302067
diff --git a/datafusion/core/tests/dataframe/mod.rs b/datafusion/core/tests/dataframe/mod.rs
index 4a49dfb2ed..50b7b9f433 100644
--- a/datafusion/core/tests/dataframe/mod.rs
+++ b/datafusion/core/tests/dataframe/mod.rs
@@ -5423,7 +5423,7 @@ async fn test_dataframe_placeholder_column_parameter() -> Result<()> {
assert_snapshot!(
actual,
@r"
- Projection: Int32(3) AS $1 [$1:Null;N]
+ Projection: Int32(3) AS $1 [$1:Int32]
EmptyRelation: rows=1 []
"
);
diff --git a/datafusion/core/tests/sql/select.rs b/datafusion/core/tests/sql/select.rs
index 8a0f620627..28f0dcb8bd 100644
--- a/datafusion/core/tests/sql/select.rs
+++ b/datafusion/core/tests/sql/select.rs
@@ -18,7 +18,6 @@
use std::collections::HashMap;
use super::*;
-use datafusion::assert_batches_eq;
use datafusion_common::{metadata::ScalarAndMetadata, ParamValues, ScalarValue};
use insta::assert_snapshot;
@@ -343,26 +342,20 @@ async fn test_query_parameters_with_metadata() -> Result<()> {
]))
.unwrap();
- // df_with_params_replaced.schema() is not correct here
- // https://github.com/apache/datafusion/issues/18102
- let batches = df_with_params_replaced.clone().collect().await.unwrap();
- let schema = batches[0].schema();
-
+ let schema = df_with_params_replaced.schema();
assert_eq!(schema.field(0).data_type(), &DataType::UInt32);
assert_eq!(schema.field(0).metadata(), &metadata1);
assert_eq!(schema.field(1).data_type(), &DataType::Utf8);
assert_eq!(schema.field(1).metadata(), &metadata2);
- assert_batches_eq!(
- [
- "+----+-----+",
- "| $1 | $2 |",
- "+----+-----+",
- "| 1 | two |",
- "+----+-----+",
- ],
- &batches
- );
+ let batches = df_with_params_replaced.collect().await.unwrap();
+ assert_snapshot!(batches_to_sort_string(&batches), @r"
+ +----+-----+
+ | $1 | $2 |
+ +----+-----+
+ | 1 | two |
+ +----+-----+
+ ");
Ok(())
}
diff --git a/datafusion/expr/src/logical_plan/plan.rs b/datafusion/expr/src/logical_plan/plan.rs
index 892ab135d6..c70501f110 100644
--- a/datafusion/expr/src/logical_plan/plan.rs
+++ b/datafusion/expr/src/logical_plan/plan.rs
@@ -1471,7 +1471,10 @@ impl LogicalPlan {
// Preserve name to avoid breaking column references to this expression
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(|res| res.data)
}
@@ -4248,6 +4251,7 @@ mod tests {
use super::*;
use crate::builder::LogicalTableSource;
use crate::logical_plan::table_scan;
+ use crate::select_expr::SelectExpr;
use crate::test::function_stub::{count, count_udaf};
use crate::{
binary_expr, col, exists, in_subquery, lit, placeholder, scalar_subquery,
@@ -4826,6 +4830,35 @@ mod tests {
.expect_err("prepared field metadata mismatch unexpectedly succeeded");
}
+ #[test]
+ fn test_replace_placeholder_empty_relation_valid_schema() {
+ // SELECT $1, $2;
+ let plan = LogicalPlanBuilder::empty(false)
+ .project(vec![
+ SelectExpr::from(placeholder("$1")),
+ SelectExpr::from(placeholder("$2")),
+ ])
+ .unwrap()
+ .build()
+ .unwrap();
+
+ // original
+ assert_snapshot!(plan.display_indent_schema(), @r"
+ Projection: $1, $2 [$1:Null;N, $2:Null;N]
+ EmptyRelation: rows=0 []
+ ");
+
+ let plan = plan
+ .with_param_values(vec![ScalarValue::from(1i32), ScalarValue::from("s")])
+ .unwrap();
+
+ // replaced
+ assert_snapshot!(plan.display_indent_schema(), @r#"
+ Projection: Int32(1) AS $1, Utf8("s") AS $2 [$1:Int32, $2:Utf8]
+ EmptyRelation: rows=0 []
+ "#);
+ }
+
#[test]
fn test_nullable_schema_after_grouping_set() {
let schema = Schema::new(vec![