Skip to content

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

Merged
kosiew merged 7 commits into
apache:mainfrom
nuno-faria:fix_alias_case_sensitive
Jun 22, 2026
Merged

fix: Consider column names' case when aliasing tables#22917
kosiew merged 7 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.

@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 the iteration.
Looks 👍 to me

@kosiew kosiew added this pull request to the merge queue Jun 22, 2026
Merged via the queue into apache:main with commit f7598e3 Jun 22, 2026
38 checks passed
@nuno-faria nuno-faria deleted the fix_alias_case_sensitive branch June 22, 2026 19:04
pull Bot pushed a commit to buraksenn/datafusion that referenced this pull request Jun 27, 2026
## Which issue does this PR close?

Follow up to apache#22917, I overlooked these:
- Add SLT regression coverage for quoted dotted names / column-list
aliasing.
- remove redundant .clone() after columns() since it already returns
Vec<Column>.


## What changes are included in this PR?

This PR adds SLT regression coverage for quoted dotted column names used
with table column-list aliasing, including qualified and unqualified
references.

It also removes a redundant `.clone()` call after
`plan.schema().columns()`.

## Are these changes tested?

Yes. Tests are added in:

`datafusion/sqllogictest/test_files/alias.slt`

## Are there any user-facing changes?

No user-facing changes.

## LLM-generated code disclosure

This PR includes LLM-generated code and comments. All LLM-generated
content has been manually reviewed.
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