This is an automated email from the ASF dual-hosted git repository.
github-bot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new 2626fc2024 fix: Ensure column names do not change with
`expand_views_at_output` (#19019)
2626fc2024 is described below
commit 2626fc20244aa2a5065ab4534f3f96a699911d0e
Author: Nuno Faria <[email protected]>
AuthorDate: Tue Dec 9 11:28:03 2025 +0000
fix: Ensure column names do not change with `expand_views_at_output`
(#19019)
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes #123` indicates that this PR will close issue #123.
-->
- Closes #18818.
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->
Making the schema consistent whether having
`datafusion.optimizer.expand_views_at_output` on or off:
```sql
> create table t(a int, b varchar);
> set datafusion.optimizer.expand_views_at_output = true;
-- before
> select * from t;
+---+-----+
| a | t.b |
+---+-----+
+---+-----+
-- now
> select * from t;
+---+---+
| a | b |
+---+---+
+---+---+
```
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->
- Added an alias when converting expressions.
- Added sqllogictests.
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->
Yes.
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->
The column names when enabling
`datafusion.optimizer.expand_views_at_output` are now the same when
having it disabled.
<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
---------
Co-authored-by: Andrew Lamb <[email protected]>
---
datafusion/expr/src/expr_rewriter/mod.rs | 6 ++-
datafusion/optimizer/src/analyzer/type_coercion.rs | 14 +++---
.../optimizer/tests/optimizer_integration.rs | 54 ++++++++++++----------
datafusion/sqllogictest/test_files/cast.slt | 36 +++++++++++++++
4 files changed, 77 insertions(+), 33 deletions(-)
diff --git a/datafusion/expr/src/expr_rewriter/mod.rs
b/datafusion/expr/src/expr_rewriter/mod.rs
index f5f49c643c..a0faca76e9 100644
--- a/datafusion/expr/src/expr_rewriter/mod.rs
+++ b/datafusion/expr/src/expr_rewriter/mod.rs
@@ -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)
diff --git a/datafusion/optimizer/src/analyzer/type_coercion.rs
b/datafusion/optimizer/src/analyzer/type_coercion.rs
index a557d3356d..85751fd70c 100644
--- a/datafusion/optimizer/src/analyzer/type_coercion.rs
+++ b/datafusion/optimizer/src/analyzer/type_coercion.rs
@@ -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
"
)?;
@@ -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
"
)?;
@@ -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
@@ -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
@@ -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
"
)?;
@@ -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
@@ -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
diff --git a/datafusion/optimizer/tests/optimizer_integration.rs
b/datafusion/optimizer/tests/optimizer_integration.rs
index 31ec026c1c..180d85be20 100644
--- a/datafusion/optimizer/tests/optimizer_integration.rs
+++ b/datafusion/optimizer/tests/optimizer_integration.rs
@@ -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(())
}
@@ -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(())
}
@@ -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(())
}
diff --git a/datafusion/sqllogictest/test_files/cast.slt
b/datafusion/sqllogictest/test_files/cast.slt
index 3466354e54..916895b8be 100644
--- a/datafusion/sqllogictest/test_files/cast.slt
+++ b/datafusion/sqllogictest/test_files/cast.slt
@@ -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;
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]