parser: support common table expression (WITH clause) - #23
Open
LordofAvernus wants to merge 1 commit into
Open
Conversation
MySQL 8.0 CTE statements (`WITH ... AS (...) SELECT/INSERT/UPDATE/DELETE`) previously failed to parse, so downstream consumers could not build an AST for them and had to fall back to treating such statements as unknown. Add `ast.WithClause` / `ast.CommonTableExpression` and attach an optional `With` field to SelectStmt, InsertStmt, UpdateStmt and DeleteStmt, with Restore and Accept support so the clause round-trips. Grammar-wise each DML rule is split into a `*NoWith` variant that the `WithClause`-prefixed rule reuses, and `RECURSIVE` is introduced as a reserved keyword, matching MySQL 8.0 and upstream TiDB. Co-authored-by: Cursor <cursoragent@cursor.com>
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.
What problem does this PR solve?
WITH ... AS (...) SELECT/INSERT/UPDATE/DELETE(MySQL 8.0 CTE) fails to parse on thecurrent
release-4.0.2, e.g.:Downstream consumers (SQLE) therefore cannot build an AST for CTE statements and end up
misclassifying them (e.g. a CTE + SELECT being reported as DDL instead of DQL).
What is changed and how it works
ast: addCommonTableExpressionandWithClause(IsRecursive,CTEs), withRestoreandAcceptimplemented so the clause round-trips.ast: add an optionalWith *WithClausefield toSelectStmt,InsertStmt,UpdateStmtandDeleteStmt;Restore/Acceptof each statement handle it whennon-nil, so behaviour is unchanged for statements without a
WITHclause.parser.y: split each DML rule into a*NoWithvariant and let theWithClause-prefixed rule reuse it; addWithClause,WithList,CommonTableExpr,IdentList,IdentListWithParenOpt.misc.go/parser.y: add theRECURSIVEtoken.parser.go: regenerated fromparser.y.Compatibility note
RECURSIVEis introduced as a reserved keyword, matching MySQL 8.0 and upstreamTiDB's parser. As a consequence
recursivecan no longer be used as a bare identifier:If keeping
recursiveusable as an identifier matters more than matching MySQL 8.0behaviour here, please say so and I will rework it as an unreserved keyword.
Tests
cte_test.gocoveringSELECT/INSERT/UPDATE/DELETEwith aWITHclause,column name lists, multiple CTEs,
WITH RECURSIVE, invalid inputs, plus AST-levelassertions. Restore round-trip is verified through the existing
RunTesthelper.go test ./...) shows no new failures: the 3 pre-existing failures(
TestKeywordConsistent,TestscanString,TestDMLStmt) and theastvet failureare identical on
b6199b7without this change.Made with Cursor