Conversation
Some dialects like PostgreSQL and DB2 allow reserved words to be used as aliases. This crashed the parser, as reserved words were not valid identifier names in the grammar. Fixes sql-formatter-org#801
Look behind at already-rewritten tokens so UPDATE set SET and INSERT INTO set VALUES still start a clause. Only treat SET as an alias after AS, so CREATE TABLE AS WITH and PREPARE AS UPDATE keep working.
cpruijsen
force-pushed
the
fix/issue-801
branch
from
September 14, 2026 14:34
f964516 to
bbed5cc
Compare
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.
A reserved word used as an alias crashes the formatter.
SELECT a AS set FROM tand the same withvaluesare the reported cases: the token is classified as the start of aSETorVALUESclause, aclause appears where the parser does not expect one, and formatting throws.
disambiguateTokensinsrc/lexer/disambiguateTokens.tsreclassified keywords by looking atimmediate neighbours, one token either side. That is enough for the cases it already handled, a
property name after a dot or a function name before a paren, and not enough here: whether
SETstartsa clause depends on where you are in the statement, not on what is next to it.
The new pass tracks that context. It follows the last clause-starting token, knows when an
UPDATE/INSERT/REPLACE/ALTERis still expecting its target object name, which is the positionwhere a following
SETorVALUESgenuinely starts a clause, and treats a reserved word afterASas an alias. Paren depth is tracked separately, so
WITH x AS (UPDATE t SET y = 1) SELECT ...keepsthe inner context distinct from the outer one.
That is why the diff is larger than the bug suggests: the existing passes are neighbour-local by
construction, so there was nowhere to add a positional rule without introducing the notion of
position. The five existing passes are unchanged and still run.
Tests cover both reported words as aliases, the clause forms that must keep working, and the nested
case.
Fixes #801