Repository navigation
Make syntax error messages point at the mistake - #116806
Conversation
Both changes come from a survey of the error messages that ClickHouse
produces for damaged benchmark queries: the queries of ClickBench, TPC-H,
TPC-DS and the Join Order Benchmark were mutated by dropping tokens and
changing single letters, and the resulting 5560 errors were compared
against the position of the actual damage.
For an ordinary syntax error the reported position is excellent - the
median distance to the real mistake is 1 character. The one path that
localizes badly is `Unmatched parentheses`, with a median of 31 and a
90th percentile of 508 characters, because it reports the position of the
outermost bracket that is left unclosed. Bracket counting cannot tell
which of the nested brackets the user forgot to close: in
SELECT a FROM (SELECT count(* FROM t) x
the bracket of `count(` gets matched with the `)` that was meant to close
the subquery, so the leftover bracket is the one of the subquery. The
token where the parser stopped does not have that problem, and it was
simply thrown away. Report it, and list every unclosed bracket with its
own position instead of only printing the bracket characters. The median
distance to the mistake drops to 16 characters and the share of errors
reported within 5 characters of it grows from 13% to 33%.
A mistyped keyword, in turn, is answered with the whole list of
alternatives the parser could accept - 34 items in the median, and 151
for a typo in the leading `SELECT`, with no indication which one was
meant. Name the closest one:
SELEgT 1
Syntax error: failed at position 1 (SELEgT): SELEgT 1.
Maybe you meant: SELECT. Expected one of: ...
On the same corpus this fires for 392 errors, and in 98% of them the
named keyword is the one the original query had at that place. The
thresholds are tighter than in `NamePrompter` because the candidates are
keywords rather than names from the query, so a short word is close to
some keyword almost always: `hits` is two edits away from `WITH`.
|
Workflow [PR], commit [18cd5d4] Summary: ✅
AI ReviewSummaryThis PR materially improves human-readable syntax diagnostics by moving unmatched-bracket errors closer to the real mistake and by naming likely keyword typos. One important consumer is still out of sync, though: the structured Findings
Tests
Final VerdictChanges are not ready to merge yet: the new message-position contract is still broken for the WebAssembly / JSON parser API on unmatched-parentheses errors. LLVM Coverage ReportMeasured on commit 18cd5d4.
Changed lines: Changed C/C++ lines covered: 79/80 (98.75%) · Uncovered code |
`utils/wasm-parser` lists the `src/Common` translation units it needs one by one, so the keyword typo hint left `DB::levenshteinDistanceCaseInsensitive` undefined at link time. The file only needs `PODArray` and `iota`, which the build already has, and both configurations stay well under the size ceiling: 1342695 bytes (102305 under) with formatting and DCL, 811172 bytes (63828 under) without them.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes4 object files changed (+21.70 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units8 translation units recompiled, 11 s compile time in total, 8 of them have a recent master baseline. |
…g one
Applying the same measurement to DDL - the `CREATE TABLE` statements of
the same four benchmarks, damaged the same way, 2280 statements - showed
that the closing-bracket branch has the mirror image of the problem this
branch already fixes for openers.
A closing bracket that closes nothing can be excessive, and then it is
itself the mistake. But it can just as well be the *opening* bracket that
is missing:
CREATE TABLE hits WatchID BIGINT NOT NULL, ... 100 columns ... )
and then the leftover closing bracket is as far from the mistake as the
column list is long - 2643 characters in the worst case in the corpus.
The parser stops right where the opening bracket belongs (position 16 in
`CREATE TABLE t a UInt32, b UInt32)`, which is `a`), so prefer its
position whenever it is before the bracket, and keep naming the bracket.
On the DDL corpus the distance from the mistake improves from p75 32,
p90 184, max 2643 to p75 5, p90 14, max 34, and the share reported within
5 characters from 57% to 80%: 101 statements improved, 1 worse. On the
query corpus of the first commit it is a strict improvement - p75 88 to
54, p90 354 to 197, 82 improved and none worse - because a query rarely
loses the opening bracket of a long list.
`getUnmatchedParenthesesErrorMessage` appended the token where the parser
stopped to the list of positions to highlight even when that token was one of
the unmatched brackets itself, as in
CREATE TABLE t [a UInt32) ENGINE = Memory
where the parser stops at `[` while `checkUnmatchedParentheses` reports `[`
and `)`. `writeQueryWithHighlightedErrorPositions` requires strictly ascending
positions and computes `current_position_to_hilite - pos`, so a repeated
position made that difference negative and the query buffer was read out of
bounds. Deduplicate the positions after sorting them.
Addresses a review comment on the pull request. Adds highlighted cases to
`05045_syntax_error_messages_locate_the_mistake`; the client takes the
highlighting path with `--ignore-error`.
|
🕵 The two CI reds on @groeneai, investigate the failure: https://github.com/ClickHouse/ClickHouse/actions/runs/33284854699/job/99205760634 ( |
|
Already fixed by #117133 (merged 2026-08-30 16:06Z), so no separate PR is needed. The job you linked started 2026-08-30 04:19Z and its tree CIDB, last 35 days: 10 failures of |
… of the query Without highlighting (the normal server-side exception path) the excerpt after `failed at position N (end of query):` starts at the error position, so for `SELECT (1, 2` there was nothing left to show and the message degraded to a bare `: .`. When the parser stopped at `EndOfStream` or `;`, start the excerpt at the innermost bracket that is never closed instead: it is the one nearest to the mistake, and the excerpt then covers everything from that bracket to the end of the statement. Addresses the AI Review Major on `src/Parsers/parseQuery.cpp`. Co-Authored-By: Claude Fable 5.1 <[email protected]>
| diagnostics->error_token = scoped_parens[0]; | ||
| out_error_message = getUnmatchedParenthesesErrorMessage( | ||
| query_begin, statement_end, scoped_parens, hilite, query_description); | ||
| query_begin, statement_end, scoped_parens, last_token, hilite, query_description); |
There was a problem hiding this comment.
getUnmatchedParenthesesErrorMessage now derives a better error_token from last_token, but tryParseQuery still stores scoped_parens[0] in ParserDiagnostics. That breaks the documented "error_token is the token the error message points at" contract: utils/wasm-parser/wasm_parser.cpp serializes diagnostics.error_token into error.begin / error.end, so ch_parse("SELECT count(* FROM numbers(10)") will still point the browser/editor at the ( while the text message now says failed at position 16 (FROM).
Please drive diagnostics->error_token from the same selection as the message cursor here (or return that token from the formatter helper), and add a utils/wasm-parser/test.mjs case for unmatched parentheses so the JSON API cannot drift from the text error again.
Error messages for mistakes in SQL queries were surveyed by taking the queries of ClickBench, TPC-H, TPC-DS and the Join Order Benchmark (278 queries), damaging them at random - dropping a token, dropping a bracket or a comma, changing, dropping, inserting or transposing a single letter, swapping or duplicating a token, truncating the query - and comparing the reported error position with the position of the actual damage over the resulting 5560 errors.
For an ordinary syntax error the reported position turned out to be excellent: the median distance to the real mistake is 1 character, and 73% of the errors are reported within 5 characters of it. Two things stood out as bad, and this pull request fixes both.
Unmatched parentheses pointed at the wrong bracket. The median distance to the mistake was 31 characters, the 90th percentile 508, the maximum 3927 - by far the worst-localized syntax error. The reason is that the message reports the outermost bracket that is left unclosed, and bracket counting cannot tell which of the nested brackets the user actually forgot to close. In
the bracket of
count(gets matched with the)that was meant to close the innermost subquery, so the leftover bracket is the one at position 15:The token where the parser stopped is right next to the mistake, and it was simply discarded on this path. It is now reported, and every unclosed bracket is listed with its own position instead of only its character:
The same reasoning applies in mirror image to a closing bracket that closes nothing. It can be excessive, and then it is itself the mistake, but it can just as well be the opening bracket that is missing:
and then the leftover closing bracket is as far from the mistake as the column list is long. Running the same damage over the
CREATE TABLEstatements of the same four benchmarks (2280 damaged statements) put the worst case at 2643 characters. The parser stops right where the opening bracket belongs — position 16 inCREATE TABLE t a UInt32, b UInt32), which isa— so its position is preferred there too, and the bracket is still named:Measured distance from the actual mistake:
No information is lost, because both positions are now in the message. A closing bracket that closes nothing also says what it fails to match:
')' at position 10 does not match '[' at position 8instead ofUnmatched parentheses: [).A mistyped keyword was answered with a wall of alternatives. The list of expected alternatives has 34 items in the median and 151 for a typo in the leading
SELECT, with nothing indicating which one was meant. The closest keyword is now named first:This fires for 392 of the errors in the corpus, and in 98% of them the named keyword is the one the original query had at that place. The edit-distance thresholds are deliberately tighter than in
NamePrompter, because here the candidates are keywords of the language rather than names taken from the query, so almost any short word is close to some keyword -hitsis two edits away fromWITH. A word containing a digit is never treated as a mistyped keyword either, which keeps aliases such asan1from being matched againstANY.Validated by running all 190 stateless tests that mention syntax errors; the only failures are unrelated environment ones (no ZooKeeper, no writeable access storage, no test clusters in the local setup).
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Improved syntax error messages. An unclosed bracket is now reported at the place where the parser stopped, which is next to the actual mistake, and every unclosed bracket is listed with its own position - previously the outermost bracket was blamed, which in a large query can be thousands of characters away from the mistake. A mistyped keyword is now named explicitly (
Maybe you meant: SELECT.) instead of only being one item in a list of dozens of alternatives.Workflow [PR]
Sync PR [sync-upstream/pr/116806]