Skip to content

Commit f7598e3

Browse files
authored
fix: Consider column names' case when aliasing tables (#22917)
## Which issue does this PR close? - Closes #22916. ## Rationale for this change Avoid errors when aliasing tables/subqueries with case sensitive column names. ## What changes are included in this PR? - Update `apply_expr_alias` so it aliases the original columns directly. ## Are these changes tested? Yes. ## Are there any user-facing changes? No.
1 parent 330d654 commit f7598e3

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

datafusion/sql/src/planner.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ use datafusion_common::{
3232
DFSchemaRef, Diagnostic, SchemaError, field_not_found, internal_err,
3333
plan_datafusion_err,
3434
};
35+
use datafusion_expr::Expr;
3536
use datafusion_expr::logical_plan::{LogicalPlan, LogicalPlanBuilder};
3637
pub use datafusion_expr::planner::ContextProvider;
3738
use datafusion_expr::utils::find_column_exprs;
38-
use datafusion_expr::{Expr, col};
3939
use sqlparser::ast::{ArrayElemTypeDef, ExactNumberInfo, TimezoneInfo};
4040
use sqlparser::ast::{ColumnDef as SQLColumnDef, ColumnOption};
4141
use sqlparser::ast::{DataType as SQLDataType, Ident, ObjectName, TableAlias};
@@ -591,10 +591,10 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
591591
idents.len()
592592
)
593593
} else {
594-
let fields = plan.schema().fields().clone();
594+
let columns = plan.schema().columns().clone();
595595
LogicalPlanBuilder::from(plan)
596-
.project(fields.iter().zip(idents).map(|(field, ident)| {
597-
col(field.name()).alias(self.ident_normalizer.normalize(ident))
596+
.project(columns.into_iter().zip(idents).map(|(col, ident)| {
597+
Expr::Column(col).alias(self.ident_normalizer.normalize(ident))
598598
}))?
599599
.build()
600600
}

datafusion/sqllogictest/test_files/alias.slt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,50 @@ drop table t1;
5757

5858
statement count 0
5959
drop table t2;
60+
61+
62+
# Test table-aliasing a subquery with case sensitive columns
63+
# (https://github.com/apache/datafusion/issues/22916)
64+
65+
statement ok
66+
create table t ("A" int, "B.C" int);
67+
68+
query II
69+
select * from (select * from t) t_(x, y);
70+
----
71+
72+
query TT
73+
explain select * from (select * from t) t_(x, y);
74+
----
75+
logical_plan
76+
01)SubqueryAlias: t_
77+
02)--Projection: t.A AS x, t.B.C AS y
78+
03)----TableScan: t projection=[A, B.C]
79+
physical_plan
80+
01)ProjectionExec: expr=[A@0 as x, B.C@1 as y]
81+
02)--DataSourceExec: partitions=1, partition_sizes=[0]
82+
83+
query TT
84+
explain select * from (select * from t) t_(X, Y);
85+
----
86+
logical_plan
87+
01)SubqueryAlias: t_
88+
02)--Projection: t.A AS x, t.B.C AS y
89+
03)----TableScan: t projection=[A, B.C]
90+
physical_plan
91+
01)ProjectionExec: expr=[A@0 as x, B.C@1 as y]
92+
02)--DataSourceExec: partitions=1, partition_sizes=[0]
93+
94+
query TT
95+
explain select * from (select * from t) t_("X", "Y");
96+
----
97+
logical_plan
98+
01)SubqueryAlias: t_
99+
02)--Projection: t.A AS X, t.B.C AS Y
100+
03)----TableScan: t projection=[A, B.C]
101+
physical_plan
102+
01)ProjectionExec: expr=[A@0 as X, B.C@1 as Y]
103+
02)--DataSourceExec: partitions=1, partition_sizes=[0]
104+
105+
statement ok
106+
drop table t;

0 commit comments

Comments
 (0)