Skip to content

fix(rules): read unparenthesised VALUES and UNPIVOT aliases as relations - #100

Open
kvlonge wants to merge 4 commits into
mainfrom
fix/values-alias-scope
Open

kvlonge wants to merge 4 commits into
mainfrom
fix/values-alias-scope

Conversation

@kvlonge

@kvlonge kvlonge commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Why

SQLCLEAN029 flagged v.a in SELECT v.a FROM VALUES (1), (2) AS v(a) and in an aliased UNPIVOT (FROM orders UNPIVOT (...) AS u, read as u.k) as a relation absent from the query, on every dialect that parses them. SQLQuack's query scopes left those items' aliases out (fixed in sqlstack-io/sqlquack#275); with them in, SQLCLEAN023 would have offered broken fixes, such as removing AS v(a).

Changes

  • Pins SQLQuack at the commit of fix(syntax): name VALUES and UNPIVOT items by their alias in query scopes sqlquack#275, which registers VALUES and UNPIVOT aliases in query scopes. To be re-pinned to the SQLQuack 0.23.1 release commit before merging.
  • SQLCLEAN023 skips bare VALUES and UNPIVOT items, so it behaves as in 0.12.0 (not every dialect's tree records where a bare VALUES alias is written).
  • 12 test cases for SQLCLEAN029 and SQLCLEAN023 on every dialect that accepts each form.
  • SQLCLEAN009 now also finds a duplicate bare-VALUES alias on Databricks.

Verification

  • make check-ci passed (fensu, docs skill, fmt, clippy).
  • cargo test -p sqlclean-lint -p sqlclean-rules (lint corpus included) and cargo test -p sqlclean --lib sql_lint passed against the SQLQuack fix.
  • The unused-join, qualification, duplicate-alias and unused-alias Rules compared before and after: the only other change is the SQLCLEAN009 finding above.

SQLCLEAN029 reported v.a in FROM VALUES (1), (2) AS v(a), and u.k for an
UNPIVOT aliased u, as qualifiers naming no relation in sight. The query
scopes now name these items by their alias. SQLCLEAN023 keeps its earlier
findings for them: a bare VALUES alias is not reported, and an UNPIVOT
alias is still kept without a fix.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Deploying sqlclean with  Cloudflare Pages  Cloudflare Pages

Latest commit: 907322e
Status: ✅  Deploy successful!
Preview URL: https://63716342.sqlclean.pages.dev
Branch Preview URL: https://fix-values-alias-scope.sqlclean.pages.dev

View logs

…s unflagged by SQLCLEAN027

SQLCLEAN027 reported three kinds of unqualified names that no relation
alias can or should qualify:

- a column merged by USING, such as k in FROM a FULL JOIN b USING (k),
  where a.k or b.k would read one side instead of the merged key;
- the bare date part of LAST_DAY(d, month), whose position the date part
  table did not know (it only knew first arguments);
- a macro parameter placeholder, which stands for whatever the caller
  passes.

The lint corpus loses 027 findings on USING keys only.
SQLCLEAN016 compared an alias with its column by the dialect's resolved
name, so on Snowflake ENTITY_ID AS entity_id was flagged although the
alias changes the output column's name for any tool that names columns
as written. The Rule now flags only an alias that copies its column's
name exactly: same spelling, same case and same quoting, on every
dialect (amount AS amount, "Bar" AS "Bar", t.amount AS amount). An
alias that changes only the case or the quoting is left alone.

PostgreSQL's tree folds unquoted names as it parses them, so there a
case change between unquoted names still reads as a copy; a test pins
this until the tree keeps names as written.

The message reads "Expression is aliased to an exact copy of its name".
The lint corpus loses 12 SQLCLEAN016 findings, each a case or quoting
change; every other changed line differs only in its message hash.

This branch has not been deployed

No deployments
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