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
-
sync-en
难度 1/5 1-3 小时 新手友好度 85/100
-
Перевод устарел
难度 1/5 1-3 小时 新手友好度 78/100
-
难度 1/5 1 小时以内 新手友好度 90/100
-
难度 2/5 1-3 小时 新手友好度 85/100