parser: reject DISTINCT ON instead of fabricating an ON() call#54
Merged
Conversation
`SELECT DISTINCT ON (a) a, b FROM t` was silently mis-parsed: after consuming DISTINCT the select-list parser read `ON (a)` as a function call `ON(a)` occupying the first output slot, then took the intended first column `a` as that call's alias -- dropping a projected column and turning the query into `SELECT DISTINCT ON(a) AS a, b`. DISTINCT ON (a PostgreSQL extension) is not supported. Reject it cleanly right after DISTINCT (error, no AST) rather than producing the wrong tree. Plain DISTINCT (no ON) is unaffected. Tests (test_parser_negative): `DISTINCT ON (...)` is rejected; plain `DISTINCT a, b` still parses. Full suite 37/37. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FyKDAbMWGwiZq4iJx3imsg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Second-pass audit finding F3 (dialect gap, silently-wrong-AST).
SELECT DISTINCT ON (a) a, b FROM twas silently mis-parsed: after consumingDISTINCT, the select-list parser readON (a)as a function callON(a)occupying the first output slot, then took the intended first columnaas that call's alias — dropping a projected column and turning the query intoSELECT DISTINCT ON(a) AS a, b.DISTINCT ON(a PostgreSQL extension) is not supported. Reject it cleanly right afterDISTINCT(error, no AST) rather than producing the wrong tree. PlainDISTINCT(noON) is unaffected.Full
DISTINCT ONsupport (distinct-on expression list + its analyzer/binder legs) is a separate feature, out of scope — this removes the silent mis-parse.Tests (
test_parser_negative)DISTINCT ON (...)is rejected; plainDISTINCT a, bstill parses. Full suite 37/37.