Skip to content

Conversation

@georgesittas
Copy link
Collaborator

@georgesittas georgesittas commented Nov 20, 2025

This PR introduces a new qualify flag: canonicalize_table_aliases. This flag controls whether the qualify_tables rule should use canonical aliases (_0, _1, ...) for all sources instead of preserving table names.

@georgesittas georgesittas force-pushed the jo/canonicalize_table_aliases branch from b809a2b to 5f99cef Compare November 20, 2025 13:30
@georgesittas georgesittas force-pushed the jo/canonicalize_table_aliases branch 2 times, most recently from 5788d3a to 8060cc4 Compare November 21, 2025 14:52
@georgesittas georgesittas changed the title Feat(optimizer): canonicalize table aliases Feat(optimizer)!: canonicalize table aliases Nov 21, 2025
@georgesittas georgesittas force-pushed the jo/canonicalize_table_aliases branch from e7e967a to f89a683 Compare November 21, 2025 18:33
@georgesittas
Copy link
Collaborator Author

This should be good to go. I'll see if I can increase the testing coverage a bit and then get it in.

@georgesittas georgesittas force-pushed the jo/canonicalize_table_aliases branch from f89a683 to ebb4aa2 Compare November 24, 2025 11:41
@georgesittas
Copy link
Collaborator Author

Found a bug with correlated subqueries, i.e. when we have scopes w/ external columns. We need to respect lexical scoping in the canonical alias mapping probably. Going to fix and improve coverage.

exp.alias_(table, table.name, copy=False, table=True)
_set_alias(table, canonical_aliases, target_alias=table.name)

for column in local_columns:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note this change vs how we previously iterated over scope.columns. If we visited external columns in this loop, we would mutate their source too early, without having had the chance to alias outer scopes' sources.

Before making this change, the test case I added in qualify_tables.sql failed due to having columns qualified with "".

Comment on lines +105 to +106
local_columns = scope.local_columns
canonical_aliases: t.Dict[str, str] = {}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Auto-generated alias mappings are constructed per-scope to respect lexical scoping.

@github-actions

This comment was marked as resolved.

@github-actions

This comment was marked as resolved.

@georgesittas georgesittas merged commit 95727f6 into main Nov 24, 2025
8 checks passed
@georgesittas georgesittas deleted the jo/canonicalize_table_aliases branch November 24, 2025 18:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants