Repository navigation
fix: Include read columns when updating MergeTreeBlockSizePredictor - #122919
Conversation
|
Workflow [PR], commit [faa73ee] Summary: ❌
AI ReviewSummaryPR #122919, reviewed at Missing context / blind spots
Final Verdict✅ Approve. No code changes requested; complete current-head CI before merging. |
…add a test Address the review: dropping the first-update reinitialization in `update` meant that `DEFAULT` and `MATERIALIZED` columns that are not stored in the part, and all columns of compact parts (which have no per-column sizes), never got a real estimation of bytes per row. Restore the reinitialization from the result block and merge the columns remembered in the constructor into it, so columns read only by `PREWHERE` steps keep the estimation obtained from the part instead of being dropped from the estimate. In the per-block update, keep the current estimation of such columns instead of indexing the result block with a name it does not have. Remove the `getReadSampleBlock` plumbing: the predictor is constructed with all columns read from the part (including `PREWHERE` columns), so passing the read header to every `update` only added a `Block` construction per read call without changing the result. Add a stateless test: on master `sum(k) ... PREWHERE s LIKE '%a%'` reads 8192-row blocks of 2 KB strings with `preferred_block_size_bytes = 200000`; with the fix the blocks stay within one 256-row granule. Closes: ClickHouse#107596
|
I pushed two commits on top of this branch ( What changed relative to the previous revision:
Note for reviewers: on the current master the pure Related: #126041 (same root cause, overlapping fix). |
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, 90 s compile time in total, 14 of them have a recent master baseline. Median compile-time ratio to the baselines is ×0.92 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
… without copying the sample block The previous revision remembered the sample block of the part in a `Block` member of the predictor and merged it into the result block on the first `update`. The predictor is created once per part and copied once per read task, so every part paid for a `NameSet`, a `Block` with its name index, a `cloneWithColumns` and one `insertUnique` per column. In the performance test `merge_tree_many_partitions_2` (3000 parts of 26 `UInt64` columns) this added 13.5 MB of memory (15.2 MB to 28.8 MB), 21 ms to `QueryPipelineBuildMicroseconds` (97 ms to 118 ms) and 15% to the query time (177 ms to 204 ms). Now the constructor stores nothing but the names and value sizes of the fixed-size columns of the part (`fixed_columns_from_part`, one small vector), and the estimates of the variable-size columns are already in `dynamic_columns_infos`. On the first `update`, `initialize` keeps the entries whose column is absent from the result block, which are exactly the columns read only by `PREWHERE`, and rebuilds the rest from the actual columns as before. The estimate for the columns read only by `PREWHERE` is unchanged, so the fix for ClickHouse#107596 and the test `05321_block_size_predictor_prewhere_only_columns` are not affected. Performance report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122919&sha=d5bc17863adae170e934707dd34edfd94b6aa38e&name_0=PR&name_1=Performance%20Comparison%20%28arm_release%2C%20master_head%2C%203%2F6%29 Pull request: ClickHouse#122919
|
Analysis of the three performance comparison failures on
Expected on the next run: |
|
@groeneai check if perf regressions are related |
|
I checked every query the performance comparison flagged on faa73ee (arm, 1 slower, 6 faster, 1 unstable). All of them except the unstable one are caused by the PR, and only through the block size.
Apart from that remaining memory, nothing here can be avoided while keeping the fix: getting |
5157e6d
Backport #122919 to 26.10: fix: Include read columns when updating `MergeTreeBlockSizePredictor`
…shorter than a bucket The selector of a table with buckets of samples wrapped every read in `WHERE notEmpty(samples)` over `timeSeriesSliceSortedArray(samples, a, b)`. Filter push-down moves that condition into PREWHERE, which reads the raw `samples`, computes the slice and drops the raw column, so the raw column is PREWHERE-only. Since ClickHouse#122919 `MergeTreeBlockSizePredictor` counts such a column at its part estimate on top of the measured slice copy: for a long window (the slice is the whole array) bytes per row double, and with granules under 512 KiB a block holds about one granule instead of two or three. Whole-metric selectors then process about twice as many blocks (`promql_selector_id_range_row_cost` #0/ClickHouse#1 1.3x slower with 262144-byte granules, ClickHouse#131678). The filter is a no-op unless the range is shorter than a bucket: every sample of a row lies in [bucket, bucket + step), `min_time`/`max_time` are the smallest/largest of them, and the inner query keeps a row only if `max_time >= a AND min_time <= b`. If `min_time >= a` the sample at `min_time` is in the range, if `max_time <= b` the sample at `max_time` is, so an empty slice needs `min_time < a AND b < max_time`, i.e. `b - a < max_time - min_time < step`. With a bound absent the same holds for the other bound. So the filter is now emitted only when both bounds are set and `b - a < step` in the units of the table being read (the samples table or the recent samples table, each with its own step). The difference is computed in Int128: the bounds of a DateTime64 table can be anywhere in [INT64_MIN, INT64_MAX]. For long windows nothing on `samples` goes to PREWHERE, the raw column is read by the main step and measured once, and the slice is computed after it. The bucket step at the table scale is moved into `getBucketStep`, shared with `makeBucketedSamplesTimeRangeConditions`. Measured (release build, master vs this change concurrently on identical parts, 2 x 25 interleaved runs, max_threads = 4): row_cost #0 0.81/0.76 at 262144, 0.86/0.81 at 131072, 0.93/0.84 at 524288; ClickHouse#1 0.79/0.73, 0.88/0.83, 0.99/0.91; promql_selector_old_short_window 0.94-0.97. Results identical. Edge: a row written directly with `min_time`/`max_time` looser than its samples (against the documented contract) can now be returned as an empty array by `timeSeriesSelector(..., 'samples')` for a long range. PromQL drops series whose merged array is empty, as it does for rows emptied by the stale-marker filter. Tests: 05770_time_series_selector_empty_slice_filter checks both sides of the bucket boundary and the plan shape (no-parallel-replicas: without a local plan the initiator's EXPLAIN does not show the replicas' filters). New perf test promql_selector_small_granules: the row_cost shape at 131072-byte granules, the default of the recent samples table. Related: ClickHouse#131678 Related: ClickHouse#122919 Co-Authored-By: Claude Opus 5.5 <[email protected]>
After the optimization introduced in #89982, the columns that were only used in the
PREWHEREno longer appear in the sampleBlockused to recompute the predicted size of the following blocks to read; only the columns that were part of the output port of the pipeline step.As a consequence, when the columns to read were large compared to the output columns, the computed predicted size (only output columns) was very small compared to the actual memory needed to read the input rows. The next read task will include too many rows, and it can crash with an OOM error.
In this pull request, I'm fetching all the columns used by all the stages of the reader and will keep considering them when updating the
MergeTreeBlockSizePredictor. We don't have, though, the actual size of these columns, so we keep using the estimation we used in the first iteration. We also don't have information about other materialized columns that could require additional memory (... WHERE lower(huge_str_column) != ''), but IMHO, this fix allows for future extensions in those directions.Closes: #107596
Changelog category:
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix computation of the predicted size for MergeTree readers involving
PREWHEREclause. The columns that are read just to execute the prewhere clause are also considered for the block size, preventing out-of-memory errors.Workflow [PR]
Sync PR [sync-upstream/pr/122919]
Version info
26.11.1.2291-master(included in26.11and later)26.10.5.7