Skip to content

Fix name tracker#19856

Merged
gabotechs merged 10 commits intoapache:mainfrom
xanderbailey:xb/fix_name_tracker
Feb 24, 2026
Merged

Fix name tracker#19856
gabotechs merged 10 commits intoapache:mainfrom
xanderbailey:xb/fix_name_tracker

Conversation

@xanderbailey
Copy link
Contributor

@xanderbailey xanderbailey commented Jan 16, 2026

Which issue does this PR close?

Rationale for this change

The previous implementation used UUID-based aliasing as a workaround to prevent duplicate names for literals in Substrait plans. This approach had several drawbacks:

  • Non-deterministic plan names that made testing difficult (requiring UUID regex filters)
  • Only addressed literal naming conflicts, not the broader issue of name deduplication
  • Added unnecessary dependency on the uuid crate
  • Didn't properly handle cases where the same qualified name could appear with different schema representations

What changes are included in this PR?

  1. Enhanced NameTracker: Refactored to detect two types of conflicts:
    - Duplicate schema names: Tracked via schema_name() to prevent validate_unique_names failures (e.g., two Utf8(NULL) literals)
    - Ambiguous references: Tracked via qualified_name() to prevent DFSchema::check_names failures when a qualified field (e.g., left.Utf8(NULL)) and unqualified field (e.g., Utf8(NULL)) share the same column name
  2. Removed UUID dependency: Eliminated the uuid crate from datafusion/substrait
  3. Removed literal-specific aliasing: The UUID-based workaround in project_rel.rs is no longer needed as the improved NameTracker handles all naming conflicts consistently
  4. Deterministic naming: Name conflicts now use predictable __temp__N suffixes instead of random UUIDs

Note: This doesn't fully fix all the issues in #17508 which allow some special casing of CAST which are not included here.

Are these changes tested?

Yes:

  • Updated snapshot tests to reflect the new deterministic naming (e.g., Utf8("people")__temp__0 instead of UUID-based names)
  • Modified some roundtrip tests to verify semantic equivalence (schema matching and execution) rather than exact string matching, which is more robust
  • All existing integration tests pass with the new naming scheme

Are there any user-facing changes?

Minimal. The generated plan names are now deterministic and more readable (using __temp__N suffixes instead of UUIDs), but this is primarily an internal representation change. The functional behavior and query results remain unchanged.

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.

Makes sense. There's still the issue with CASTs that you mentioned in the PR description but this solution works. Handling the CAST issue seems to require a much much deeper rewrite; this solution is straightforward and enough for now.

@xanderbailey
Copy link
Contributor Author

Thanks for the review, I have a couple of failing test cases here that I need to look into. Will take a look on Monday and report back.

@alamb alamb marked this pull request as draft February 8, 2026 12:25
@xanderbailey xanderbailey marked this pull request as ready for review February 16, 2026 01:06
let mut counter = 0;
loop {
let candidate_name = format!("{schema_name}__temp__{counter}");
let candidate_expr = expr.clone().alias(candidate_name.clone());
Copy link
Contributor

Choose a reason for hiding this comment

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

This clone could be avoided by checking in the hashsets directly

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.

Thank you for the nice tests. This makes sense to me. Just left a small comment, it might be a little off as I haven't worked in the name tracker in a while.

let mut counter = 0;
let candidate_name = loop {
let candidate_name = format!("{schema_name}__temp__{counter}");
// .alias always produces an unqualified name so check for conflicts accordingly.
Copy link
Contributor

Choose a reason for hiding this comment

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

could we use alias_qualified() instead of alias or does that complicate things too much?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah great question, looked at that and I think it complicates things and I can't find a reason to change it. I.e. I couldn't write a failing test that it would fix so thought it was best to keep it as is. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

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

let's keep it as is then

@xanderbailey
Copy link
Contributor Author

xanderbailey commented Feb 17, 2026

@LiaCastaneda are you able to give this a look, seems like @dd-annarose and @hareshkh are good with it but I know you're also invested in the substrait work.

Hoping this will fix a number of the ambiguous reference errors we're seeing.

Copy link
Contributor

@LiaCastaneda LiaCastaneda left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this! 🙇‍♀️ I think this is a neat and easy to follow solution
cc @gabotechs or @alamb -- I think this PR makes sense. would either of you be able
to review whenever you have time?

let schema_name = expr.schema_name().to_string();
let mut counter = 0;
let candidate_name = loop {
let candidate_name = format!("{schema_name}__temp__{counter}");
Copy link
Contributor

Choose a reason for hiding this comment

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

There is also some logic to rename aliases to make them unique (used for avoiding duplicate names in join schemas here and here) This generates plans with :N suffixes like this, but this operates on Arrow Fields rather than Expr, so it can't be easily unified with the __temp__ mechanism. Maybe a future consistency improvement could standardize on one naming convention (using __temp__{N} everywhere), though probably the current distinction may be intentional (__temp__ = substrait conversion, :N = standard deduplication)?
(I'm not suggesting any change with this, it's an open question if it makes sense)

Copy link
Contributor

@gabotechs gabotechs left a comment

Choose a reason for hiding this comment

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

Looks good! thanks @xanderbailey

@gabotechs gabotechs added this pull request to the merge queue Feb 24, 2026
Merged via the queue into apache:main with commit d59cdfe Feb 24, 2026
31 checks passed
@gabotechs
Copy link
Contributor

Thanks @xanderbailey for the PR, and @dd-annarose, @hareshkh and @LiaCastaneda for the reviews.

hareshkh pushed a commit to hareshkh/datafusion that referenced this pull request Feb 25, 2026
- Closes apache#17508

The previous implementation used UUID-based aliasing as a workaround to
prevent duplicate names for literals in Substrait plans. This approach
had several drawbacks:
- Non-deterministic plan names that made testing difficult (requiring
UUID regex filters)
- Only addressed literal naming conflicts, not the broader issue of name
deduplication
- Added unnecessary dependency on the `uuid` crate
- Didn't properly handle cases where the same qualified name could
appear with different schema representations

  1. Enhanced NameTracker: Refactored to detect two types of conflicts:
- Duplicate schema names: Tracked via schema_name() to prevent
validate_unique_names failures (e.g., two Utf8(NULL) literals)
- Ambiguous references: Tracked via qualified_name() to prevent
DFSchema::check_names failures when a qualified field (e.g.,
left.Utf8(NULL)) and unqualified field (e.g., Utf8(NULL)) share the same
column name
2. **Removed UUID dependency**: Eliminated the `uuid` crate from
`datafusion/substrait`
3. **Removed literal-specific aliasing**: The UUID-based workaround in
`project_rel.rs` is no longer needed as the improved NameTracker handles
all naming conflicts consistently
4. **Deterministic naming**: Name conflicts now use predictable
`__temp__N` suffixes instead of random UUIDs

Note: This doesn't fully fix all the issues in apache#17508 which allow some
special casing of `CAST` which are not included here.

Yes:
- Updated snapshot tests to reflect the new deterministic naming (e.g.,
`Utf8("people")__temp__0` instead of UUID-based names)
- Modified some roundtrip tests to verify semantic equivalence (schema
matching and execution) rather than exact string matching, which is more
robust
- All existing integration tests pass with the new naming scheme

Minimal. The generated plan names are now deterministic and more
readable (using `__temp__N` suffixes instead of UUIDs), but this is
primarily an internal representation change. The functional behavior and
query results remain unchanged.
LiaCastaneda pushed a commit to DataDog/datafusion that referenced this pull request Feb 25, 2026
- Closes apache#17508

The previous implementation used UUID-based aliasing as a workaround to
prevent duplicate names for literals in Substrait plans. This approach
had several drawbacks:
- Non-deterministic plan names that made testing difficult (requiring
UUID regex filters)
- Only addressed literal naming conflicts, not the broader issue of name
deduplication
- Added unnecessary dependency on the `uuid` crate
- Didn't properly handle cases where the same qualified name could
appear with different schema representations

  1. Enhanced NameTracker: Refactored to detect two types of conflicts:
- Duplicate schema names: Tracked via schema_name() to prevent
validate_unique_names failures (e.g., two Utf8(NULL) literals)
- Ambiguous references: Tracked via qualified_name() to prevent
DFSchema::check_names failures when a qualified field (e.g.,
left.Utf8(NULL)) and unqualified field (e.g., Utf8(NULL)) share the same
column name
2. **Removed UUID dependency**: Eliminated the `uuid` crate from
`datafusion/substrait`
3. **Removed literal-specific aliasing**: The UUID-based workaround in
`project_rel.rs` is no longer needed as the improved NameTracker handles
all naming conflicts consistently
4. **Deterministic naming**: Name conflicts now use predictable
`__temp__N` suffixes instead of random UUIDs

Note: This doesn't fully fix all the issues in apache#17508 which allow some
special casing of `CAST` which are not included here.

Yes:
- Updated snapshot tests to reflect the new deterministic naming (e.g.,
`Utf8("people")__temp__0` instead of UUID-based names)
- Modified some roundtrip tests to verify semantic equivalence (schema
matching and execution) rather than exact string matching, which is more
robust
- All existing integration tests pass with the new naming scheme

Minimal. The generated plan names are now deterministic and more
readable (using `__temp__N` suffixes instead of UUIDs), but this is
primarily an internal representation change. The functional behavior and
query results remain unchanged.

(cherry picked from commit d59cdfe)
alamb pushed a commit that referenced this pull request Feb 25, 2026
- Closes #17508

The previous implementation used UUID-based aliasing as a workaround to
prevent duplicate names for literals in Substrait plans. This approach
had several drawbacks:
- Non-deterministic plan names that made testing difficult (requiring
UUID regex filters)
- Only addressed literal naming conflicts, not the broader issue of name
deduplication
- Added unnecessary dependency on the `uuid` crate
- Didn't properly handle cases where the same qualified name could
appear with different schema representations

  1. Enhanced NameTracker: Refactored to detect two types of conflicts:
- Duplicate schema names: Tracked via schema_name() to prevent
validate_unique_names failures (e.g., two Utf8(NULL) literals)
- Ambiguous references: Tracked via qualified_name() to prevent
DFSchema::check_names failures when a qualified field (e.g.,
left.Utf8(NULL)) and unqualified field (e.g., Utf8(NULL)) share the same
column name
2. **Removed UUID dependency**: Eliminated the `uuid` crate from
`datafusion/substrait`
3. **Removed literal-specific aliasing**: The UUID-based workaround in
`project_rel.rs` is no longer needed as the improved NameTracker handles
all naming conflicts consistently
4. **Deterministic naming**: Name conflicts now use predictable
`__temp__N` suffixes instead of random UUIDs

Note: This doesn't fully fix all the issues in #17508 which allow some
special casing of `CAST` which are not included here.

Yes:
- Updated snapshot tests to reflect the new deterministic naming (e.g.,
`Utf8("people")__temp__0` instead of UUID-based names)
- Modified some roundtrip tests to verify semantic equivalence (schema
matching and execution) rather than exact string matching, which is more
robust
- All existing integration tests pass with the new naming scheme

Minimal. The generated plan names are now deterministic and more
readable (using `__temp__N` suffixes instead of UUIDs), but this is
primarily an internal representation change. The functional behavior and
query results remain unchanged.

## Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes #123` indicates that this PR will close issue #123.
-->

- Closes #.

## Rationale for this change

<!--
Why are you proposing this change? If this is already explained clearly
in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand
your changes and offer better suggestions for fixes.
-->

## What changes are included in this PR?

<!--
There is no need to duplicate the description in the issue here but it
is sometimes worth providing a summary of the individual changes in this
PR.
-->

## Are these changes tested?

<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code

If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
-->

## Are there any user-facing changes?

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->

<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->

Co-authored-by: Xander <[email protected]>
LiaCastaneda added a commit to DataDog/datafusion that referenced this pull request Feb 26, 2026
- Closes apache#17508

The previous implementation used UUID-based aliasing as a workaround to
prevent duplicate names for literals in Substrait plans. This approach
had several drawbacks:
- Non-deterministic plan names that made testing difficult (requiring
UUID regex filters)
- Only addressed literal naming conflicts, not the broader issue of name
deduplication
- Added unnecessary dependency on the `uuid` crate
- Didn't properly handle cases where the same qualified name could
appear with different schema representations

  1. Enhanced NameTracker: Refactored to detect two types of conflicts:
- Duplicate schema names: Tracked via schema_name() to prevent
validate_unique_names failures (e.g., two Utf8(NULL) literals)
- Ambiguous references: Tracked via qualified_name() to prevent
DFSchema::check_names failures when a qualified field (e.g.,
left.Utf8(NULL)) and unqualified field (e.g., Utf8(NULL)) share the same
column name
2. **Removed UUID dependency**: Eliminated the `uuid` crate from
`datafusion/substrait`
3. **Removed literal-specific aliasing**: The UUID-based workaround in
`project_rel.rs` is no longer needed as the improved NameTracker handles
all naming conflicts consistently
4. **Deterministic naming**: Name conflicts now use predictable
`__temp__N` suffixes instead of random UUIDs

Note: This doesn't fully fix all the issues in apache#17508 which allow some
special casing of `CAST` which are not included here.

Yes:
- Updated snapshot tests to reflect the new deterministic naming (e.g.,
`Utf8("people")__temp__0` instead of UUID-based names)
- Modified some roundtrip tests to verify semantic equivalence (schema
matching and execution) rather than exact string matching, which is more
robust
- All existing integration tests pass with the new naming scheme

Minimal. The generated plan names are now deterministic and more
readable (using `__temp__N` suffixes instead of UUIDs), but this is
primarily an internal representation change. The functional behavior and
query results remain unchanged.

(cherry picked from commit d59cdfe)

Co-authored-by: Xander <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

substrait Changes to the substrait crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve substrait NameTracker so it doesn't require uuids

5 participants