Skip to content
Merged
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
6 changes: 5 additions & 1 deletion datafusion/expr/src/expr_rewriter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,11 @@ fn coerce_exprs_for_schema(
}
#[expect(deprecated)]
Expr::Wildcard { .. } => Ok(expr),
_ => expr.cast_to(new_type, src_schema),
_ => {
// maintain the original name when casting
let name = dst_schema.field(idx).name();
Ok(expr.cast_to(new_type, src_schema)?.alias(name))
}
}
} else {
Ok(expr)
Expand Down
14 changes: 7 additions & 7 deletions datafusion/optimizer/src/analyzer/type_coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ mod test {
true,
plan.clone(),
@r"
Projection: CAST(a AS LargeUtf8)
Projection: CAST(a AS LargeUtf8) AS a
EmptyRelation: rows=0
"
)?;
Expand Down Expand Up @@ -1341,7 +1341,7 @@ mod test {
true,
plan.clone(),
@r"
Projection: CAST(a AS LargeUtf8)
Projection: CAST(a AS LargeUtf8) AS a
EmptyRelation: rows=0
"
)?;
Expand Down Expand Up @@ -1371,7 +1371,7 @@ mod test {
true,
sort_plan.clone(),
@r"
Projection: CAST(a AS LargeUtf8)
Projection: CAST(a AS LargeUtf8) AS a
Sort: a ASC NULLS FIRST
Projection: a
EmptyRelation: rows=0
Expand Down Expand Up @@ -1400,7 +1400,7 @@ mod test {
true,
plan.clone(),
@r"
Projection: CAST(a AS LargeUtf8)
Projection: CAST(a AS LargeUtf8) AS a
Sort: a ASC NULLS FIRST
Projection: a
EmptyRelation: rows=0
Expand Down Expand Up @@ -1436,7 +1436,7 @@ mod test {
true,
plan.clone(),
@r"
Projection: CAST(a AS LargeBinary)
Projection: CAST(a AS LargeBinary) AS a
EmptyRelation: rows=0
"
)?;
Expand Down Expand Up @@ -1493,7 +1493,7 @@ mod test {
true,
sort_plan.clone(),
@r"
Projection: CAST(a AS LargeBinary)
Projection: CAST(a AS LargeBinary) AS a
Sort: a ASC NULLS FIRST
Projection: a
EmptyRelation: rows=0
Expand Down Expand Up @@ -1524,7 +1524,7 @@ mod test {
true,
plan.clone(),
@r"
Projection: CAST(a AS LargeBinary)
Projection: CAST(a AS LargeBinary) AS a
Sort: a ASC NULLS FIRST
Projection: a
EmptyRelation: rows=0
Expand Down
54 changes: 29 additions & 25 deletions datafusion/optimizer/tests/optimizer_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,14 +538,15 @@ fn recursive_cte_projection_pushdown() -> Result<()> {
// columns from the base table and recursive table, eliminating unused columns
assert_snapshot!(
format!("{plan}"),
@r#"SubqueryAlias: nodes
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)
Filter: nodes.id < Int32(3)
TableScan: nodes projection=[id]
"#
@r"
SubqueryAlias: nodes
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
Filter: nodes.id < Int32(3)
TableScan: nodes projection=[id]
"
);
Ok(())
}
Expand All @@ -561,14 +562,16 @@ fn recursive_cte_with_aliased_self_reference() -> Result<()> {

assert_snapshot!(
format!("{plan}"),
@r#"SubqueryAlias: nodes
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)
SubqueryAlias: child
Filter: nodes.id < Int32(3)
TableScan: nodes projection=[id]"#,
@r"
SubqueryAlias: nodes
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
SubqueryAlias: child
Filter: nodes.id < Int32(3)
TableScan: nodes projection=[id]
",
);
Ok(())
}
Expand Down Expand Up @@ -620,15 +623,16 @@ fn recursive_cte_projection_pushdown_baseline() -> Result<()> {
// and only the needed column is selected from the recursive table
assert_snapshot!(
format!("{plan}"),
@r#"SubqueryAlias: countdown
RecursiveQuery: is_distinct=false
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)
Filter: countdown.n > Int32(1)
TableScan: countdown projection=[n]
"#
@r"
SubqueryAlias: countdown
RecursiveQuery: is_distinct=false
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
Filter: countdown.n > Int32(1)
TableScan: countdown projection=[n]
"
);
Ok(())
}
Expand Down
36 changes: 36 additions & 0 deletions datafusion/sqllogictest/test_files/cast.slt
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,39 @@ select * from t0 where v0<1e100;

statement ok
drop table t0;


# ensure that automatically casting with "datafusion.optimizer.expand_views_at_output" does not
# change the column name

statement ok
create table t(a int, b varchar);

statement ok
set datafusion.optimizer.expand_views_at_output = true;

query TT
explain select * from t;
----
logical_plan
01)Projection: t.a, CAST(t.b AS LargeUtf8) AS b
02)--TableScan: t projection=[a, b]
physical_plan
01)ProjectionExec: expr=[a@0 as a, CAST(b@1 AS LargeUtf8) as b]
02)--DataSourceExec: partitions=1, partition_sizes=[0]

query TT
explain select b from t;
----
logical_plan
01)Projection: CAST(t.b AS LargeUtf8) AS b
02)--TableScan: t projection=[b]
physical_plan
01)ProjectionExec: expr=[CAST(b@0 AS LargeUtf8) as b]
02)--DataSourceExec: partitions=1, partition_sizes=[0]

statement ok
set datafusion.optimizer.expand_views_at_output = false;

statement ok
drop table t;