Skip to content

Apache DataFusion SQL Query Engine

fix: Ensure columns are casted to the correct names with Unions (#20146)

Nuno Fariad7925715caaa
diff --git a/datafusion/expr/src/expr_rewriter/mod.rs b/datafusion/expr/src/expr_rewriter/mod.rs
index a0faca76e9..32a88ab8cf 100644
--- a/datafusion/expr/src/expr_rewriter/mod.rs
+++ b/datafusion/expr/src/expr_rewriter/mod.rs
@@ -261,9 +261,16 @@ fn coerce_exprs_for_schema(
#[expect(deprecated)]
Expr::Wildcard { .. } => Ok(expr),
_ => {
- // maintain the original name when casting
- let name = dst_schema.field(idx).name();
- Ok(expr.cast_to(new_type, src_schema)?.alias(name))
+ match expr {
+ // maintain the original name when casting a column, to avoid the
+ // tablename being added to it when not explicitly set by the query
+ // (see: https://github.com/apache/datafusion/issues/18818)
+ Expr::Column(ref column) => {
+ let name = column.name().to_owned();
+ Ok(expr.cast_to(new_type, src_schema)?.alias(name))
+ }
+ _ => Ok(expr.cast_to(new_type, src_schema)?),
+ }
}
}
} else {
diff --git a/datafusion/optimizer/tests/optimizer_integration.rs b/datafusion/optimizer/tests/optimizer_integration.rs
index 36a6df54dd..fd4991c244 100644
--- a/datafusion/optimizer/tests/optimizer_integration.rs
+++ b/datafusion/optimizer/tests/optimizer_integration.rs
@@ -543,7 +543,7 @@ fn recursive_cte_projection_pushdown() -> Result<()> {
RecursiveQuery: is_distinct=false
Projection: test.col_int32 AS id
TableScan: test projection=[col_int32]
- Projection: CAST(CAST(nodes.id AS Int64) + Int64(1) AS Int32) AS id
+ Projection: CAST(CAST(nodes.id AS Int64) + Int64(1) AS Int32)
Filter: nodes.id < Int32(3)
TableScan: nodes projection=[id]
"
@@ -567,7 +567,7 @@ fn recursive_cte_with_aliased_self_reference() -> Result<()> {
RecursiveQuery: is_distinct=false
Projection: test.col_int32 AS id
TableScan: test projection=[col_int32]
- Projection: CAST(CAST(child.id AS Int64) + Int64(1) AS Int32) AS id
+ Projection: CAST(CAST(child.id AS Int64) + Int64(1) AS Int32)
SubqueryAlias: child
Filter: nodes.id < Int32(3)
TableScan: nodes projection=[id]
@@ -630,7 +630,7 @@ fn recursive_cte_projection_pushdown_baseline() -> Result<()> {
Projection: test.col_int32 AS n
Filter: test.col_int32 = Int32(5)
TableScan: test projection=[col_int32]
- Projection: CAST(CAST(countdown.n AS Int64) - Int64(1) AS Int32) AS n
+ Projection: CAST(CAST(countdown.n AS Int64) - Int64(1) AS Int32)
Filter: countdown.n > Int32(1)
TableScan: countdown projection=[n]
"
diff --git a/datafusion/substrait/tests/cases/logical_plans.rs b/datafusion/substrait/tests/cases/logical_plans.rs
index 115c598430..79a19dc92b 100644
--- a/datafusion/substrait/tests/cases/logical_plans.rs
+++ b/datafusion/substrait/tests/cases/logical_plans.rs
@@ -231,8 +231,6 @@ mod tests {
}
#[tokio::test]
- // Test still failing, issue tracked in "https://github.com/apache/datafusion/issues/20123".
- #[ignore]
async fn duplicate_name_in_union() -> Result<()> {
let proto_plan =
read_json("tests/testdata/test_plans/duplicate_name_in_union.substrait.json");