Repository navigation
Preserve MySQL operator semantics and executable-comment provenance - #72
Draft
renecannao wants to merge 5 commits into
Draft
renecannao wants to merge 5 commits into
renecannao wants to merge 5 commits into
Conversation
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
|
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MySQL queries can currently produce complete ASTs for different semantics:
SELECT a <=> bemitsa > b,SELECT a && bemitsa & b, andSELECT 1 /*! + 1 */emitsSELECT 1although 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 returnsParseResult::ERROR,full_input=falseand "SQL parse depth limit exceeded". The parser stays reusable andparse_allrecovers 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 giveTK_ERRORwithout 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 existingPgSessionCommands.RejectsMalformedCommandstest fails.PostgreSQL compatibility baselines (
test: refresh PostgreSQL compatibility baselines for UTF-8 identifiers): the first CI run of the two commits above failedpg-compatbecause 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 withmake 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_txnand the fourtest_distributed_*), which this environment lacks; CI covers them. Downstream, ProxySQL applies this branch's changes to the unchanged 1.1.1 archive (identical to basec7adfcb) 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-compatfailed 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.