Skip to content

Preserve MySQL operator semantics and executable-comment provenance - #72

Draft
renecannao wants to merge 5 commits into
mainfrom
fix/mysql-analytical-ast-admission
Draft

renecannao wants to merge 5 commits into
mainfrom
fix/mysql-analytical-ast-admission

Conversation

@renecannao

@renecannao renecannao commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

MySQL queries can currently produce complete ASTs for different semantics: SELECT a <=> b emits a > b, SELECT a && b emits a & b, and SELECT 1 /*! + 1 */ emits SELECT 1 although MySQL returns 2. These cases block safe AST-based analytical query admission in sysown/proxysql#6284.

This change preserves the two operators and their precedence, exposes executable-comment provenance on parse results, and makes executable-comment-only batch fragments explicit errors instead of discarding them. Ordinary comments remain ignored. It also represents MySQL SELECT modifiers as explicit options and conservatively refuses unsupported temporal, national-string and BINARY expression forms that previously became misleading aliases; statement classification is retained for fallback.

Projection items now also retain their original expression span without aliases or trailing trivia, and ParseResult exposes ordinary-comment provenance. Tokenizer consumption boundaries distinguish consumed tokens from lookahead. These support MySQL output-label preservation without a downstream lexer.

Two further commits complete the set ProxySQL carries as local patches:

  • Bounded recursion (fix: bound recursive parser depth): expression and query parsing share one active-depth counter through the tokenizer, with an RAII guard so every failure path restores it. ParserConfig::max_parse_depth (default and hard maximum 128) lowers the limit; exceeding it returns ParseResult::ERROR, full_input=false and "SQL parse depth limit exceeded". The parser stays reusable and parse_all recovers at the next statement. This bounds the expression/query recursion paths, not every unrelated DDL grammar.

  • UTF-8 identifiers (fix: tokenize UTF-8 identifiers as single tokens): well-formed UTF-8 scalars (no overlongs, surrogates or values above U+10FFFF) are identifier characters in both dialects, malformed or truncated sequences give TK_ERROR without over-reading, and PostgreSQL integer parsing treats an adjacent byte >= 0x80 as trailing junk (FETCH 1é remains invalid, as in PostgreSQL). Without the last part the existing PgSessionCommands.RejectsMalformedCommands test fails.

  • PostgreSQL compatibility baselines (test: refresh PostgreSQL compatibility baselines for UTF-8 identifiers): the first CI run of the two commits above failed pg-compat because 19 replayed cases (22 corpus statements) from PostgreSQL's own multibyte tests (src/test/mb/sql/utf8.sql, Japanese table/column names) were recorded as ERROR/PARTIAL/TRAILING_INPUT and now correctly parse as DEEP_SUPPORTED, matching the libpg_query oracle. Regenerated with make pg-compat-refresh: DEEP_SUPPORTED 49,848 -> 49,870, PARTIAL 52 -> 49, ERROR 1,041 -> 1,039, TRAILING_INPUT 377 -> 360, nothing moved the other way, and the committed-case replay passes (5,102 cases).

Validation (local, clean build of head 7018f12, before the baseline refresh): 1,698 parser/engine GoogleTests passed, excluding the 8 test files that need Oracle libmysqlclient/libpq development headers (test_mysql_executor, test_pgsql_executor, test_session, test_single_backend_txn and the four test_distributed_*), which this environment lacks; CI covers them. Downstream, ProxySQL applies this branch's changes to the unchanged 1.1.1 archive (identical to base c7adfcb) as two patches and the resulting tree is byte-identical to this branch at 7018f12 for every tracked file (the later baseline commit only adds regenerated compatibility data). The earlier commits passed upstream compiler, macOS, corpus and MySQL/PostgreSQL compatibility CI; On the previous push, CI passed g++/clang++ builds on Ubuntu 22.04/24.04, macOS, corpus, MySQL compatibility and benchmark; pg-compat failed for the reason above and is fixed by the baseline commit (CI pending). Four nesting patterns that previously crashed were independently checked with 512 KiB and 1 MiB stacks. No release tag or ProxySQL dependency update is included.

@coderabbitai

coderabbitai Bot commented Nov 16, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Expression and query parsing recurse without a limit, so deeply nested
parentheses, NOT chains, function calls or scalar subqueries could exhaust
the stack of a caller with a small stack.

Share one active-depth counter through the tokenizer across expression and
query entry points, guarded by an RAII object so every failure path restores
it. ParserConfig::max_parse_depth (default and hard maximum 128) lowers the
limit. Exceeding it yields ParseResult::ERROR with full_input=false and the
message "SQL parse depth limit exceeded"; the parser stays reusable and
parse_all recovers at the next statement.

Tests cover both dialects at depths 16 and 512 for four nesting forms,
configured limits including 0 and values above the cap, batch recovery and
long flat lists.
Bytes >= 0x80 were not identifier characters, so a name such as "éDROP"
split into an error token followed by an ASCII keyword. Accept well-formed
UTF-8 scalars (rejecting overlongs, surrogates and values above U+10FFFF)
inside identifiers for both dialects, and emit TK_ERROR for malformed or
truncated sequences without reading past the input.

Because "é" is now an identifier start, PostgreSQL integer literal parsing
must also treat a directly adjacent byte >= 0x80 as trailing junk, as it
already does for ASCII letters ("FETCH 1é" stays invalid, matching
PostgreSQL's "trailing junk after numeric literal").

Reported downstream in ProxySQL #6630.
PostgreSQL's own multibyte regression tests (src/test/mb/sql/utf8.sql) use
Japanese table and column names. They were recorded as ERROR, PARTIAL or
TRAILING_INPUT because non-ASCII identifiers were not tokenized; with the
UTF-8 identifier fix 22 corpus statements now parse as DEEP_SUPPORTED, in
line with the libpg_query oracle accepting them (49,848 -> 49,870 supported;
PARTIAL 52 -> 49, ERROR 1041 -> 1039, TRAILING_INPUT 377 -> 360). No result
moved in the other direction.

Regenerated with `make pg-compat-refresh` (corpus source paths normalized to
the committed /tmp/parsersql-coverage prefix). The replayed CI case set shrinks
from 5122 to 5102 because the fixed statements are no longer non-supported.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant