Repository navigation
Conversation
Since ClickHouse#127389 `WindowTransform::flushBatch` called `collectArgumentBlocks` for every batch: it walked from the batch block back to the first retained block and then built one `WindowArguments::Block` per retained block, and every slot copied that vector. The positions in it were counted from the partition start, so the vector could not be reused between batches. A frame that starts at `UNBOUNDED PRECEDING` retains the whole partition, so a partition of B blocks cost Theta(B^2) block visits. Keep one index for the whole transform instead, next to `SlidingBlocks`: an entry is appended in `addInputBlock` and popped together with its block in `releaseUnusedBlocks`, and positions are counted from the first input row, so an entry never changes while its block is retained. A batch now computes only the transform-wide position of its partition start and points every slot at the shared index; `WindowArguments::at` keeps taking partition-relative positions and adds that offset. The per-argument block cache stores a pointer to the entry and is checked in `at` itself, so a lookup that hits it neither indexes the deque nor calls the binary search (with the check inside an out-of-line `findBlock`, every lookup paid a call, 3-5% on the `nth_value`/`lag`/`lead` queries at the default block size). `SELECT lagInFrame(number) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM numbers(40000) FORMAT Null SETTINGS max_block_size = 1, max_threads = 1`, median of 3: 9.958 s before, 0.191 s after (RelWithDebInfo without LTO); 10.917 s before, 0.229 s after (debug; 0.217 s before ClickHouse#127389). Closes ClickHouse#127659 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The shared argument index added one entry per retained block also for windows whose functions take no arguments (aggregate-only windows, `row_number`, `rank`, ...), which never read it. Master skipped all argument work for them, so keep the index only when some general function has arguments. Peak memory with one-row blocks, 1M rows (`--memory-usage`): `row_number() OVER ()` 357901880 on master, 387340944 before this change, 357901880 after; `sum(number) OVER (ROWS UNBOUNDED PRECEDING)` 376681663, 408749231, 376681703. Also shorten the new perf query to 1M rows so it stays under the perf report's single-run limit on the old server (1.047 s -> 0.101 s locally). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (2 rounds, head 4ba3a43): 1 major + 1 nit agreed and fixed, nothing open
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261028-224700 |
|
Workflow [PR], commit [3702271] Summary: ❌
AI ReviewSummaryThe PR removes the per-batch retained-block scan from Missing context / blind spots
Final VerdictStatus: ✅ Approve. No requested code changes. |
|
cc @novikd, could you review this and add the |
| Int64 end_position = 0; | ||
| }; | ||
|
|
||
| using Blocks = std::deque<Block>; |
There was a problem hiding this comment.
Removed in 3864c62, the type is spelled out now.
| std::vector<size_t> column_indices; | ||
| std::vector<Block> blocks; | ||
| mutable std::vector<size_t> last_used_block_for_arg; | ||
| const Blocks * blocks = nullptr; |
There was a problem hiding this comment.
Removed in 3864c62: WindowArguments now gets the index in its constructor and keeps a const std::deque<Block> & (like Frame::params), and the per-argument cache holds a copy of the last used Block instead of a pointer.
| ArgumentValue at(size_t argument, Int64 position) const; | ||
|
|
||
| private: | ||
| const Block & findBlock(Int64 position) const; |
There was a problem hiding this comment.
Why was it moved??
never unite private fields and methods.
There was a problem hiding this comment.
It returned const Block &, which needs Block declared above it, so I moved it next to the fields. In 3864c62 it is back in its original place with its original parameters (it now refreshes the cache entry instead of returning an index).
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units14 translation units recompiled, 25 s compile time in total, 14 of them have a recent master baseline. |
Review of ClickHouse#128097: no `Blocks` alias, no raw pointers, and private methods stay separate from the fields. `WindowArguments` now receives the transform's argument block index in its constructor and keeps a reference to it (like `Frame::params`); per batch the transform only sets the partition start. The per-argument cache holds a copy of the last used `Block` instead of a pointer. `findBlock` is back in its original place with its original parameters and refreshes that cache, so the cache hit in `at` is unchanged: in a RelWithDebInfo build without LTO the six default-block-size window queries of the existing perf tests are within 1% of the previous commit (median of 12 runs, 1.1% on `nonNegativeDerivative`). Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 3864c62Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261029-180003 |
| std::vector<Block> blocks; | ||
| mutable std::vector<size_t> last_used_block_for_arg; | ||
| const std::deque<Block> & blocks; | ||
| Int64 partition_start = 0; |
There was a problem hiding this comment.
Removed in 3702271. The index now counts positions from the start of the current partition, as collectArgumentBlocks did, so at needs no offset: startNextPartition renumbers the stored entries once and calls WindowArguments::resetPartition, which drops the cached block entries.
| auto materialized_columns = materializeColumns(chunk.getColumns(), params.should_materialize); | ||
| auto index = indexes.calculate(materialized_columns, rows_count); | ||
| auto & block = blocks.add(std::move(chunk), std::move(materialized_columns), std::move(index)); | ||
| if (has_arguments) |
There was a problem hiding this comment.
Done in 3702271: has_arguments is gone, and the index is appended and popped for every window. For a window without argument functions this costs 32 bytes per stored block, +8% peak memory for row_number() over 1M one-row blocks.
Review of ClickHouse#128097: `WindowArguments` keeps no partition offset, and the argument index is maintained for every window. The shared index now numbers its positions from the start of the current partition, as `collectArgumentBlocks` did before, so `WindowArguments::at` reads positions as the window functions pass them. `startNextPartition` renumbers the stored entries once and drops the per-argument block cache, whose entries carry the old numbering. Blocks before the partition start get negative positions, which keeps the index sorted. With no stored blocks left, a new block starts at the current row, so its first position is that row's index in the partition. `has_arguments` is removed: the index is appended in `addInputBlock` and popped in `releaseUnusedBlocks` unconditionally. For a window without argument functions over one-row blocks this costs 32 bytes per stored block (peak memory +8% for `row_number`/`sum` over 1M one-row blocks). Release build, median of 12 runs: the six default-block-size lag/lead/ nth_value/exponentialTimeDecayedSum/nonNegativeDerivative queries are 0.934 to 1.025 of master and 0.978 to 1.014 of the previous head. The previous head's inline cache check in `at` is kept: master's `findBlock` shape over the deque measured +5% to +21%. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 3702271Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261030-150005 |
|
Fixing PR for the Performance Comparison (arm_release, master_head, 2/6) dashboard HTTP 500 failure: #128255 |
On 2026-10-30 the performance dashboard backend was switched to a new CIDB (explained by egor-click on ClickHouse#128236). For about 3 minutes (14:09:54Z to 14:13:05Z, measured from the polls of 6 shards) it answered HTTP 500 with ClickHouse code 60 for the tables it reads: failed to resolve performance comparison run: code: 60, message: Unknown table expression identifier 'perf_test_times_v1' failed to load performance comparison rows: code: 60, message: Unknown table expression identifier 'query_metrics_v2' `dashboard_did_not_answer` knew no signature for this message, so the 500 was a `PerfDashboardError`; the poll loop keeps the first hard error, and `main` failed the shard instead of judging it by its own report. Four `Performance Comparison (arm_release, master_head, N/6)` jobs whose 600 s wait ended inside the window failed this way (ClickHouse#126879 6/6, ClickHouse#128209 1/6 and 3/6, ClickHouse#128097 2/6), all with 0 slower queries. Shards whose wait outlived the window got the same requests answered with 200 and passed. The request cannot cause this answer: the table names are the backend's own storage, and identical requests (same run id and test) were served after the window. It is the case the class documents, "the dashboard, or the CIDB tables it reads, did not answer", so add the measured signature next to the two existing ones. Only a 5xx with an application envelope is affected; a 4xx with this text, a non-JSON body, and every other 5xx keep their classification, and a real hard error seen before or after it still wins. Validated locally against a fake dashboard serving the logged bodies: the incident replay fails on master and reaches the own-report fallback with the fix. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
will do it differently. |
Closes: #127659
Related: #127389
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Not required: the slow path exists only on master since #127389 and is not in any release.
Description
Since #127389,
WindowTransform::flushBatchrebuilds the argument block index for every batch:collectArgumentBlockswalks all retained blocks, and every function slot copies the result. A frame that starts atUNBOUNDED PRECEDINGkeeps the whole partition, so a partition of B blocks costs O(B^2) block visits.Now the transform keeps one index: an entry is appended in
addInputBlockand removed with its block inreleaseUnusedBlocks, and everyWindowArgumentsholds a reference to it. Positions are still counted from the start of the current partition, so the window functions are unchanged:startNextPartitionrenumbers the stored entries once and drops the per-argument block cache. The cache is checked inWindowArguments::atitself, so a cache hit costs no call.collectArgumentBlocksis removed.SELECT lagInFrame(number) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM numbers(N) FORMAT Null SETTINGS max_block_size = 1, max_threads = 1, median of 3, local RelWithDebInfo build without LTO:I added a 1M-row
max_block_size = 100variant towindow_functions_streaming.xml(1.045 s -> 0.105 s locally). At the default block size, the sixlag/lead/nth_value/exponentialTimeDecayedSum/nonNegativeDerivativequeries of the existing perf tests stay between 7% faster and 3% slower than master (median of 12 runs each). The index is kept for every window, so a window without argument functions pays 32 bytes per stored block (+8% peak memory forrow_number()over 1M one-row blocks). The existing block-size tests (05293*, 05441-05446) pass 50 times each with random settings, and fail when the partition offset is shifted by one.Workflow [PR]
Sync PR [sync-upstream/pr/128097]