Repository navigation
Let the query condition cache serve conditions whose bounds changed - #127479
alexey-milovidov wants to merge 20 commits into
Conversation
Dashboards repeat a query with literal time bounds that move a little with every refresh (issue #78502). Each variant is a deterministic condition with its own hash, so the entries of one refresh were never found by the next one. The derivation for `now()` from #115522 does not help: it only rewrites non-deterministic constants, and a window that moves on both sides is never contained in the earlier one, whatever the grid. Now a query with such a condition also looks up the entries of up to four recent variants of the same condition with other values of the time bounds (comparisons of an expression with a `Date`/`Date32`/`DateTime`/`DateTime64` constant, or with a string constant if the expression has such a type, under `AND`, `OR` and `NOT`). A granule recorded there as not matching is skipped unless the primary key shows that it may have rows admitted by the new bounds but not by the earlier ones. The derived verdicts are written under the new variant as well, so the entries of the latest variant stay complete. The write paths are unchanged. The keys of the recent variants are remembered in `QueryConditionCache`, per table and per structure of the condition with the constants left out. The reuse is controlled by the new setting `use_query_condition_cache_for_changed_time_bounds` (enabled by default). #78502 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
|
Workflow [PR], commit [6d75a25] Summary: ❌
|
|
This seems oddly specific -- I wonder if a general approach has the DDL specify columns whose conditions are "likely to change across queries" and then those conditions are ignored for QCC or subject to the narrowing / band covering argument of the PR? |
A condition that is non-deterministic only because of folded constants of time comparisons, e.g. `time >= now() - INTERVAL 10 DAY`, is now cached under its own hash, which includes the value of the constant, instead of under a condition with the constant rounded onto a time grid. Later queries reuse the entries through the earlier variants of the condition, like for literal bounds: a lower bound that moved forward is narrower, so all recorded granules are skipped at once, whatever the primary key; a window moving on both sides is reused through the primary key. With the grid, an unaligned lower bound was reused only from the next grid cell, and a moving window not at all. The rounding code and the setting `query_condition_cache_time_condition_grid_factor` are removed (not released yet). The reuse of earlier variants is controlled by `use_query_condition_cache_for_time_conditions`, which replaces the setting `use_query_condition_cache_for_changed_time_bounds` added in the previous commit. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…hanged Generalize the reuse of entries of earlier variants of a condition from time bounds to bounds of any numeric type: a bound is now a comparison of an expression with a constant of an integer, floating point, `Decimal` or date/time type (or a string compared with a date/time expression). The argument does not depend on time: a condition built of `AND`, `OR` and `NOT` over comparisons is monotone in each compared constant, so e.g. `price > 150` reuses the entries of `price > 100` whatever the primary key, and moving bounds of a primary key column reuse them through the band. A NaN constant is not a bound: a comparison with it is never true, so the condition is not monotone in it. Constants of native numeric types are told apart by the type of their `Field` rather than by their data type, because the literals `100` and `300` have different types (`UInt8`, `UInt16`); the band is built with constants of a type that fits each value. The setting `use_query_condition_cache_for_time_conditions` is renamed to `use_query_condition_cache_for_changing_bounds` (not released yet), and `QueryConditionCacheTimeConditions` to `QueryConditionCacheBounds`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes132 object files changed (+185.11 KiB total), 1 added.
717 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units1899 translation units recompiled, 5696 s compile time in total, 1898 of them have a recent master baseline. |
The flaky check showed that the shell tests ran too long under ASan (more than 180 seconds, mostly starting clients) and that a random `merge_tree_read_split_ranges_into_intersecting_and_non_intersecting_injection_probability` splits reads into layers, from which the query condition cache records nothing. Rewrite both tests in SQL with a single check of the granules read in `system.query_log`, disable the injection, fix the size of granules, and allow the priming queries to read a granule more. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=127479&sha=841a579a632c9f25b62a992f8090e35fb316ba77&name_0=PR #127479 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…he-literal-time-bounds
…ory budget The variants of conditions with bounds remembered by `QueryConditionCache` lived outside of the cache and were limited only by counts. Give them a byte budget of 1/16 of the cache size, evict the least recently used conditions when it is exceeded or reduced by `setMaxSizeInBytes`, and remember nothing when the cache is disabled. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The condition on the primary key that checks the band between an earlier and the current variant of a condition with bounds ignored `use_primary_key`. Construct it with `skip_analysis_` like the ordinary primary key condition, so with `use_primary_key = 0` only variants with an empty band are reused. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…he-literal-time-bounds Conflicts in `Settings.cpp` and the docs of the query condition cache: master marked `use_query_condition_cache_for_time_conditions` and `query_condition_cache_time_condition_grid_factor` experimental, as shipped in 26.10. This PR replaces both with `use_query_condition_cache_for_changing_bounds`, so the PR side is kept; as the old settings were released in 26.10, they are moved to the obsolete settings instead of being removed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 @groeneai, investigate the failures and provide fixes in separate PRs. If a fix is already in progress, link it here.
Both are unrelated to this PR. The branch has been merged with the latest master. |
|
🕵 @EmeraldShift, the PR is no longer specific to time: after the first revision it was generalized to any bound, i.e. a comparison ( |
|
🕵 The RabbitMQ failure above is already being fixed in #126746. |
|
🕵 Merged the latest
|
|
🕵 Merged the latest
|
The tests rely on the entries of the instance-wide query condition cache staying between their queries. Concurrent tests clear it: dropping or renaming a `MergeTree` table without UUID calls `Context::clearCaches`. In the targeted runs, `QueryConditionCacheEntries` in `metric_log` drops to a few entries every several seconds, and the queries of the tests that followed such a drop read all granules. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=127479&sha=latest&name_0=PR&name_1=Stateless%20tests%20(arm_asan_ubsan%2C%20targeted) #127479 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Merged the latest The @groeneai, investigate the failure: Stress test (amd_debug), |
|
The failing frame is |
…he-literal-time-bounds Conflict: `src/Interpreters/Cache/QueryConditionCacheTimeConditions.cpp` was deleted in this PR and modified on `master` (b67008d, floor division fix); keep the deletion. The replacement code in `QueryConditionCacheBounds.cpp` does not use that division.
|
🕵 Merged the latest |
|
@alexey-milovidov any chance of getting this MR merged in the near future? |
|
🕵 @orloffv, the PR is ready from the author's side: CI on the previous head was green, there are no open review threads, and I've just merged the latest |
…experimental and disabled by default The replaced time-condition derivation was `EXPERIMENTAL` and disabled by default, so introduce the new cross-variant pruning strategy the same way: the setting is now `EXPERIMENTAL` with default `false`, its history entry and documentation say so, and the tests of the feature enable it explicitly. It can be enabled by default once it has been validated more broadly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Merged the latest |
Queries are often repeated with bounds that change a little every time: dashboards with time bounds given as literals (
WHERE timestamp >= fromUnixTimestamp64Nano(L1) AND timestamp <= fromUnixTimestamp64Nano(L2) AND message LIKE '%x%') or relative to the current time (WHERE time >= now() - INTERVAL 1 HOUR AND ...), or a threshold tweaked by the user (WHERE price > 100 AND category = 'books'). Every variant has a hash of its own, so the query condition cache entries written by one query were never found by the next one.A query with such a condition now also looks up the entries of up to four recent variants of the same condition with other values of the bounds. A granule recorded there as not matching is skipped unless the primary key shows that it may have rows admitted by the new bounds but not by the earlier ones (the "band"). If the new bounds are narrower (e.g.
time >= now() - INTERVAL 10 DAYa bit later, orprice > 150afterprice > 100), there is no band and every recorded granule is skipped regardless of the primary key. The skipped granules are written under the new variant as well, so the entries of the latest variant stay complete.A bound is a comparison (
<,<=,>,>=) of an expression with a constant of an integer, floating point,Decimalor date/time type (or a string constant compared with a date/time expression); bounds are combined with each other and with arbitrary deterministic subconditions byAND,OR,NOT(which flips the direction). This works because such a condition is monotone in each compared constant. A NaN constant is not a bound (a comparison with it is never true). The keys of recent variants are remembered inQueryConditionCacheper table and per structure of the condition with the constants left out; constants of native numeric types are told apart by the type of theirField, sox > 100(UInt8) andx > 300(UInt16) are variants of the same condition.This replaces the grid snapping from #115522 (not released yet): a condition that is non-deterministic only because of folded constants of its bounds (
now(),today(), ...) is cached under its own hash, which includes the values of the constants, and later queries reuse it through the earlier variants. Compared to the grid, an unaligned lower bound is reused immediately instead of from the next grid cell, and a window moving on both sides is reused through the primary key instead of never; aligned bounds liketoday() - 10keep the same key, as before. The settingsuse_query_condition_cache_for_time_conditionsandquery_condition_cache_time_condition_grid_factorare replaced byuse_query_condition_cache_for_changing_bounds, which is experimental and disabled by default, like the setting it replaces.On the setup from the issue comment (10M rows, both bounds shifted by 3 ms), the repeated query reads 16384 rows instead of 122880.
Closes: #78502
Related: #115522
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The query condition cache now serves conditions whose bounds changed since they were cached, e.g. a dashboard refresh moving the time range
WHERE time >= ... AND time <= ...(with literals ornow()), orWHERE price > 150afterWHERE price > 100: granules recorded as not matching an earlier variant of the condition are skipped unless the primary key shows they may have rows between the earlier and the new bounds. This replaces the time-grid rounding for conditions withnow()introduced earlier in this release cycle. Enabled by the experimental settinguse_query_condition_cache_for_changing_bounds(disabled by default).🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/127479]