Skip to content

Conversation

@nuno-faria
Copy link
Contributor

Which issue does this PR close?

Rationale for this change

When aliasing a cast, use the original name to prevent duplicate column names errors.

What changes are included in this PR?

  • Updated coerce_exprs_for_schema to use the correct name.
  • Updated tests.

Are these changes tested?

Yes.

Are there any user-facing changes?

No.

@github-actions github-actions bot added logical-expr Logical plan and expressions optimizer Optimizer rules substrait Changes to the substrait crate labels Feb 4, 2026
Projection: test.col_int32 AS id
TableScan: test projection=[col_int32]
Projection: CAST(CAST(nodes.id AS Int64) + Int64(1) AS Int32) AS id
Projection: CAST(CAST(nodes.id AS Int64) + Int64(1) AS Int32)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These changes are ok, the alias had been introduced by #19019 but it was not needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

it isn't needed because the outer query doesn't use id?

Copy link
Contributor

@dd-annarose dd-annarose left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thanks @nuno-faria -- this looks good to me. Given all the tests pass I think this one is good to go, though it is strange to me 🤔 I wonder if this is something specific to unions

Ok(expr.cast_to(new_type, src_schema)?.alias(name))
match expr {
// maintain the original name when casting a column
Expr::Column(ref column) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we add some context about why we maintain the original name only for columns?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

Projection: test.col_int32 AS id
TableScan: test projection=[col_int32]
Projection: CAST(CAST(nodes.id AS Int64) + Int64(1) AS Int32) AS id
Projection: CAST(CAST(nodes.id AS Int64) + Int64(1) AS Int32)
Copy link
Contributor

Choose a reason for hiding this comment

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

it isn't needed because the outer query doesn't use id?

@nuno-faria
Copy link
Contributor Author

it isn't needed because the outer query doesn't use id?

The outer query uses id but the cast on the recursive branch was not needed since I think the name is set by the non-recursive term (or by the CTE definition).

@alamb alamb added this pull request to the merge queue Feb 8, 2026
@alamb
Copy link
Contributor

alamb commented Feb 8, 2026

Thanks again @nuno-faria

Merged via the queue into apache:main with commit d792571 Feb 8, 2026
32 checks passed
@nuno-faria nuno-faria deleted the duplicate_cast_union branch February 8, 2026 16:25
github-merge-queue bot pushed a commit that referenced this pull request Feb 9, 2026
## Which issue does this PR close?

- Follow on to #20146 from
@nuno-faria

## Rationale for this change

While reviewing the change with Codex, I noticed that the actual output
names and their uniqueness were not verified, which I think would help
the coverage

## What changes are included in this PR?

Extend the test  

## Are these changes tested?

Yes it is only tests

## Are there any user-facing changes?

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

Labels

logical-expr Logical plan and expressions optimizer Optimizer rules substrait Changes to the substrait crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate unqualified name error on unions

3 participants