phpmyadmin / phpmyadmin/sql-parser

CTE parsing fails when name is a non-reserved keyword

オープン
#662 コメント 0 件 リアクション 2 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
PHP
スター
485
フォーク
119
PR マージ指標
30日以内にマージされた PR はありません

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、2つの vendor/bin/sql-parser --lint --query 再現ケースを実行し、WITH の後の CTE 名を lexer と parser がどのように処理するかを、バッククォートで引用された名前も含めて追跡します。両方のクエリが、予約されていないキーワード名と引用符で囲まれたキーワード名を含め、報告されたエラーなしに正常にパースできれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
mysql, php, sql
領域
compilers, databases
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
静か
明瞭さ
おおむね明確
初心者へのやさしさ
65/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。