Repository navigation
Fix inconsistent AST formatting for INSERT with SAMPLE and OFFSET - #102547
Conversation
|
Workflow [PR], commit [2e8a52f] Summary: ✅ AI ReviewSummaryThis PR fixes formatter round-trip behavior for ClickHouse Rules
Final Verdict
|
be0591f to
51a7a8a
Compare
51a7a8a to
95756d5
Compare
LLVM Coverage Report
Changed lines: 100.00% (9/9) · Uncovered code |
|
@azat CI is green now. Could you please take some time to review this PR when you're free? Thanks! |
| bool format_from_first = sampleSize() && limitOffset() && !limitLength(); | ||
| /// Note: FROM-first syntax is disabled for SELECTs inside INSERT to avoid | ||
| /// parsing ambiguity with INSERT ... FROM INFILE syntax. | ||
| bool format_from_first = sampleSize() && limitOffset() && !limitLength() && !frame.disable_from_first_syntax; |
There was a problem hiding this comment.
This looks wrong and breaks the whole point of the previous fix #100579
:) EXPLAIN AST INSERT INTO t (FROM system.one SAMPLE 0.5 SELECT 1 OFFSET 10)
EXPLAIN AST
INSERT INTO t SELECT 1
FROM system.one
SAMPLE 5 / 10
OFFSET 10
Query id: 9371f344-f131-4345-a873-e34bb6be0b3e
┌─explain──────────────────────────────────────┐
1. │ InsertQuery (children 2) │
2. │ Identifier t │
3. │ SelectWithUnionQuery (children 1) │
4. │ ExpressionList (children 1) │
5. │ SelectQuery (children 3) │
6. │ ExpressionList (children 1) │
7. │ Literal UInt64_1 │
8. │ TablesInSelectQuery (children 1) │
9. │ TablesInSelectQueryElement (children 1) │
10. │ TableExpression (children 2) │
11. │ TableIdentifier system.one │
12. │ SampleRatio 5 / 10 │
13. │ Literal UInt64_10 │
└──────────────────────────────────────────────┘
13 rows in set. Elapsed: 0.001 sec.
:) EXPLAIN AST INSERT INTO t (SELECT 1
FROM system.one
SAMPLE 5 / 10
OFFSET 10)
EXPLAIN AST
INSERT INTO t SELECT 1
FROM system.one
SAMPLE 5 / 10 OFFSET 10
Query id: c7e11ee7-ffed-4f54-8b76-78565223ca61
┌─explain──────────────────────────────────────┐
1. │ InsertQuery (children 2) │
2. │ Identifier t │
3. │ SelectWithUnionQuery (children 1) │
4. │ ExpressionList (children 1) │
5. │ SelectQuery (children 2) │
6. │ ExpressionList (children 1) │
7. │ Literal UInt64_1 │
8. │ TablesInSelectQuery (children 1) │
9. │ TablesInSelectQueryElement (children 1) │
10. │ TableExpression (children 3) │
11. │ TableIdentifier system.one │
12. │ SampleRatio 5 / 10 │
13. │ SampleRatio 10 │
└──────────────────────────────────────────────┘
13 rows in set. Elapsed: 0.001 sec.And this extra flag looks hacky, can we just do not remove brackets instead?
…_operators=false` contexts
Inside `CODEC` / `STATISTICS` / `BACKUP_NAME` argument lists, `ASTFunction::formatImpl`
forces `frame.allow_operators = false`, so a multi-argument `Function_tuple` falls back
to its function-call form `tuple(arg, arg, ...)` instead of the operator form
`(arg, arg, ...)`.
`RoundBracketsLayer::getResultImpl` in `src/Parsers/ExpressionListParsers.cpp` sets
`parenthesized = true` on any node it unwraps from a single-element `(expr)`. So when a
query like
CREATE TABLE t (c0 Int CODEC(not((not(materialize(1), materialize(2)))), ZSTD))
ENGINE = Memory
first formats, the parser-synthesized inner `Function_tuple` (lifted from the multi-arg
`not(a, b)`) emits as `tuple(materialize(1), materialize(2))`. Re-parsing the output then
sees `tuple(...)` wrapped in `(...)` from the outer `not(...)` argument list and sets
`parenthesized = true` on the re-parsed `Function_tuple`. The next format pass adds an
extra `(...)` around `tuple(...)` via the `parenthesized` flag's parens emission in
`IAST::decideParensEmission`, breaking the format-parse-format round-trip with
`Logical error: 'Inconsistent AST formatting' (STID 1941-1bfa)` in debug / sanitizer
builds.
Both `tuple(a, b)` and `(tuple(a, b))` denote the same value, so dropping the redundant
outer paren in `allow_operators = false` contexts is semantics-preserving and keeps the
round-trip stable. The new clause mirrors the existing literal-tuple suppression a few
lines above.
The fix is gated on `args.size() > 1` so the single-argument `tuple(x)` form keeps its
existing behaviour, and on `!frame.allow_operators` so plain expression contexts where
the operator form `(a, b)` is canonical are unaffected.
This is a partial fix for chronic STID 1941-1bfa: it addresses the `CODEC` / multi-arg
`not(a, b)` subvariant and the `ALTER TABLE ... MODIFY STATISTICS ... TYPE
concatAssumeInjective((...))` subvariant. The other reported subvariants — `DESCRIBE
TABLE (SELECT ... COLUMNS(multiIf(...)) ...)` and qualifier-chained `db.t.COLUMNS(*)` —
involve different parser/formatter paths (`COLUMNS` matcher / `DESCRIBE` wrapper) and
need separate fixes.
Related: PR ClickHouse#102547 (zoomxi, INSERT-with-SAMPLE-OFFSET), PR ClickHouse#104520 (data-type
collection literals), PR ClickHouse#104626 (lambda-after-comma).
Affected PRs (90-day window per @alexey-milovidov 2026-05-14 directive on PR ClickHouse#100412):
93037, 104440, 104812, 104591, 100177, 100651, 104523, 94517, 103428, 99495, 97540,
103866, 103349, 101447, 102646, 100453, 100386, 99179, 94849, 98677, 99131, 99277,
99275, 99227, 99229, 99246, 98884, 99138, 99021, 99105, 98906, 99055, 99011, 99066,
99033, 97821, 70661, 103389, 103347, 100700, master.
CI report (PR ClickHouse#100412 sighting):
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100412&sha=3284612ceea8078d500d582ce82e5ace86b239cf&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29
This PR fixes an AST formatting inconsistency discovered by BuzzHouse fuzzer. When an INSERT query contains a SELECT with both SAMPLE and a query-level OFFSET (without LIMIT), the formatter incorrectly used FROM-first syntax (
FROM t SAMPLE x SELECT ... OFFSET y), which caused the re-parsed query to fail with a syntax error due to ambiguity withINSERT ... FROM INFILEsyntax.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix inconsistent AST formatting for INSERT with SAMPLE and query-level OFFSET. Closes #102523.