Skip to content

fix: Consider column names' case when aliasing tables#22917

Open
nuno-faria wants to merge 6 commits into
apache:mainfrom
nuno-faria:fix_alias_case_sensitive
Open

fix: Consider column names' case when aliasing tables#22917
nuno-faria wants to merge 6 commits into
apache:mainfrom
nuno-faria:fix_alias_case_sensitive

Conversation

@nuno-faria

@nuno-faria nuno-faria commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

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.

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) common Related to common crate labels Jun 11, 2026
@github-actions github-actions Bot added sql SQL Planner and removed common Related to common crate labels Jun 11, 2026

@kosiew kosiew left a comment

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.

@nuno-faria
Thanks for working on this. I think there is still one blocking case around quoted identifiers with separators.

Comment thread datafusion/sql/src/planner.rs Outdated
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sql SQL Planner sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Table alias fails with case sensitive column names

2 participants