Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions datafusion/expr/src/expr_rewriter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add some context about why we maintain the original name only for columns?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

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 {
Expand Down
6 changes: 3 additions & 3 deletions datafusion/optimizer/tests/optimizer_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are ok, the alias had been introduced by #19019 but it was not needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it isn't needed because the outer query doesn't use id?

Filter: nodes.id < Int32(3)
TableScan: nodes projection=[id]
"
Expand All @@ -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]
Expand Down Expand Up @@ -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]
"
Expand Down
2 changes: 0 additions & 2 deletions datafusion/substrait/tests/cases/logical_plans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down