Skip to content

Backport #108493 to 26.6: Fix crash on deeply nested expressions with raised max_parser_depth - #108832

Merged
clickhouse-gh[bot] merged 3 commits into
26.6from
backport/26.6/108493
Jun 30, 2026
Merged

clickhouse-gh[bot] merged 3 commits into
26.6from
backport/26.6/108493

Conversation

@robot-clickhouse-ci-2

@robot-clickhouse-ci-2 robot-clickhouse-ci-2 commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

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

  • Merged into: 26.6.2.16

@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added pr-backport Changes, backported to release branch. Do not use manually - automated use only! pr-critical-bugfix labels Jun 29, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [BackportPR], commit [d5c3981]

Summary: ✅

alexey-milovidov and others added 2 commits June 30, 2026 06:47
`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]>
@alexey-milovidov

Copy link
Copy Markdown
Member

Merged origin/26.6 into the branch (it was 6 commits behind and red) and pushed a fix for the only CI failure, Stress test (amd_tsan) → "Hung check failed, possible deadlock found" (report).

Root cause

The single hung query in the report was the backported test 04412_deep_nested_literal_format:

SELECT formatQuery($$SELECT [[[ ... 20000 ... ]]]$$)

It is tagged no-fasttest, no-debug, no-sanitizers, but the stress-test job deliberately ignores no-{build} tags — clickhouse-test raises an error for the no-stress tag ("stress tests must be able to run every test"), so 04412 runs under ThreadSanitizer.

ParserCollectionOfLiterals::parseImpl builds the nested literal iteratively, and at every closing bracket it appended the finished collection to its parent with layers.back().arr.push_back(literal->value), which deep-copies the whole Field accumulated so far. For a depth-d literal this is O(d²). Building the depth-20000 literal inside formatQuery took ~1620s under TSan and, being non-cancellable, tripped the hung check (is_cancelled = 1, elapsed = 1620s, read_rows = 0).

Fix

literal is a local ASTLiteral 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 O(d). The literal is built quickly and formatQuery still reports TOO_DEEP_RECURSION (code 306) from the FieldVisitorToString stack guard, exactly as the test expects — only the parse speed changes. The same path (ParserAllCollectionsOfLiterals) is used by 04413_set_param_deep_collection (SET param_p = [...]), so that test is sped up too.

Verified with -fsyntax-only under -Werror.

Note for master

Master's 04412/04413 are identical to the backported versions and carry the same latent quadratic-parse slowness; master just doesn't run this stress workflow on PRs. I'll open a separate PR applying the same one-line fix to master so the branches stay in sync.

alexey-milovidov added a commit that referenced this pull request Jun 30, 2026
`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]>
alexey-milovidov added a commit that referenced this pull request Jun 30, 2026
`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]>
alexey-milovidov added a commit that referenced this pull request Jun 30, 2026
`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]>
alexey-milovidov added a commit that referenced this pull request Jun 30, 2026
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]>
@clickhouse-gh
clickhouse-gh Bot merged commit 218ed6d into 26.6 Jun 30, 2026
39 checks passed
@clickhouse-gh
clickhouse-gh Bot deleted the backport/26.6/108493 branch June 30, 2026 08:59
pull Bot pushed a commit to mikeyhodl/ClickHouse that referenced this pull request Jun 30, 2026
`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]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
`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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backport Changes, backported to release branch. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants