Repository navigation
Conversation
geohashesInBox expands a single input row into up to max_array_size = 10,000,000 geohash strings inside one call to DB::geohashesInBox, and that call held no cancellation checkpoint. The checkpoint the function did have sat one frame above, in the caller's per-row loop, and was evaluated before the expansion of the row it charged for, so for the whole of a large row neither max_execution_time nor KILL QUERY was observed. The bespoke accumulator's own comment claimed a bounded span of "100k items ... tens of milliseconds", but a row near the array-size cap overshoots that by up to 100x. The charge moves into the expansion, using the shared CancellationBudget helper instead of the hand-rolled accumulator, so the checkpoint sits in the same frame as the work. DB::geohashesInBox charges once per emitted geohash and once for a row that emits none, which is the same total the caller charged before: max(1, items_count) per row. The three charge sites are the three exits of the function, the invalid-arguments guard, the nested loop and the items == 0 fallback. The guard is the exit taken by a reversed-bounds or NaN box, whose prepared precision is 0, so leaving it uncharged would have made a block of such rows cost nothing at all. Only when the units are charged changes; every result byte is unchanged, and the granularity moves from a bespoke 100,000 to the shared 65,536. Measured on one debug binary, one row expanding to 9,997,500 geohashes, with max_threads = 1. Before, the reported elapsed at the timeout was 1258 ms for max_execution_time = 0.05, 1186 ms for 0.1 and 1234 ms for 0.5, that is the full expansion regardless of the deadline. After, the same query stops at 108 ms and 501 ms for 0.1 and 0.5, and under the break overflow mode at 51 to 57 ms, 103 to 105 ms and 501 to 508 ms for 0.05, 0.1 and 0.5. A block of 20,000,000 reversed-bounds rows takes 450 ms and now stops at 200 ms for a deadline of 0.2, which is the guard charge site. The expansion of the same 9,997,500-item row costs 1.250 s by the minimum of twelve interleaved runs either way, so the per-encode cost of the charge is within the noise of this machine.
The oracle is which of two textually identical statements raises: the first runs with no deadline and asserts the item count, the second adds max_execution_time = 0.00001 with timeout_overflow_mode = 'break' and expects TIMEOUT_EXCEEDED. Nothing measures a duration, so there is no wall-clock constant to calibrate and nothing degrades on a slower flavour. Every argument is a constant. An all-constant call is executed while the query is analysed and again while it is planned, outside any processor, and an exception from either reaches the client. Inside a pipeline it cannot: ISimpleTransform::work() stores a function's exception on its output port, and the deadline that produced it also makes the executor's own checkTimeLimitSoft() false on the same loop iteration, so the executor cancels and breaks before any downstream processor pulls that port. The query then ends as QueryFinish with no error, on either overflow mode. The box expands to 90,000 geohashes: above the 65,536 items the shared checkpoint interval covers, and below the 100,000 the deleted per-row accumulator needed before it read the clock. That window is exactly where a single row was uninterruptible, so the statement raises with the checkpoint in place and raises nothing without it, at any deadline, which removes the false red a bounded-latency assertion would carry. The control statement prints the count on every run and keeps it inside the window. Measured on debug builds of the same tree, one commit apart: without the checkpoint the arm returns rc 0, type QueryFinish, exception_code 0; with it, rc 159, type ExceptionBeforeStart, and the message names geohashesInBox. Both directions hold with settings randomization enabled (50/50 through the runner, 50/50 driven directly). Deleting the charge from the expansion loop, or making makeCancellationCheck return an empty check, each turns the arm red. Co-Authored-By: Claude Opus 5 <[email protected]>
05153 proves that a check fires somewhere inside one row, not that it fires during the expansion, and no SQL statement can tell the two apart. The result buffer is resized to the whole row before the first encode, so a partially written row has the same size as a complete one; no ProfileEvent counts encodes; and the exception text is identical wherever the check runs. Replacing the three charge sites with a single bulk charge after DB::geohashesInBox returns keeps both of 05153's statements passing while leaving a ten-million-item row uninterruptible again. DB::geohashesInBox is callable directly, so a unit test can observe the output buffer at the moment the check runs. The suite fills that buffer with NUL, which the base32 alphabet never produces, and counts its leading non-NUL bytes from inside the cancellation callback. ChecksCancellationDuringExpansion is the load-bearing case: the callback fires exactly once, and the buffer then holds exactly (units_per_check - 1) * precision bytes, fewer than the whole row. The count is exact rather than a range because charge() decrements while units_left > 1, so the interval's last encode is the one that does not happen. It is written symbolically so that changing the interval cannot red it. InvalidArgumentsExitChargesOneUnit and ZeroItemFallbackChargesOneUnit cover the other two charge sites, the reversed-bounds guard and the items == 0 fallback, by calling units_per_check times against one budget and asserting a single callback and a constant return value. EmptyCheckIsANoOp covers a budget built from an empty function, which is what a background merge or a call with no process list element gets. The first assertion reds when budget.charge() is deleted from the nested loop. Nothing here reads a clock, starts a server or inspects a process list, so there is no timing arm that can become flaky. This does not make either test redundant. The SQL arm catches the loss of the CancellationBudget construction in executeImpl, which a unit test on DB::geohashesInBox cannot see; the unit test catches where the charge sits, which SQL cannot see. Co-Authored-By: Claude Opus 5 <[email protected]>
Filling res_strings_offsets is a second loop over the same row, up to 10,000,000 push_back calls with no checkpoint. A cancelled expansion never reaches it, because the throw leaves DB::geohashesInBox first, so the only exposure is a deadline that falls after the expansion's last checkpoint. I had dismissed that on an unmeasured ratio of operations per item. Measured by adding a second throwaway loop of the same shape and comparing the two builds, one row of 9,997,500 geohashes, max_threads = 1, 22 interleaved runs of each: one offsets loop costs 51 ms by the median and 56 ms by the mean, that is 2.1% of the 2417 ms row. The interval this fix establishes for the expansion is 65,536 of those 9,997,500 items, or 15.8 ms, so the uncharged loop is more than three times the granularity the rest of the fix delivers. A second build of the unchanged tree, measured in the same sweep, differs from the first by 0.5 ms by the median, so the 51 ms is the loop and not the build. The charge is per iteration, which is how CancellationBudget counts everywhere else. A row therefore costs two units per geohash instead of one and the check fires about twice as often within a row; on the same query that costs 7.5 ms by the median and 25 ms by the mean of twelve runs each, with the distributions overlapping (2385 to 2453 ms unchanged against 2404 to 2513 ms charged). The second arm added to 05153 is what distinguishes this commit from its parent. Its box holds 40,000 geohashes, more than half of one interval and less than a whole one, so those encodes reach no checkpoint by themselves and the statement can only raise once writing the offsets is charged as well. It raises here, and returns silently on the parent commit, where the first arm already passes. Like the first arm it asserts which statement raises and never a duration. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal review before publishing: what changed and one residual (click to expand)An independent model reviewed the code cold and bounced one fix round back, which produced two Residual, not blocking. The 40,000-geohash arm proves the offsets loop is accounted for, but not Session id: cron:clickhouse-review-slot-10:20260909-235700 |
|
Workflow [PR], commit [1b4df76] Summary: ❌
AI ReviewSummaryThis PR moves Final VerdictStatus: ✅ Approve |
|
cc @nikitamikhaylov @Algunenano, could you review this? |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units10 translation units recompiled, 12 s compile time in total, 10 of them have a recent master baseline. |
CI finish ledger - 0adc6a9Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260910-053056 |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
max_execution_timeandKILL QUERYbeing ignored whilegeohashesInBoxexpands a single row. One box can produce up to 10,000,000 geohashes inside one call, and the only cancellation checkpoint ran before that expansion started.Description
Requested by @ alexey-milovidov in #109368 (comment) as a separate PR; #109368 is now closed. It does not fix the hung check in his artifact, whose reversed box is zero-item; the reachable shape is one legal box under the size cap.
Root cause.
DB::geohashesInBoxrunslongitude_items * latitude_itemsencodes with no checkpoint. The caller's throttle,items_between_cancellation_checks = 100'000, ran before each row's expansion, so one row could overshoot the shared granularity,CancellationBudget::units_per_check = 65,536, by 152x.The change.
DB::geohashesInBoxtakes aCancellationBudgetand charges once per emitted geohash, plus once for a row emitting nothing; the bespoke accumulator, its constant and itscheckQueryTimeLimithelper are deleted. The caller's loop writing the same row's offsets is charged too: one such loop measures 51 ms of the 2417 ms a 9,997,500-item row takes, more than three times the 15.8 ms interval this fix establishes. Results are byte-identical, and the h3 functions use the same mechanism.Validation.
05153asserts which of two textually identical statements raises, for two boxes: one above the shared interval, one of 40,000 geohashes that can only raise once the offsets loop is charged. Every argument is a constant, so they run during analysis, where nothing absorbs the throw. Both arms are red on master.gtest_geohash_cancellationasserts where inside a row the check lands: at exactly one interval, with the output buffer partially written. Neither test replaces the other. The unit test reds when the per-encode charge is deleted, which SQL cannot see;05153reds when the budget's construction inexecuteImplis deleted, which the unit test cannot see. 50 randomized runs; the 8 existing geohash tests pass.Disclosed. If the per-encode charge is unwanted,
ReplaceRegexpImpl.h's local accumulation is a two-line change, as long as its bulk charge stays underunits_per_check; that call is yours.Related: #109368
Measured latency and overhead, one row of 9,997,500 geohashes, debug build
Server-side
query_duration_ms,max_threads = 1. Before the change the elapsed time is constant whatever the deadline is; after it, it tracks the deadline to within one checkpoint interval.max_execution_timeCost of the checkpoints on the same expansion with no deadline, interleaved runs of one build against another on a loaded machine. The two sweeps below ran hours apart under different load, so only the deltas inside one sweep compare. Per-encode charge alone, 12 runs: minimum 1.250 s unchanged, mean 1.356 s against 1.384 s, median 1.355 s against 1.405 s, distributions overlapping. Both charges, 12 runs: minimum 2385 ms against 2404 ms, median 2424 ms against 2431 ms, mean 2419 ms against 2444 ms, ranges 2385-2453 ms against 2404-2513 ms. A second build of the unchanged tree measured in the same sweep differs from the first by 0.5 ms at the median, which is the noise this sits in.
Workflow [PR]
Sync PR [sync-upstream/pr/119116]