Repository navigation
TimeSeries selector: filter out empty sample slices only when the time range is shorter than a bucket - #131766
Conversation
…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]>
The "shorter than a bucket" case used [700.001, 1199.998], 499.997 s, so any threshold above 499.997 s and up to 600 s passed the test. The bucket-600 row now starts at its bucket (sample 600) and the range is [600.001, 1199.998], 599.997 s, still strictly between the row's two samples. A threshold of 599 s now fails the first and third lines. The reference is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (rounds 1-2): 1 finding fixed, 1 nit kept, round 2 clean
Round 2 (the test fix): no findings. |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261115-053300 |
|
Workflow [PR], commit [a7efc57] Summary: ✅
AI ReviewSummaryReviewed PR head Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 13 s compile time in total, 7 of them have a recent master baseline. |
CI finish ledger - 38144e9Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261115-113011 |
readImpl now converts the bucket step of the table being read to a Decimal64 at the scale of its timestamp type once, and passes it down instead of the step in seconds; getBucketStep is removed. Every use is either a zero check or wants the value at the table's timestamp scale. Decimal64 matches TimeSeriesSink, which stores the steps the same way. The product fits in Int64: check_bucket_step caps the step at UInt32::max seconds and the scale of DateTime64 is at most 9. No behaviour change: 05770 passes, and fails under four mutations (an unscaled step and a 599 s threshold in the compare, `<=` instead of `<`, and an unscaled step at the conversion in readImpl). Co-Authored-By: Claude Opus 5.5 <[email protected]>
PromQL Compliance ReportBaseline: S3 master
Extended support (upstream engine tests): native-histogram exclusions: 149; excluded assertions: 248; upstream Note: Prometheus compliance score is unchanged vs baseline. |
CI builds the merge commit with master, which now contains ClickHouse#131766, so the performance comparisons measure this PR's defaults with the selector fix. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #131678
Related: #122919
Related: #131459
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Description
A selector over a TimeSeries table with buckets of samples reads
timeSeriesSliceSortedArray(samples, a, b)and filters it withWHERE notEmpty(samples). The filter goes to PREWHERE, so the rawsamplesbecomes a PREWHERE-only column, and since #122919 the block-size predictor counts it on top of the slice copy. Tables with granules under 512 KiB are read in about 2x more blocks (2836 vs 1215 at 262144 bytes), sopromql_selector_id_range_row_cost#0 and #1 are 1.3x slower on #131678.A row holds the samples of one bucket and matches the range by
min_timeandmax_time, so its slice can be empty only if the range lies strictly between them, which needs a range shorter than a bucket. The selector now emits the filter only for such ranges, so results do not change. Edge: a row written directly withmin_time/max_timelooser than its samples now appears as an empty array intimeSeriesSelector(..., 'samples')over a long range; PromQL drops such series. Doublingpreferred_block_size_bytesfor these reads (#131459) also restores the speed, but enlarges the blocks of every bucketed read.Validation: new test
05770_time_series_selector_empty_slice_filter(fails on master and on three mutations of the compare), the TimeSeries/PromQL tests, and a new perf testpromql_selector_small_granules(131072-byte granules, as in the recent samples table). Release build, master vs this PR on byte-identical parts, 2 x 25 interleaved runs, ratio of medians (PR / master):promql_selector_old_short_window(#131678): 0.94-0.97.Not for changelog: version 11 (#117344) is only on master.
Workflow [PR]
Sync PR [sync-upstream/pr/131766]
Version info
26.11.1.2393-master(included in26.11and later)