Repository navigation
Backport #108493 to 26.6: Fix crash on deeply nested expressions with raised max_parser_depth - #108832
Conversation
… raised max_parser_depth
|
Workflow [BackportPR], commit [d5c3981] Summary: ✅ |
`ParserCollectionOfLiterals::parseImpl` builds an array/tuple/map literal
iteratively with an explicit stack of layers. When a nested collection was
finished it was appended to its parent layer with
`layers.back().arr.push_back(literal->value)`, which deep-copies the whole
nested `Field` accumulated so far. For a deeply nested literal such as
`[[[ ... ]]]` this made parsing quadratic in the nesting depth.
`literal` is a local `ASTLiteral` that is discarded immediately after this
line (only the outermost layer is kept as `node`), so its value can be moved
into the parent instead of copied. This makes the parse linear.
This is needed for the backported test `04412_deep_nested_literal_format`.
It is tagged `no-fasttest, no-debug, no-sanitizers`, but the stress test job
deliberately ignores `no-{build}` tags (every test must run under stress), so
`04412` runs under `Stress test (amd_tsan)`. With the quadratic parse, building
the depth-20000 literal in `formatQuery` took ~1620s under ThreadSanitizer and,
being non-cancellable, tripped the hung-check ("Hung check failed, possible
deadlock found"). With the linear parse the literal is built quickly and
`formatQuery` still reports `TOO_DEEP_RECURSION` (code 306) from the
`FieldVisitorToString` stack guard, exactly as the test expects.
CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108832&sha=401dccaf12f6f90f5d51f3614a5ea775a4088828&name_0=BackportPR&name_1=Stress%20test%20%28amd_tsan%29
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Merged Root causeThe single hung query in the report was the backported test SELECT formatQuery($$SELECT [[[ ... 20000 ... ]]]$$)It is tagged
Fix
Verified with Note for masterMaster's |
`ParserCollectionOfLiterals::parseImpl` builds an array/tuple/map literal
iteratively with an explicit stack of layers. When a nested collection was
finished it was appended to its parent layer with
`layers.back().arr.push_back(literal->value)`, which deep-copies the whole
nested `Field` accumulated so far. For a deeply nested literal such as
`[[[ ... ]]]` this made parsing quadratic in the nesting depth.
`literal` is a local `ASTLiteral` that is discarded immediately after this
line (only the outermost layer is kept as `node`), so its value can be moved
into the parent instead of copied. This makes the parse linear.
This is needed for the backported test `04412_deep_nested_literal_format`.
It is tagged `no-fasttest, no-debug, no-sanitizers`, but the stress test job
deliberately ignores `no-{build}` tags (every test must run under stress), so
`04412` runs under `Stress test (amd_tsan)`. With the quadratic parse, building
the depth-20000 literal in `formatQuery` took ~1620s under ThreadSanitizer and,
being non-cancellable, tripped the hung-check ("Hung check failed, possible
deadlock found"). The same quadratic parse also made `04412` run for ~441s under
`Stateless tests (amd_asan, distributed plan, parallel)` and the server got
killed by signal. With the linear parse the literal is built quickly and
`formatQuery` still reports `TOO_DEEP_RECURSION` (code 306) from the
`FieldVisitorToString` stack guard, exactly as the test expects.
This mirrors the fix already applied to the 26.6 backport in
#108832 and the master change
#108892.
CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108896&sha=0a636f95b8f50b19259d8d0280d26e62639e47a4&name_0=BackportPR&name_1=Stress%20test%20%28amd_tsan%29
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`ParserCollectionOfLiterals::parseImpl` builds an array/tuple/map literal
iteratively with an explicit stack of layers. When a nested collection was
finished it was appended to its parent layer with
`layers.back().arr.push_back(literal->value)`, which deep-copies the whole
nested `Field` accumulated so far. For a deeply nested literal such as
`[[[ ... ]]]` this made parsing quadratic in the nesting depth.
`literal` is a local `ASTLiteral` that is discarded immediately after this
line (only the outermost layer is kept as `node`), so its value can be moved
into the parent instead of copied. This makes the parse linear.
This is needed for the backported test `04412_deep_nested_literal_format`.
It is tagged `no-fasttest, no-debug, no-sanitizers`, but the stress test job
deliberately ignores `no-{build}` tags (every test must run under stress), so
`04412` runs under `Stress test (amd_tsan)`. With the quadratic parse, building
the depth-20000 literal in `formatQuery` took ~1620s under ThreadSanitizer and,
being non-cancellable, tripped the hung-check ("Hung check failed, possible
deadlock found"). With the linear parse the literal is built quickly and
`formatQuery` still reports `TOO_DEEP_RECURSION` (code 306) from the
`FieldVisitorToString` stack guard, exactly as the test expects.
This mirrors the same fix applied to the master change in #108892 and to the
26.6 backport in #108832.
CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108899&sha=573ba507851cfb5c2c745dee34924cbdbf4fe08a&name_0=BackportPR&name_1=Stress%20test%20%28amd_tsan%29
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`ParserCollectionOfLiterals::parseImpl` copied the just-finished nested collection into its parent layer on every closing bracket, making the parsing of a deeply nested literal such as `[[[ ... ]]]` quadratic in its depth. The new test `04412_deep_nested_literal_format` builds `formatQuery([...20000...])`. The `Stress test (amd_tsan)` runner ignores the test's `no-*` tags and runs it anyway, so the quadratic build never finishes in time and trips the "Hung check failed, possible deadlock found" check. Move the value into the parent instead of copying it. `literal` is a local that is discarded right after (only the outermost layer is kept as `node`), so moving its value out is safe and produces an identical `Field`. This mirrors the master change in #108892 and the 26.6 backport in #108832. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108897&sha=cc66187446ca00eca7656b498616f904352d22eb&name_0=BackportPR&name_1=Stress%20test%20%28amd_tsan%29 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The deeply-nested-expression tests added by this backport
(`04412_deep_nested_literal_format`, `04413_set_param_deep_collection`)
construct arrays nested ~20000 levels deep. Under the stress test,
which ignores the `no-debug`/`no-sanitizers`/`no-fasttest` tags and runs
every test, parsing such a literal under TSan took ~1620 seconds and was
reported as a hung query ("Hung check failed, possible deadlock found").
`ParserCollectionOfLiterals::parseImpl` iteratively builds the collection
with a stack of layers. On every closing bracket it copied the whole
just-completed nested `Field` into the parent layer with
`push_back(literal->value)`, deep-copying the entire subtree at each
level and making parsing O(d^2) in the nesting depth `d`.
The `literal` is a local `ASTLiteral` that is discarded immediately after
(only the outermost one is kept as `node`), so its value can be moved
instead of copied. This makes parsing O(d) while producing a byte-identical
`Field`. The formatting guard still trips the `FieldVisitorToString`
recursion check and returns `TOO_DEEP_RECURSION`; only the parse speed changes.
Same fix as master sibling #108892
and the 26.6 backport #108832.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`ParserCollectionOfLiterals::parseImpl` builds an array/tuple/map literal
iteratively with an explicit stack of layers. When a nested collection was
finished it was appended to its parent layer with
`layers.back().arr.push_back(literal->value)`, which deep-copies the whole
nested `Field` accumulated so far. For a deeply nested literal such as
`[[[ ... ]]]` this made parsing quadratic in the nesting depth.
`literal` is a local `ASTLiteral` that is discarded immediately after this
line (only the outermost layer is kept as `node`), so its value can be moved
into the parent instead of copied. This makes the parse linear and leaves the
produced `Field` byte-identical.
This also keeps the deeply nested literal regression tests
`04412_deep_nested_literal_format` and `04413_set_param_deep_collection` fast:
they are tagged `no-fasttest, no-debug, no-sanitizers`, but the stress-test job
ignores `no-{build}` tags and runs every test, where the quadratic parse of the
depth-20000/30000 literal was slow enough to trip the hung check under
ThreadSanitizer. Surfaced by the 26.6 backport ClickHouse#108832.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`ParserCollectionOfLiterals::parseImpl` builds an array/tuple/map literal
iteratively with an explicit stack of layers. When a nested collection was
finished it was appended to its parent layer with
`layers.back().arr.push_back(literal->value)`, which deep-copies the whole
nested `Field` accumulated so far. For a deeply nested literal such as
`[[[ ... ]]]` this made parsing quadratic in the nesting depth.
`literal` is a local `ASTLiteral` that is discarded immediately after this
line (only the outermost layer is kept as `node`), so its value can be moved
into the parent instead of copied. This makes the parse linear and leaves the
produced `Field` byte-identical.
This also keeps the deeply nested literal regression tests
`04412_deep_nested_literal_format` and `04413_set_param_deep_collection` fast:
they are tagged `no-fasttest, no-debug, no-sanitizers`, but the stress-test job
ignores `no-{build}` tags and runs every test, where the quadratic parse of the
depth-20000/30000 literal was slow enough to trip the hung check under
ThreadSanitizer. Surfaced by the 26.6 backport ClickHouse#108832.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Original pull-request #108493
This pull-request is a last step of an automated backporting.
Treat it as a standard pull-request: look at the checks and resolve conflicts.
Merge it only if you intend to backport changes to the target branch, otherwise just close it.
The PR source
The PR is created in the CI job
Version info
26.6.2.16