Skip to content

fix(parser): support unquoted non-ASCII identifiers - #27010

Merged
mergify[bot] merged 5 commits into
matrixorigin:mainfrom
iamlinjunhong:m2-26715
Aug 14, 2026
Merged

mergify[bot] merged 5 commits into
matrixorigin:mainfrom
iamlinjunhong:m2-26715

Conversation

@iamlinjunhong

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #26715

What this PR does / why we need it:

fix(parser): support unquoted non-ASCII identifiers

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Aug 12, 2026

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Preserve raw client bytes when a digit-leading token is promoted to an identifier.

This change explicitly supports both raw high-byte client identifiers (for example t_\xe9g) and digit-leading extended identifiers, but their combination takes a different path. In pkg/sql/parsers/dialect/mysql/scanner.go:813-820, scanNumber promotes the token to ID and then returns strings.ToLower(s.buf[start:s.Pos]). Go's Unicode lowercasing replaces an invalid UTF-8 byte such as the latin1 0xe9 with U+FFFD.

I reproduced this through the public parser at head 65f88bcf1d0388e299fd2ac85d8bf6438697fd13:

stmt, _ := ParseOne(ctx, "CREATE TABLE 1\xe9 (a INT)", 1)
tree.String(stmt, dialect.MYSQL)
// actual:   "create table 1� (a int)"
// expected: "create table 1\xe9 (a int)"

This silently addresses a different catalog object and can collapse distinct byte identifiers. Please keep the identifier bytes intact (or use byte-preserving ASCII-only folding if normalization is required), and add both scanner-level and ParseOne regressions for the pairwise case.

There is a second compatibility boundary to close: isUnquotedIdentifierLetter accepts every byte >= 0x80, so a valid UTF-8 supplementary character such as U+1F600 is accepted as an unquoted ID. MySQL explicitly limits identifiers to the BMP and prohibits U+10000 and above in quoted and unquoted identifiers: https://dev.mysql.com/doc/refman/8.0/en/identifiers.html. I confirmed CREATE TABLE 😀 (a INT) parses successfully on this head. The validation needs to be charset-aware (or otherwise distinguish the supported raw-byte contract) so the fix does not broaden the grammar beyond the compatibility contract.

@aptend aptend left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep-reviewed exact head 65f88bcf1d0388e299fd2ac85d8bf6438697fd13, including the existing same-head feedback. Two compatibility blockers remain:

  1. [P1] Preserve raw client bytes when a digit-leading token is promoted to ID. scanNumber now accepts a following extended identifier byte, but still returns strings.ToLower(s.buf[start:s.Pos]). Go Unicode lowercasing rewrites invalid UTF-8 bytes. I reproduced CREATE TABLE 1\xe9 (a INT) through ParseOne: deparse returns create table 1� (a int) rather than preserving byte 0xe9, so the parsed catalog name changes. Use byte-preserving ASCII-only folding (or return the original identifier bytes) and cover this combined digit-leading/raw-byte path at scanner and parser levels.

  2. [P1] Do not accept supplementary Unicode code points as MySQL identifiers. isUnquotedIdentifierLetter accepts every byte >= 0x80 independently, so UTF-8 for U+10000 and above is accepted as an unquoted identifier. I independently confirmed CREATE TABLE 😀 (a INT) parses successfully. MySQL restricts identifiers to the BMP; the validation boundary must distinguish supported raw client bytes from valid UTF-8 supplementary sequences instead of broadening the grammar to all high-byte sequences.

The two focused counterexamples fail on this exact head as described.

@iamlinjunhong

Copy link
Copy Markdown
Contributor Author

已将分支从精确旧 head 68ec2d9 rebase 到当前 canonical main 0fc87ec,并用精确 lease 推送;新 head 为 fb5cfe6,无冲突。

失败的 UT run 31711715051 / job 94486146456(attempt 1)完整日志显示失败命令为 make ut UT_PARALLEL=6 UT_TIMEOUT=40,唯一失败是 pkg/tests/features.TestTableFeatures;稳定错误签名是 HAKeeper 连接 ID 分配期间 connection reset by peer / broken pipe / context deadline exceeded。该 run 执行期间合入的 #27119(fix(logservice): prevent connection ID refill convoys)直接修复了同一调用链和 2 秒超时/共享 transport reset 问题,本次 rebase 已包含该修复。未 rerun 旧 head 的 run,push 已触发新 head CI。

本地验证(rebase 后):

  • parser/tree 与 mysql parser:go list、go build、go vet 通过
  • mo-cgo-test -v -count=1 -timeout=120s ./pkg/sql/parsers/tree ./pkg/sql/parsers/dialect/mysql 通过
  • mo-cgo-test -race -v -count=1 -timeout=120s -run ^TestTableFeatures$ ./pkg/tests/features 通过(22.305s)
  • git diff --check 通过,worktree clean

现有 CHANGES_REQUESTED 均基于旧 head;raw client bytes、digit-leading/raw-byte 组合、BMP/补充平面边界及 charset introducer 反馈已包含回归覆盖。请基于 fb5cfe6 重新 review。

@iamlinjunhong

Copy link
Copy Markdown
Contributor Author

@XuPeng-SH @aptend 重新核对当前 exact head fb5cfe62f12fb2f84f5f43dcb829fef4be820a7f:现有 4 条 CHANGES_REQUESTED 全部基于旧 head(65f88bcf、6bf6b244、2f9b4f87),当前没有 inline thread,也没有基于 fb5cfe62 的新 review。

旧反馈在当前实现中的闭环证据:

  • raw client byte / digit-leading:scanNumber 的各个 ID promotion 路径使用 byte-preserving toLowerASCII(scanner.go:789,804,840),scanner 与 public ParseOne 覆盖 1\xe9A。
  • supplementary Unicode:公共 scanIdentifier 入口调用 supplementaryUTF8SequenceSizeAt(scanner.go:844),正常入口及 0x/0b/@/@@/_utf8mb4 入口均有 scanner/public-parser 拒绝回归(scanner_test.go:150、mysql_sql_test.go:5130)。
  • _utf8mb4 前缀:isCharsetIntroducer 只在后续确为单引号字符串时成立(scanner.go:330);BMP/raw-byte/ASCII/数字后缀均保留完整 ID(scanner_test.go:116-120,mysql_sql_test.go:5104-5110),合法 introducer control 也保留(scanner_test.go:137,mysql_sql_test.go:5125)。

该 exact head 在 rebase 后已 fresh 通过 parser tree/mysql 的 list、build、vet、完整 deterministic tests,以及 TestTableFeatures race 回归;当前 exact-head check rollup 无 failure,CI 仍在运行。分支 mergeable、无冲突,worktree clean。烦请基于 fb5cfe62 重新 review 并更新旧 review 状态。

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep re-review completed on exact head fb5cfe6 (base and merge-base 0fc87ec). The previous raw-byte, supplementary-code-point, direct-entry, and charset-introducer blockers are closed. One PR-introduced correctness blocker remains.

[P1] Do not absorb invalid ASCII punctuation into an unquoted identifier when Unicode follows (pkg/sql/parsers/dialect/mysql/scanner.go:784-804, 813-840, 843-862).

scanNumber promotes the result to ID whenever the next position begins a supported Unicode identifier character, even after the numeric state machine has already consumed '.', or exponent '+'/'-'. Similarly, the 0x/0b fallback and @/@@ direct paths call scanIdentifier after punctuation, while scanIdentifier consumes its first byte unconditionally. Consequently the exact head accepts all of these malformed statements:

  • CREATE TABLE 1.数量 (a INT)
  • CREATE TABLE 1e+数量 (a INT)
  • CREATE TABLE 1e-数量 (a INT)
  • CREATE TABLE 0x.数量 (a INT)
  • CREATE TABLE 0b+数量 (a INT)
  • SELECT @+数量
  • SELECT @@-数量

The ASCII '.', '+', and '-' are not legal unquoted-identifier characters. The same public ParseOne regression test passes on the exact base (all statements are rejected) and fails on the exact head (all are accepted), proving the behavior is introduced by this PR.

Please gate ID promotion on the complete consumed prefix being identifier-legal, and validate the first byte before every direct scanIdentifier entry. Preserve valid controls such as 1数量 and 1e数量, and add scanner plus public ParseOne regressions for the malformed boundaries above.

Validation on the exact head: git diff --check; GOWORK=off go list/build/vet -mod=readonly for parser tree/mysql; full deterministic tests for ./pkg/sql/parsers/tree and ./pkg/sql/parsers/dialect/mysql. All existing checks pass; the temporary head/base counterexample was removed after verification.

@iamlinjunhong

Copy link
Copy Markdown
Contributor Author

已修复 XuPeng-SH 在 exact head fb5cfe62 的 review 4930469792,并 rebase 到当时最新 canonical main a6a6d0963f8a6af642504d19e6149c9d0ea60d43。新 head:5c2276dd727869218c7c8959c4ca3f7ca8d8be09。

修复闭环:

  • scanNumber 仅在已消费前缀全部可作为 unquoted identifier 组成部分时允许提升为 ID;含小数点或指数 +/- 的前缀保持 FLOAT,且不会吞掉后续 Unicode token。
  • scanIdentifier 统一校验首字节;0x/0b fallback 与 @/@@ 遇到非法 ./+/- 入口返回 LEX_ERROR。
  • 保留合法 controls:1数量、1e数量、raw client bytes,以及既有裸 0x/0b ID 行为。

新增 scanner + public ParseOne 回归覆盖 review 中七个反例,并在 scanner 层验证 FLOAT 的原值及下一完整 Unicode ID,防止仅改 token type 却仍错误推进位置。

最终 rebase 后验证均 exit 0:

  • GOWORK=off go list/build/vet -mod=readonly:parser tree/mysql
  • deterministic full tests:./pkg/sql/parsers/tree ./pkg/sql/parsers/dialect/mysql
  • focused -race:新边界、raw-byte、supplementary、裸 0x/0b
  • git diff --check,worktree clean

新 push 已触发 CI,未 rerun 旧 head run。烦请基于 5c2276dd72 重新 review。

@iamlinjunhong

Copy link
Copy Markdown
Contributor Author

@XuPeng-SH @aptend 当前 exact head 5c2276dd727869218c7c8959c4ca3f7ca8d8be09 复核结果:没有基于该 head 的新 review 或 inline thread;现存 CHANGES_REQUESTED 仍分别基于旧 head 65f88bcf / 2f9b4f87 / fb5cfe62。

其中最新 review 4930469792 的七个 punctuation + Unicode 反例已在当前 head 闭环:

  • scanNumber 以 canPromoteToIdentifier 阻止含 . 或 exponent +/- 的 numeric prefix 提升为 ID(scanner.go:767-842);
  • scanner 回归 TestUnquotedIdentifierRejectsPunctuationBeforeUnicode 同时断言 FLOAT 原值与下一完整 Unicode ID(scanner_test.go:208);
  • public parser 回归 TestUnquotedIdentifiersRejectPunctuationBeforeUnicode 覆盖七条 SQL(mysql_sql_test.go:5154);
  • 合法 1e数量 control 在 scanner 与 ParseOne 两层保留。

当前 fork ref 与 PR head 均为 5c2276dd72,PR mergeable、无冲突;exact-head checks 当前无 failure,剩余 CI 正在运行。修复与最终 rebase 后的 list/build/vet、两包全量 deterministic tests、focused race 证据见上一条评论。烦请基于当前 head 重新 review 并更新旧状态。

@aptend aptend left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep re-review completed on exact head 5c2276d, including all prior review rounds. The raw-client-byte, supplementary-plane, direct scanIdentifier entry, _utf8mb4 prefix, and punctuation-before-Unicode blockers are closed. The latest change gates numeric-to-ID promotion after decimal/exponent punctuation, validates every direct identifier entry byte, preserves legal 1数量 and 1e数量 controls, and keeps malformed statements split/rejected without consuming the following BMP token.

I inspected the full scanner/CStr boundary and reran the focused scanner plus public ParseOne matrix for BMP/raw bytes, charset introducers, supplementary rejection, 0x/0b/@/@@ direct paths, and all seven punctuation counterexamples. Both parser packages passed on this exact head. No blocking correctness issue remains.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep re-review on exact head 5c2276d. The earlier blockers are closed: valid supplementary-plane UTF-8 is rejected across the normal and direct scanner entry paths; malformed single-byte client-encoding bytes are preserved without RuneError replacement; _utf8mb4 is treated as a charset introducer only when followed by a string literal; and number/hex/bit/@ paths no longer absorb punctuation before a BMP identifier. Coverage exercises both scanner tokens and public ParseOne behavior. Verified full tree/mysql parser tests, 20x focused regressions, go vet, and git diff --check.

@mergify

mergify Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-14 04:09 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-08-14 07:42 UTC · at 1b0a1454b3bbf31a9b3348c8fc4c75b300f66791 · squash

This pull request spent 3 hours 32 minutes 48 seconds in the queue, including 57 minutes 26 seconds running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants