Repository navigation
Docs: break does not always return a partial result - #113902
alexey-milovidov merged 3 commits into
Conversation
The `timeout_overflow_mode` and `timeout_overflow_mode_leaf` descriptions promise unconditionally that `break` will "stop executing the query and return the partial result, as if the source data ran out". A user who picks `break` specifically to avoid errors can still get an error. `QueryStatus::checkTimeLimit()` returns false rather than throwing under `break`, signalling "stop and return what you have". That contract is expressible wherever a truncated output is a smaller valid output, but not where it would be a wrong one, so several call sites deliberately stop instead of truncating. Three distinct shapes do this. A function computing one scalar value (`arrayFold.cpp`, `geohashesInBox.cpp`, `FunctionBaseXXConversion.h`, `FunctionVariantAdaptor.cpp`, and the string replace family through the shared `CancellationBudget.cpp` helper, each site carrying an in-code comment stating the rationale). For that shape the stop reaches the client as `TIMEOUT_EXCEEDED` only when the call is not interrupted mid-pipeline: a constant-folded call is evaluated during analysis with no pipeline to absorb the throw, so the error surfaces, whereas inside a pipeline the executor's own soft check and the function's check race and either stop is correct, leaving the query to end without a client-visible error. `04650_replace_cancellation.sh:203-206` states that race, and its `:218-224` arm asserts the surfaced error for the folded call. This is why the description says such a function stops without producing its value rather than naming one error code for the shape. An operation whose output must be applied atomically, which among the mutation targets is the `Memory` table (`StorageMemory::mutate`, which throws "Timeout exceeded while mutating `Memory` table"; a `MergeTree` mutation instead keeps waiting, see below). And a quorum write, which cannot report a partial success at all: `ReplicatedMergeTreeSink.cpp:1308-1322` deliberately ignores the false return and keeps waiting until the quorum is satisfied or `insert_quorum_timeout` elapses, then reports `UNKNOWN_STATUS_OF_INSERT`. That shape does not stop the query at `max_execution_time` at all once the wait has been entered, and `04337_quorum_insert_honors_time_limit` asserts the quorum outcome. It is not alone in discarding the false return: the dictionary-load wait (`ExternalDictionariesLoader.cpp:125`) and the synchronous mutation waits (`StorageMergeTree.cpp:1074`, `StorageReplicatedMergeTree.cpp:839`) do the same, which is why the description says "some operations" rather than naming a single shape. The quorum wait is the one of those with a bound of its own (`insert_quorum_timeout`, `ReplicatedMergeTreeSink.cpp:1299`), so it is the one the description names. None of the PRs that introduced those sites amended the description, which is why it is stale. The error code depends on which executor runs the query, so the description names the codes rather than attributing one code to a statement type, and it hedges: the exception is a property of the site, not of a category. The read path raises `TIMEOUT_EXCEEDED`. An `INSERT` whose data the caller pushes block by block runs on `PushingPipelineExecutor` or `PushingAsyncPipelineExecutor`, which translate the executor's `CancelledByTimeout` status into `QUERY_WAS_CANCELLED` through `throwOnExecutionStatus`, a helper that exists nowhere else in `src/`. The common case is the native protocol, which `03221_insert_timeout_overflow_mode` asserts for both executors (`TCPHandler.cpp:1501` and `:1506`); the same push loop is also driven by the PostgreSQL wire `COPY ... FROM` handler (`PostgreSQLHandler.cpp:617`), the gRPC handler (`GRPCServer.cpp:1069`) and `clickhouse-local` (`LocalConnection.cpp:388` and `:394`), and the timeout is live on all of them because `executeQuery.cpp:2055-2058` sets the process list element on any pushing pipeline. An `INSERT` whose pipeline is instead completed with its own input source runs on `CompletedPipelineExecutor`, which never inspects the execution status and so reports no error at all. That covers `INSERT ... SELECT`, whose reference in `04416_storage_alias_insert_cancel_no_crash` is a bare `survived` with the insert's own output piped through a filter that would have shown any error text, and it also covers an insert whose data is the body of an HTTP request, which `executeQuery` completes at `:2722-2725` before running it at `:2790`. Measured against a local server: the same 3000000-row body sent over HTTP under `break` returned no error, while sending it with `throw` returned `TIMEOUT_EXCEEDED` and sending it over the native protocol under `break` returned `QUERY_WAS_CANCELLED`. Calling that shape "streamed" would therefore point the reader at the wrong code, since an HTTP upload is also a streaming insert, so the wording names the protocol as the common case without claiming it is the only one. The `QUERY_WAS_CANCELLED` wording stays hedged for a second reason: an in-pipeline throw pre-empts that translation. `PushingPipelineExecutor::push` reaches `throwOnExecutionStatus` only when `executeStep()` returns false, and `PipelineExecutor::executeStep` rethrows any `node.exception` at `PipelineExecutor.cpp:258-260` before reaching `return false` at `:264`; the async path rethrows at `PushingAsyncPipelineExecutor.cpp:189` before `:192`. A materialized view's inner `SELECT` runs as an `ExceptionKeepingTransform` (`InsertDependenciesBuilder.cpp:571`), so a native-protocol `INSERT` feeding a view whose `SELECT` contains one of the scalar-value functions above reports `TIMEOUT_EXCEEDED` rather than `QUERY_WAS_CANCELLED`. Whether any rows survive is a property of the target engine rather than of the timeout, so the description does not promise retention. `MergeTreeSink::consume` calls `finishDelayedChunk()` unconditionally once per pushed block; inside it `checkTimeLimit()` has its bool return discarded, and under `break` that returns false without throwing, so each pushed block's part is committed during `consume` and stays: five runs of a 3000000-row native-protocol insert into a MergeTree table with `max_execution_time = 0.3` reported `QUERY_WAS_CANCELLED` and left between 1472000 and 1496000 rows in four to seven committed parts. Engines that publish in `onFinish` keep nothing, because cancellation stops the executor scheduling further work: `PipelineExecutor::cancel` calls `finish()`, every executor loop is guarded by `!tasks.isFinished()`, and a sink's `onFinish()` is reachable only from `work()`. The same insert into `Memory`, `Log` and `StripeLog` tables reported `QUERY_WAS_CANCELLED` and left zero rows, the two `Log` engines having truncated their data files back to the recorded sizes in the sink destructor's rollback branch; rows written before the statement were unaffected. `StorageMemory::mutate` likewise throws before swapping its data in, and `04229_memory_mutation_timeout_break` asserts the table is unchanged afterwards, so the atomic-mutation and scalar-function shapes are genuine instead-cases and keep that word. The same sentence also made `max_estimated_execution_time` a subject of `break`, but `ExecutionSpeedLimits.cpp:82` gates the whole estimated-time check on `throw`, so under `break` the estimate is never evaluated. That gate is intentional and predates this change, so only the text is corrected. The leaf description names `max_execution_time_leaf` and no estimate, and there is no `max_estimated_execution_time_leaf` setting, so the clause is not repeated there. For the same reason the leaf description names only `TIMEOUT_EXCEEDED`: `timeout_overflow_mode_leaf` is read at one site, whose callers are both read paths, so neither an `INSERT` nor a quorum write is reachable through it. The hedge is also required in the other direction: `FillingTransform` and `sleep` honour `break` and do return partial output. Measured with `max_execution_time = 1, timeout_overflow_mode = 'break'`: a scalar `arrayFold` over `range(1000000)` gives `elapsed time limit reached in function arrayFold`; the same fold over 40 ascending rows at `max_block_size = 1` returns rows with no error; the same scalar fold with `throw` gives the executor's own message instead of the in-function one; and a `Memory`-table `UPDATE` gives the mutation message with zero in-function messages, confirming it as a second independent carrier. The sentence is shared boilerplate appearing 6 times in `Settings.cpp`; only these 2 settings are on the elapsed-time path, so the other 4 (row and byte threshold modes, where a partial result always exists) are left unchanged. `docs/reference/settings/session-settings/timeout-overflow-mode.mdx` is inside an AUTOGENERATED region and regenerates from these descriptions, whereas `query-complexity.mdx` is hand-written and carries the same sentence under its generic `overflow_mode` heading, so it is updated directly. That heading governs every `overflow_mode` setting on the page, while this caveat is elapsed-time only, so the paragraph there names `max_execution_time` explicitly. No behaviour change. Returning a half-produced value would be silent wrong results, strictly worse than an exception, so the implementation is correct and untouched.
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 7 review rounds, 6 fix rounds,
Severity: ❌ blocker / Verified clean and left byte-identical: the Session id: cron:clickhouse-review-slot-9:20260807-232300 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20260807-230800 |
|
cc @vitlibar, could you review this? It is text only: the |
|
Workflow [PR], commit [bc210c5] Summary: ✅
AI ReviewSummaryThis PR narrows the Findings
Final VerdictChanges requested. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 29/29 (100.00%) · Uncovered code |
Two sentences added by the previous commit are themselves categorical where the source is conditional, which is the defect class this change exists to remove. `StorageMemory::mutate` has two branches and only one throws unconditionally. The all-columns branch fails closed on any cancellation. The partial-column branch throws only from `reject_incomplete`, which fires on a block-count or a per-block row-count mismatch, so a soft timeout that flips after every expected block was already produced passes both shape checks and the mutation is committed with no exception. That late flip is reachable rather than theoretical: `PullingPipelineExecutor::pull(Block)` returns true with a cleared block on the soft timeout, which is the case the second shape check exists to catch. The text now scopes the unchanged-table-plus-`TIMEOUT_EXCEEDED` outcome to a mutation the timeout leaves incomplete. `ReplicatedMergeTreeSink::waitForQuorum` throws `UNKNOWN_STATUS_OF_INSERT` under `if (!quorum_updated)`, reached only when the inner wait exits on `quorum_timeout_ms`, which is `insert_quorum_timeout`. If the quorum is satisfied first the watch fires, the loop breaks with `quorum_updated` set and the wait returns normally after logging that the quorum is satisfied. The previous wording attached the report to both alternatives; it now attaches it to the timeout branch alone, leaving the ignored false return and the absent `max_execution_time` stop unchanged, since both of those are correct. The leaf description is deliberately untouched: it names neither shape, because `timeout_overflow_mode_leaf` is read at a single site whose callers are both read paths, so no `INSERT` and no quorum write is reachable through it. No behaviour change; both files carry the same sentence and both are updated.
The `timeout_overflow_mode` row of the hand-written summary table in `query-complexity.mdx` is a verbatim copy of the first sentence of the setting's `DECLARE` docstring in `src/Core/Settings.cpp`. This PR added "(the estimate is only checked with `throw`)" to that docstring and left the copy behind, so the two surfaces of the same sentence disagreed and the page still documented a `break`-mode estimated-time path. `ExecutionSpeedLimits.cpp:82` gates the whole estimated-time block on `timeout_overflow_mode == OverflowMode::THROW`, so under `break` the estimate is never evaluated. The gate predates this change; only the text is corrected. Measured: the row is byte-identical at the PR base and at `origin/master` and was an exact mirror of the docstring sentence at both, so the divergence was introduced by this PR rather than pre-existing. The autogenerated `timeout-overflow-mode.mdx` page carries the same sentence but regenerates from `Settings.cpp` and is correctly left untouched. `timeout_overflow_mode_leaf` has no estimate setting, so its row is unchanged. Raised by clickhouse-gh[bot] in ClickHouse#113902 (comment)
CI finish ledger - bc210c5CI is fully finished and green: 176 check-runs, 0 incomplete, 0 red (153 success / 23 skipped), No failed tests, so no owner rows are needed. Session id: cron:our-pr-ci-monitor:20260812-170000 |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
Both
timeout_overflow_modedescriptions promise unconditionally thatbreakreturns"the partial result, as if the source data ran out", so a user picking
breakto avoiderrors can still get one.
QueryStatus::checkTimeLimitreturns false rather than throwing underbreak, meaning"stop and return what you have". That works where a truncated output is a
smaller valid one, not where it would be a wrong one, so several sites stop instead of
truncating. A function computing one scalar value (
arrayFold,geohashesInBox, stringreplace) stops without producing it, surfacing
TIMEOUT_EXCEEDEDonly when notinterrupted mid-pipeline, since a pipeline absorbs the throw into a clean cancellation. An
incomplete
Memory-table mutation (StorageMemory::mutate) leaves the table unchangedand reports it. A quorum write cannot report partial success at all, so it ignores the false
return and does not stop at
max_execution_time: it waits outinsert_quorum_timeout,then reports
UNKNOWN_STATUS_OF_INSERT. The mutation and dictionary-load waits are thesame, hence "some operations".
The
INSERTcode differs by executor:QUERY_WAS_CANCELLEDwhen the caller pushes datablock by block, as over the native protocol; "may", because an in-pipeline throw
pre-empts that translation, and a pipeline completed with its own input source raises
nothing. Retention is an engine property, so none is promised.
That sentence also made
max_estimated_execution_timea subject ofbreak, butExecutionSpeedLimits.cpp:82gates that check onthrow, so the estimate is neverevaluated. That gate predates this change, so only the text is corrected. The leaf
setting has no estimate and reaches no
INSERTor quorum write, so those clauses areomitted there.
The sentence is shared boilerplate appearing 6 times in
Settings.cpp; only these 2 areon the elapsed-time path, so the other 4 threshold modes are unchanged. Hedging is
required in both directions:
FillingTransformandsleepdo return partial output. Nobehaviour changes.
Raised by
clickhouse-gh[bot]in #112483 (comment).Version info
26.8.1.1300(included in26.8and later)