Repository navigation
Fix set and text skip indexes and projections dropping rows for conditions with non-deterministic functions - #122787
Conversation
The `set` skip index computes the query condition on the distinct values it stores for each granule and skips a granule when none of them passes. A function that can return another value on each evaluation, such as `rand`, was copied into that computation like any other function, so it was drawn once per stored value instead of once per row, and a granule was skipped when that draw failed although about half of its rows pass their own draw. A plain sampling filter `WHERE rand() % 2 = 0` on a table with a `set` index returned about a quarter of the rows. `checkDAGUseless` also treated `rand()` as usable because it has no arguments, so the index was applied to a condition that references no indexed column at all. The `text` index had the same defect on its `mapKeys(m)` and `JSONAllPaths(j)` paths: they evaluate the whole atom once on the default value, skip every granule without the key or path when the result is false, and checked determinism only on the root node of the atom. Refuse such a function where the index copies or evaluates it, with `isDeterministicInScopeOfQuery`, the predicate `KeyCondition` and the virtual-column filter already use. Constants folded from `now`, `getSetting` or `randConstant` keep working, so no deterministic condition loses pruning. The test pins `text_index_posting_list_block_size` to its default on the text index tables: with the small block sizes the randomizer draws, the index cannot skip granules for this data at all, so those rows could not tell a fixed build from a broken one. ClickHouse#89615 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…index A function node is named like a column with that text (`rand()`), so on a table with a set index over a column named `rand()` the query's `rand()` was bound to the stored column before the determinism check ran. Check it first in both `atomFromDAG` and `checkDAGUseless`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; round 1 full pass: 1 finding; round 2 delta recheck: 2 findings), plus my own review (1 nit).
Severity: ❌ blocker / Session id: cron:clickhouse-review-self-slot-58:20260928-223100 |
|
Workflow [PR], commit [a6d88cb] Summary: ✅
AI ReviewSummaryThe PR prevents non-deterministic predicates from filtering index values or aggregate projection rows. Both added regression tests pass on the binary built from head Findings❌ Blockers
Tests
Final Verdict❌ Block. Close the two remaining pruning gaps and add their regressions, or link explicit follow-up issues and narrow the claimed fix. Existing projection, full skip-index, nested-lambda, and |
…nd` condition The `json path` row only counts rows, so a broken build can pass it by chance: when its single planning-time draw of `rand` succeeds, or when rows without the path are read from a granule the index cannot skip. Add an `EXPLAIN indexes = 1` row that the `ji` index is not used for that condition, as the map keys case already does. ClickHouse#122787 (comment) 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 Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units10 translation units recompiled, 59 s compile time in total, 10 of them have a recent master baseline. |
…maller The flaky check runs eight copies of a new test at once with the thread fuzzer on, and this test took 244-300 s per run there, over the 180 s limit. Most of it was the inserts into the `set` index tables: the index aggregator filters the whole inserted block once per granule, so with one granule per 8 rows the cost of an 80000-row insert grows with the square of the row count. Use 8000 rows and scale the tolerance windows with them; each window is still at least 11 standard deviations wide. The map and JSON rows use `rand() % 100` so that about 40 rows without the key still pass on a correct build. On master every non-control row still fails. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - ba2a001Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260929-090225 |
|
Fixing PR for the |
…deterministic-condition Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 9cf5886Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The Fast test failure dropped 88 dependent jobs, so only the four Session id: cron:our-pr-ci-monitor:20261014-153008 |
Two projection paths had the same defect as the `set` and `text` skip indexes: they compute the query's filter on rows that are not the table rows, so a function that can return another value on each evaluation, such as `rand`, decides for many rows at once or is drawn twice. - An aggregate projection computes the filter and the GROUP BY keys once per projection row, that is once per group. With `PROJECTION p (SELECT k, count() GROUP BY k)`, the query `SELECT k, count() FROM t WHERE rand() % 2 = 0 GROUP BY k` kept or dropped whole groups (56 of 100 groups, each with all of its rows), and `GROUP BY k, rand() % 2` returned 100 groups instead of 200. The same happened for PREWHERE and DISTINCT. Only the implicit `_minmax_count_projection` was excluded for such a filter. Now `analyzeAggregateProjection` rejects any aggregate projection whose keys or filter are not deterministic in the scope of the query. - The projection index (`PROJECTION p INDEX b TYPE basic`) computes the part of the filter it can compute from the projection columns on the projection rows to find the table rows to read, and the read then computes the whole filter again, so `WHERE b = 5 AND rand() % 2 = 0` sampled twice: 2529 of 10000 rows instead of about 5000, and 72064 of 200000 instead of about 100000 with default settings on a table of 2000000 rows. `restrictFilterDAGToInputs`, used only for this, now treats such a function as not computable, so the projection keeps filtering by `b = 5` and the `rand` conjunct is computed once. Measured on master 7a987ba (debug), which also confirms the other skip index types (`minmax`, `bloom_filter`, `tokenbf_v1`, `ngrambf_v1`, the other `text` index paths) and normal projections do not have it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
set and text skip indexes dropping rows for conditions with non-deterministic functionsset and text skip indexes and projections dropping rows for conditions with non-deterministic functions
Closes: #89615
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results for a query whose condition calls a non-deterministic function such as
randorgenerateUUIDv4on a table with asetskip index, atextindex onmapKeysorJSONAllPaths, an aggregate projection or a projection index: the function was computed on index values or projection rows instead of once per row, soWHERE rand() % 2 = 0could return about a quarter of the rows instead of half, or keep or drop whole groups. Closes #89615.Description
On 26.8.10.6,
SELECT count() FROM t WHERE rand() % 2 = 0over a table with asetskip index returns 20106 of 80000 rows instead of about 40000 (39969 withuse_skip_indexes = 0; also 26.3 and 26.9).Root cause. Some index and projection paths compute the query condition on rows that are not the table rows, so a non-deterministic function is drawn per stored value instead of per row:
setindex, on the distinct values of each granule (andcheckDAGUselesscounted a zero-argumentrand()as usable);textindex paths formapKeys(m)andJSONAllPaths(j), once on the default value, checking determinism only on the root;GROUP BYkeys, so whole groups were kept or dropped;INDEX b TYPE basic), on the projection rows before the read computes it again, sob = 5 AND rand() % 2 = 0sampled twice.Change. These paths now refuse a function that is not deterministic within the query (
isDeterministicInScopeOfQuery, asKeyConditiondoes): thesetindex before matching an index column by name,analyzeAggregateProjectionfor keys and filter, andrestrictFilterDAGToInputs, so the projection index still filters byb = 5. Constants folded fromnow(),getSettingorrandConstant()keep pruning. A condition whose only indexed atom calls such a function no longer uses the index, soforce_data_skipping_indicesreportsINDEX_NOT_USEDfor it.minmax,bloom_filter,tokenbf_v1,ngrambf_v1, the primary key and normal projections were measured unaffected.Validation. Two tests check row and group counts within a tolerance (
rand,rand(id),generateUUIDv4, lambdas,if,OR/AND,PREWHERE,DISTINCT,GROUP BY, a set index on a column namedrand()) and index and projection usage inEXPLAIN, next to deterministic controls. On master every count row except the controls fails; 50/50 randomized runs pass.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/122787]
Version info
26.11.1.2167-master(included in26.11and later)