Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 4 additions & 3 deletions datafusion/sql/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ use std::vec;

use crate::utils::make_decimal_type;
use arrow::datatypes::*;
use datafusion_common::TableReference;
use datafusion_common::config::SqlParserOptions;
use datafusion_common::datatype::{DataTypeExt, FieldExt};
use datafusion_common::error::add_possible_columns_to_diag;
use datafusion_common::{Column, TableReference};
use datafusion_common::{DFSchema, DataFusionError, Result, not_impl_err, plan_err};
use datafusion_common::{
DFSchemaRef, Diagnostic, SchemaError, field_not_found, internal_err,
plan_datafusion_err,
};
use datafusion_expr::Expr;
use datafusion_expr::logical_plan::{LogicalPlan, LogicalPlanBuilder};
pub use datafusion_expr::planner::ContextProvider;
use datafusion_expr::utils::find_column_exprs;
use datafusion_expr::{Expr, col};
use sqlparser::ast::{ArrayElemTypeDef, ExactNumberInfo, TimezoneInfo};
use sqlparser::ast::{ColumnDef as SQLColumnDef, ColumnOption};
use sqlparser::ast::{DataType as SQLDataType, Ident, ObjectName, TableAlias};
Expand Down Expand Up @@ -575,7 +575,8 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
let fields = plan.schema().fields().clone();
LogicalPlanBuilder::from(plan)
.project(fields.iter().zip(idents).map(|(field, ident)| {
col(field.name()).alias(self.ident_normalizer.normalize(ident))
Expr::Column(Column::from_qualified_name_ignore_case(field.name()))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

apply_expr_alias still rebuilds a column reference by reparsing field.name() as SQL text. That helps with the simple case-sensitive name case, but quoted identifiers can legally contain separators like ..

For example, CREATE TABLE t ("A.B" int); SELECT * FROM (SELECT * FROM t) t_(x); is still resolved as a qualified column A.B instead of the existing field named A.B, so aliasing still errors.

I think the important invariant here is positional: each existing output field should be aliased while preserving its already resolved schema identity. Could we build Expr::Column from plan.schema().columns() or qualified_field instead of parsing the display name string? One way to do that would be to collect the schema columns before moving plan, then zip those with idents.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks @kosiew, it's better to alias the existing columns directly. I also updated the test to include a column with a dot.

.alias(self.ident_normalizer.normalize(ident))
}))?
.build()
}
Expand Down
47 changes: 47 additions & 0 deletions datafusion/sqllogictest/test_files/alias.slt
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,50 @@ drop table t1;

statement count 0
drop table t2;


# Test table-aliasing a subquery with a case sensitive column
# (https://github.com/apache/datafusion/issues/22916)

statement ok
create table t ("A" int);

query I
select * from (select * from t) t_(x);
----

query TT
explain select * from (select * from t) t_(x);
----
logical_plan
01)SubqueryAlias: t_
02)--Projection: t.A AS x
03)----TableScan: t projection=[A]
physical_plan
01)ProjectionExec: expr=[A@0 as x]
02)--DataSourceExec: partitions=1, partition_sizes=[0]

query TT
explain select * from (select * from t) t_(X);
----
logical_plan
01)SubqueryAlias: t_
02)--Projection: t.A AS x
03)----TableScan: t projection=[A]
physical_plan
01)ProjectionExec: expr=[A@0 as x]
02)--DataSourceExec: partitions=1, partition_sizes=[0]

query TT
explain select * from (select * from t) t_("X");
----
logical_plan
01)SubqueryAlias: t_
02)--Projection: t.A AS X
03)----TableScan: t projection=[A]
physical_plan
01)ProjectionExec: expr=[A@0 as X]
02)--DataSourceExec: partitions=1, partition_sizes=[0]

statement ok
drop table t;
Loading