Skip to content

fix(syntax): name VALUES and UNPIVOT items by their alias in query scopes - #275

Merged
kvlonge merged 1 commit into
mainfrom
fix/values-alias-located
Nov 24, 2026
Merged

kvlonge merged 1 commit into
mainfrom
fix/values-alias-located

Conversation

@kvlonge

@kvlonge kvlonge commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Why

Expression::query_scopes left two FROM items out of their query's relations, because their alias sits on their own node:

  • a VALUES list written without parentheses: FROM VALUES (1), (2) AS v(a)
  • an aliased UNPIVOT: FROM orders UNPIVOT (val FOR k IN (a, b)) AS u

So qualified_columns reported v.a and u.k as reading a relation that is not in sight. SQLClean 0.12.0, which runs its relation Rules on these scopes, reports such references as SQLCLEAN029 findings on valid SQL. The parenthesised FROM (VALUES ...) AS v(a) was found, because it is a derived table.

Changes

  • relation() in the scope builder reads the alias of Expression::Values and Expression::Unpivot.
  • Scope cases for VALUES with and without parentheses, joined, without AS, with a non-matching qualifier, and for UNPIVOT.

query_scopes is used only by downstream Rules, not by conversion, so no converted output changes.

Verification

  • cargo test -p sqlquack-parse --lib given_qualified_columns_when_scoped: the new cases fail without the change and pass with it.
  • make check-ci.
  • SQLClean's rule tests against this commit (companion pull request in sqlclean).

…opes

A VALUES list written without parentheses, as in
FROM VALUES (1), (2) AS v(a), and an aliased UNPIVOT carry their alias on
their own node. The scope builder read aliases only from tables, derived
tables, aliases, joined tables and PIVOT, so neither item was a relation
of its query, and a reference such as v.a looked like one to a relation
out of sight. In parentheses the list is a derived table and was found.
@kvlonge
kvlonge added this pull request to the merge queue Nov 24, 2026
Merged via the queue into main with commit e78032e Nov 24, 2026
10 checks passed
@kvlonge
kvlonge deleted the fix/values-alias-located branch November 24, 2026 17:28
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.

1 participant