Skip to content

fix(optimizer): Quotation issue in canonicalize_internal_names#7795

Closed
fivetran-kwoodbeck wants to merge 1 commit into
mainfrom
optimizer/bug-canonicalize-internal-names-alias-collision
Closed

fix(optimizer): Quotation issue in canonicalize_internal_names#7795
fivetran-kwoodbeck wants to merge 1 commit into
mainfrom
optimizer/bug-canonicalize-internal-names-alias-collision

Conversation

@fivetran-kwoodbeck

@fivetran-kwoodbeck fivetran-kwoodbeck commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

canonicalize_internal_names was quoting unqualified base column references when its name happened to match the top level output alias. This produced inconsistent identifier quoting in the canonical output. This violates the rule that base-table column references are part of the data contract and must be preserved verbatim.

The fix narrows the rewrite so it only fires when the alias is actually being renamed.

Query:

SELECT a AS a, b AS c, d AS renamed FROM cat.db.t WHERE a > b AND b > 0 AND d < 0
Stage Result
Before SELECT "a" AS a, b AS c, d AS renamed FROM cat.db.t AS "_t0" WHERE "a" > b AND b > 0 AND d < 0
After SELECT a AS a, b AS c, d AS renamed FROM cat.db.t AS "_t0" WHERE a > b AND b > 0 AND d < 0

@github-actions

Copy link
Copy Markdown
Contributor

SQLGlot Integration Test Results

✅ All tests passed

Comparing:

  • this branch (sqlglot:optimizer/bug-canonicalize-internal-names-alias-collision @ sqlglot efe3c32)
  • baseline (main @ sqlglot 17f7870)

Overall

main: 192428 total, 153523 passed (pass rate: 79.8%)

sqlglot:optimizer/bug-canonicalize-internal-names-alias-collision: 180234 total, 142378 passed (pass rate: 79.0%)

Transitions:
No change

Dialect pair changes: 0 previous results not found, 3 current results not found

✅ All tests passed

@georgesittas

Copy link
Copy Markdown
Collaborator

@fivetran-kwoodbeck how did you test this? I don't see any issue:

>>> from sqlglot import parse_one
>>> from sqlglot.optimizer.qualify import qualify
>>> from sqlglot.optimizer.canonicalize_internal_names import canonicalize_internal_names
>>>
>>> sql = "SELECT a AS a, b AS c, d AS renamed FROM cat.db.t WHERE a > b AND b > 0 AND d < 0"
>>> ast = parse_one(sql)
>>> qualified = qualify(ast, schema={"cat": {"db": {"t": {"a": "int", "b": "int", "d": "int"}}}})
>>> canonicalized = canonicalize_internal_names(qualified)
>>>
>>> canonicalized.sql()
'SELECT "_t0"."a" AS "a", "_t0"."b" AS "c", "_t0"."d" AS "renamed" FROM "cat"."db"."t" AS "_t0" WHERE "_t0"."a" > "_t0"."b" AND "_t0"."b" > 0 AND "_t0"."d" < 0'

@georgesittas georgesittas deleted the optimizer/bug-canonicalize-internal-names-alias-collision branch June 25, 2026 14:56
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.

2 participants