CTE parsing fails when name is a non-reserved keyword

未關閉
#662 0 則留言 2 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
65/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
冷清
技術堆疊
mysql, php, sql

研究方向

先執行兩個 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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

phpmyadmin/sql-parser 的其他 Issue

查看 phpmyadmin/sql-parser 的全部 Issue

相似的 Issue

更多 PHP Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。