Repository navigation
fix(dialect): treat a CTE with no main statement as a degraded parse (#698) - #701
Merged
Merged
Conversation
macbre
enabled auto-merge (squash)
October 15, 2026 10:39
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
Fixes #698 — valid multi-CTE queries that use a map/array subscript such as
m['key']orARRAY_JOIN(m['key'], ' ')raiseInvalidQueryDefinition("WITH clause without a main statement is not valid SQL").Root cause
When the SQL contains a
[(map/array element access),DialectParser._detect_dialectsselects the TSQL
BracketedTableDialectfirst. Under that dialect sqlglot's WARN-modeparse misreads the
[as a bracket-quoted identifier and silently drops the trailingmain
SELECT, leaving a bare top-levelexp.Withnode.DialectParser._is_degradedonly checked for an opaqueexp.Commandor badtable/column names, so this broken parse was accepted as the winning result — and the
missing main statement surfaced later as
InvalidQueryDefinitionwhen.tables/.columnswere accessed.
Change
DialectParser._is_degradednow also returnsTruewhen the root AST node is atop-level
exp.With(a CTE with no main statement). A complete query always parses toits main statement (
Select/Insert/…) with the CTE attached as awithargument, so abare top-level
Withis unambiguously a degraded result._try_dialectsthen fallsthrough to the next candidate dialect, which parses the query correctly.
Verification
test_with_map_subscript_in_cte_does_not_drop_main_statement)covering
ARRAY_JOIN(m['k'], ' '), a plainm['k']subscript, and anarr[1]subscriptinside a non-first CTE. All now return the correct tables instead of raising.
270 passed, with the only 4 failures being pre-existing and unrelated(they fail identically on the unmodified code —
test_comments.py/test_query_type.pyDROP/DELETE-with-comment cases, caused by sqlglot version drift, not this change).