CTE parsing fails when name is a non-reserved keyword
還沒有人認領這個 Issue。
評估
- 難度
- 3/5
- 預估耗時
- 1-2 天
- 新手友好度
- 65/100
研究方向
先執行兩個 vendor/bin/sql-parser --lint --query 重現案例,並追蹤 lexer 和 parser 如何處理 WITH 後的 CTE 名稱,包括以反引號引用的名稱。完成的判定標準是兩個查詢都能成功剖析,包括非保留關鍵字名稱和加引號的關鍵字名稱,且不出現回報的錯誤。
由索引模型根據 Issue 內容生成。
描述
The parser rejects valid CTEs whose names are non-reserved keywords, throwing:
The name of the CTE was expected.
MySQL has a large set of keywords that are explicitly not reserved (no (R) marker in the official MySQL 9.7 keyword list^1). Non-reserved keywords are explicitly permitted as unquoted identifiers, including CTE names. MySQL executes these queries without error^2.
Reproducer
Unquoted:
vendor/bin/sql-parser --lint --query "WITH data AS (SELECT 1) SELECT * FROM data"
#1: The name of the CTE was expected. (near "data" at position 5)
#2: Unexpected end of the WITH CTE. (near "data" at position 5)
#3: Unrecognized statement type. (near "data" at position 5)
Backtick-quoted (which should unconditionally allow any keyword as an identifier):
vendor/bin/sql-parser --lint --query "WITH `data` AS (SELECT 1) SELECT * FROM `data`"
#1: The name of the CTE was expected. (near "`data`" at position 5)
#2: Unexpected end of the WITH CTE. (near "`data`" at position 5)
#3: Unexpected beginning of statement. (near "`data`" at position 5)
#4: Unrecognized statement type. (near "AS" at position 12)
Expected behaviour
The query parses successfully for any non-reserved keyword used as a CTE name, whether unquoted or backtick-quoted.
Actual behaviour
Parser throws errors in both cases. The root cause appears to be that the lexer classifies all keywords as keyword tokens unconditionally, rather than resolving them contextually. After WITH, the parser expects an identifier token; receiving a keyword token instead, it fails, even though MySQL allows non-reserved keywords in identifier position without quoting, and allows any keyword in identifier position when backtick-quoted.
- 主要語言
- PHP
- 星號
- 485
- 分支
- 119
- PR 合併指標
- 30 天內沒有已合併 PR
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
phpmyadmin/sql-parser 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 68/100
phpmyadmin/sql-parser#655 · 1 個 reaction ·
-
難度 3/5 1-2 天 新手友好度 55/100
phpmyadmin/sql-parser#666 ·
-
kind/support
難度 3/5 1-2 天 新手友好度 45/100
phpmyadmin/sql-parser#653 · 1 個 reaction ·
-
難度 3/5 1-2 天 新手友好度 70/100
phpmyadmin/sql-parser#649 ·
-
bug
難度 3/5 1-2 天 新手友好度 25/100
phpmyadmin/sql-parser#643 · 1 則留言 ·
查看 phpmyadmin/sql-parser 的全部 Issue
相似的 Issue
-
sync-en
難度 1/5 1-3 小時 新手友好度 85/100
-
難度 1/5 1 小時以內 新手友好度 90/100
-
難度 2/5 1-3 小時 新手友好度 68/100
getgrav/grav-plugin-api#45 ·
-
難度 2/5 1-3 小時 新手友好度 78/100
RSS-Bridge/rss-bridge#5098 ·
-
難度 2/5 1-3 小時 新手友好度 68/100
phingofficial/phing#2025 ·