Skip to content

Stop the Freeform structure search at the query's memory and time limits - #123238

Merged
alexey-milovidov merged 8 commits into
ClickHouse:masterfrom
groeneai:fix-freeform-search-query-limits
Oct 2, 2026
Merged

alexey-milovidov merged 8 commits into
ClickHouse:masterfrom
groeneai:fix-freeform-search-query-limits

Conversation

@groeneai

@groeneai groeneai commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #122447
Related: #122412

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

The search for the structure of a Freeform row now stops at max_memory_usage, max_execution_time and KILL QUERY.

Description

Stress test (arm_release) fails with Hung check failed, possible deadlock found on master since #122447 (master report) and on PRs. The stress runner sets compatibility below 26.10 in 20% of runs, which restores input_format_freeform_max_search_steps = 0. 05258 then infers a row of 24 words, a search of about 2^25 steps, and nothing can stop it:

  • The candidates are kept in standard containers, whose allocations do not throw on the memory limit, so the query grows to 22 GB against max_memory_usage = 150000000 until the server runs out of memory.
  • The search never polls the query status, so KILL QUERY and max_execution_time are ignored (is_cancelled: 1 for 8 minutes in CI).

The same holds for any query with input_format_freeform_max_search_steps = 0 or an older compatibility, including reading rows with an explicit structure, which reruns the search inside a processor.

The search now calls CurrentMemoryTracker::check() and QueryStatus::checkTimeLimit() once per step and once per row checked by a candidate, so it ends with MEMORY_LIMIT_EXCEEDED, TIMEOUT_EXCEEDED or QUERY_WAS_CANCELLED.

Freeform first ships in 26.10, so I can also make the compatibility entry keep the bound at 4096 if you prefer. This change is still needed for an explicit = 0.

Validation (debug build): on master the CI query is still running after 90 s with is_cancelled = 1 and 7 GB tracked, and KILL QUERY ... SYNC does not return; with the fix it ends in 1.6 s with MEMORY_LIMIT_EXCEEDED, and KILL QUERY ... SYNC returns in 0.17 s. The new test 05315_freeform_structure_search_query_limits has one case per limit (each hangs on master), and one where validating the candidates takes 7 s on master and now stops at max_execution_time = 0.1; it passes 100 randomized runs. Inference time at the default bound is unchanged (41 vs 40 ms median).

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/123238]

Version info

  • Merged into: 26.10.1.1260-master (included in 26.10 and later)

groeneai and others added 3 commits October 1, 2026 08:04
…imits

With `input_format_freeform_max_search_steps = 0`, which any `compatibility`
below 26.10 restores, the search for the structure of a wide row of words
is exponential and could not be stopped:

- its candidates live in standard containers, whose allocations go through
  the throwing `operator new` and are never refused by the memory tracker
  (`min_allocation_size_to_throw_on_memory_limit` is 0 by default), so the
  query passed its `max_memory_usage` and took the server's memory;
- nothing in the search polled the query status, so `KILL QUERY` and
  `max_execution_time` only set a flag nobody read.

The stress runner randomizes `compatibility`, and `05258` then hung the
server in `Stress test (arm_release)` (`Hung check failed`): 22 GB tracked
against a 150 MB limit, `is_cancelled = 1` for 8 minutes.

Check `CurrentMemoryTracker::check()` and `QueryStatus::checkTimeLimit()`
once per `buildSolutions` call and once per validated candidate. The query
status is resolved from the executing thread, so the check also works when
the search runs inside a processor while reading rows with an explicit
structure. A `false` from `checkTimeLimit` (`timeout_overflow_mode = 'break'`)
throws too, because a partial schema has no meaning.

Add one case per limit to `05258`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The time-limit cases of `05258` do not cover `KILL QUERY`: with
`checkTimeLimitSoft` in place of `checkTimeLimit`, both stay green while
a killed search ends with `TIMEOUT_EXCEEDED` instead of
`QUERY_WAS_CANCELLED`. The new case starts an unbounded search, waits
until it has allocated 10 MB, then runs `KILL QUERY ... SYNC` under a
10 s timeout. On master the KILL does not return and the query keeps
growing past its 2 GB `max_memory_usage`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e limit

No case of `05258` covers the checkpoint before each candidate's
validation: with it deleted, every arm stays green because the
checkpoint in `buildSolutions` stops them first. The new case keeps the
search within the default bound with a short first row, then makes
every one of the 2047 candidates fail only on the last of 100 long rows,
so validation takes about 7 s on a debug build. At `max_execution_time`
0.1 the query ends with `TIMEOUT_EXCEEDED`; with the validation
checkpoint deleted, and on master, it runs to `BAD_ARGUMENTS` after
about 7.3 s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding labels Oct 1, 2026
@groeneai

groeneai commented Oct 1, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 3 rounds) plus my own cold review in each round.

# Sev Finding Verdict Evidence / action
1 ⚠️ No test for KILL QUERY: the time arms stay green even if the search reads only the elapsed time (checkTimeLimitSoft) AGREE, fixed @ 2a3e2c7 New arm: kill the search while it runs and require QUERY_WAS_CANCELLED. It fails on master and on the checkTimeLimitSoft mutant
2 ⚠️ No test reaches the per-candidate checkpoint in the validation loop AGREE, fixed @ 5e4de29 New arm at the default bound: a short first row keeps the search small, long later rows make each validation expensive, and the last row fails every candidate. It ends with TIMEOUT_EXCEEDED, and fails on master and when that checkpoint is deleted
3 💡 The test header does not mention the new limit arms AGREE, fixed @ 2a3e2c7 One header sentence

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-9:20261001-105300

@groeneai

groeneai commented Oct 1, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Two rows of 24 tab-separated words, desc file(..., 'Freeform') settings max_memory_usage = 150000000, compatibility = '25.11', max_execution_time = 5: on master no answer in 90 s, is_cancelled = 1, 7 GB tracked with the peak frozen at 149995871, server reaches its memory limit. 2 of 2 runs.
b Root cause explained? compatibility below 26.10 restores input_format_freeform_max_search_steps = 0. The search keeps its candidates in standard containers, whose allocations are never refused by the memory tracker, and it never polls the query status, so neither the memory limit, the time limit nor KILL QUERY can stop it.
c Fix matches root cause? Yes. The search's own steps (each buildSolutions call and each validated candidate) now call CurrentMemoryTracker::check() and QueryStatus::checkTimeLimit(). No bound, setting or test-only change.
d Test intent preserved / new tests added? 05258 keeps every existing case and adds one per limit: memory, time during inference, time while reading rows with an explicit structure, KILL QUERY, and time during a long validation of the candidates (2047 candidates that each fail only on the last of 100 long rows).
e Both directions demonstrated? Each new case hangs or runs to the end on master and passes on the fix in 0.1-0.6 s. Mutants: no memory check hangs the memory case; no time check fails both time cases; checkTimeLimitSoft fails only the KILL case; no per-candidate check fails only the validation case (BAD_ARGUMENTS after 7.3 s).
f Fix is general across code paths? Both entry points of the search (schema inference and row reading inside a processor) go through buildSolutionsAndPickBest, and the validation loop is covered too. The parse-error handlers do not swallow the limit errors, and schema inference reports them inside CANNOT_EXTRACT_TABLE_STRUCTURE with the original code.
g Fix generalizes across inputs (params/datatypes/wrappers)? Measured: explicit = 0 and compatibility 25.11, DESC and SELECT with a structure, timeout_overflow_mode throw and break, max_memory_usage = 0. Typed rows do not branch and are unaffected.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, default or format change. A query that could not be stopped now ends with its own limit error.
i Invariants and contracts preserved? The new exceptions leave the search at the same point as the existing step-bound exception, the query status is a local and is never stored, and the per-step cost is one uncontended lock (default-bound inference: 41 vs 40 ms median).

Session id: cron:clickhouse-impl-slot-5:20261001-103700

@clickhouse-gh

clickhouse-gh Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cb24b95]

Summary: ✅


AI Review

Summary

This PR makes the Freeform structure search poll query memory, timeout, and cancellation limits during candidate generation and validation, and adds a focused regression test. The C++ side looks sound to me, but the new shell test still has two harness-level issues that can make it fail or hang independently of the server behavior, so I would request changes before merging.

PR Metadata
  • Changelog category should be Improvement, not Not for changelog: this changes user-visible Freeform query behavior when input_format_freeform_max_search_steps = 0, not only CI.
  • Exact replacement:
    ### Changelog category (leave one):
    - Improvement
    The current Changelog entry text is specific enough and can stay as-is.
Findings
⚠️ Majors
  • [tests/queries/0_stateless/05315_freeform_structure_search_query_limits.sh:19] Both yes | head pipelines in this test (here and at line 52) leak yes: standard output: Broken pipe to the script's stderr on GNU coreutils. clickhouse-test treats any non-empty stderr from a .sh test as having stderror, so the new regression test can fail before it even exercises Freeform.
    Suggested fix: build the repeated-row strings without yes, or at least redirect yes's stderr in both places.
  • [tests/queries/0_stateless/05315_freeform_structure_search_query_limits.sh:46] The KILL QUERY arm uses an unbounded wait after a timed KILL QUERY ... SYNC. On the very regression this arm is meant to catch, the kill times out but the background DESC keeps running, so the test then hangs until the global test timeout instead of failing promptly.
    Suggested fix: bound the post-kill wait, emit an explicit failure if the client is still alive after the deadline, and reap it, following the pattern in tests/queries/0_stateless/03986_kill_query_mutation_sync_replicated.sh.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    • Fix the two shell-test harness issues in tests/queries/0_stateless/05315_freeform_structure_search_query_limits.sh so the regression test stays quiet on stderr and fails fast when KILL QUERY stops working.

LLVM Coverage Report

Measured on commit cb24b95.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 38/39 (97.44%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Oct 1, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). label Oct 1, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing cb24b95e6 with master 8de7a5fd4 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 12 s compile time in total, 7 of them have a recent master baseline.

Job report

groeneai and others added 3 commits October 1, 2026 14:11
In the flaky check (8 copies of the test at once, with the thread fuzzer), the
extended `05258` took 225-233 s on `amd_asan_ubsan` against the 180 s limit.
Master's own cases of `05258` already take 159-172 s there, because each one
runs a search of about 4096 steps. So the new cases go to
`05315_freeform_structure_search_query_limits`, and `05258` is back to master's
version.

The memory case was the slowest one (75 s): a row of 24 words needs about 12k
steps to fill 50 MB with candidates. A row of 240 words makes every candidate
ten times larger, and a 20 MB limit is reached after about 500 steps.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The limits were polled once per candidate, before its validation. One
candidate reads up to 100 rows and
`input_format_max_bytes_to_read_for_schema_inference` bytes, so with a
raised byte budget a single validation could outlast `max_execution_time`
and `KILL QUERY`. Measured on a debug build with a one-word first row and
99 rows of 10 MB (budget raised): `max_execution_time = 0.1` fired after
0.73 to 1.43 s, and `KILL QUERY` was noticed 0.79 s after it was issued.
With the check in the row loop the query stops at 0.10 s, and within one
row of the `KILL`.

The check at row 0 also covers the old per-candidate check. The
worst-case search of `05315` (2047 candidates, 100 rows each) takes the
same time with and without it (7.4 to 7.7 s). A test where one
candidate's validation outlasts the time limit needs a sample of several
GB, so the existing validation case of `05315` covers the check instead:
deleting it turns that case into `BAD_ARGUMENTS`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…break'`

In `break` mode `checkTimeLimit` returns false instead of throwing, and
the cancellation checker does not cancel the query, so only the search's
own check of that result stops it. The DESC time-limit case now runs in
that mode; the pipeline and validation cases keep the default `throw`.
Ignoring the result makes the case end with `MEMORY_LIMIT_EXCEEDED`
after 10 s instead of `TIMEOUT_EXCEEDED`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov and others added 2 commits October 2, 2026 03:32
…the `Freeform` limits test

`yes | head` prints `yes: standard output: Broken pipe` to stderr, which fails a `.sh` test, so the
repeated rows are built without `yes`. The client of the killed query now runs in the background with
its output in a file, and the test waits for it at most 60 seconds after `KILL QUERY`, reporting an
explicit error and killing the client instead of hanging until the test time limit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merged via the queue into ClickHouse:master with commit 3666d3d Oct 2, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 2, 2026
kolsha added a commit to groeneai/ClickHouse that referenced this pull request Oct 2, 2026
Preserve explicit secret masks before formatting nested maps and let the
`S3` classifier handle named maps before the generic positional pass.

Related: ClickHouse#123238
zoomxi pushed a commit to zoomxi/ClickHouse that referenced this pull request Nov 19, 2026
… memory limit

The KILL case of `05315_freeform_structure_search_query_limits` lost a race against the query's own
`max_memory_usage = 2000000000` on `Fast test (arm_darwin)` (PR ClickHouse#110102): no client could connect to
the server for 8.4 s right after the poll saw the search under way, the search reached its limit and
threw `MEMORY_LIMIT_EXCEEDED`, and the KILL then landed on the query while it was freeing its
candidates.

The KILL is now the poll itself and matches only while `peak_memory_usage` is between 10 MB and 1 GB,
so a sent KILL is always seen by the search, and a late one matches nothing instead of racing the
limit. The peak, not `memory_usage`: the current usage of a query that failed on its limit falls back
through the window while it frees its candidates. An attempt in which the query ended before any KILL
could be sent says nothing about KILL and is repeated, at most 3 times. `kill_throw_if_noop = 0`
keeps the empty matches off stderr. The client output goes to a file and the client is bounded by
`timeout 60`, as in the review change `ba2c2d703b40` of ClickHouse#123238, which never reached master.

Related: ClickHouse#110102
Co-Authored-By: Claude Opus 5.5 <[email protected]>
zoomxi pushed a commit to zoomxi/ClickHouse that referenced this pull request Nov 19, 2026
`yes | head` prints `yes: standard output: Broken pipe` to stderr where SIGPIPE is ignored, which
fails a `.sh` test. The rows are byte-identical. This is part of the review change `ba2c2d703b40` by
Alexey Milovidov on ClickHouse#123238, which never reached master.

Co-Authored-By: Alexey Milovidov <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants