Repository navigation
Do not build bloom_filter index atoms for negated functions - #116544
Conversation
A bloom_filter skip index whose condition consists only of negated atoms
(v != c, v NOT IN (...)) was put into the useful index set and read in
full, and then skipped zero granules, so the whole index read was pure
overhead.
Measured on the reporter's fixture (2000000 rows, one bloom_filter(0.01)
index), WHERE v != <absent> read 2530271 compressed bytes with the index
and 19036 with ignore_data_skipping_indices, at identical SelectedMarks
and read_rows. The 2511235 byte difference is the compressed size of the
index to the byte.
Cause: mayBeTrueOnGranule pushes BoolMask(match_rows, true) for every
atom and then negated it for FUNCTION_NOT_EQUALS and FUNCTION_NOT_IN.
BoolMask::operator! is {can_be_false, can_be_true}, so a negated atom
evaluated to can_be_true = true unconditionally and the function, which
returns rpn_stack[0].can_be_true, could never return false. That is
mathematically unavoidable: a bloom filter proves a value is absent from
a granule, never that every row of the granule equals it. The index
nevertheless still built those atoms and alwaysUnknownOrTrue counted
them as useful, so the index was selected and read.
notEquals, notIn and globalNotIn are now declined at condition
construction, so RPNBuilder emits FUNCTION_UNKNOWN, the condition is
always-unknown-or-true and the index is not selected. The two atom kinds
then have no producer and are removed. This is the shape the supported
text index already has after d129056,
which removed the same two atoms from MergeTreeIndexConditionText.
No pruning is lost, because the condition provably could not drop a
granule. A negated atom beside a positive one still prunes. The only
user-visible change is that force_data_skipping_indices now reports
INDEX_NOT_USED for a purely negated condition, which is already how the
text index behaves.
tokenbf_v1 and ngrambf_v1 carry the identical atoms and are left
untouched because they are deprecated, per the scope ruling on the
issue. NOT has(column, needle) has the same symptom and is also left
alone; 04903_not_has_secondary_index pins that behaviour.
Nine query lines in 02021_map_bloom_filter_index and
03990_map_subcolumn_skip_index asserted the old behaviour and now expect
INDEX_NOT_USED, each followed by the same query without the setting so
their row-level assertions are preserved.
Closes: ClickHouse#116470
Co-Authored-By: Claude Opus 5 <[email protected]>
…ntracts The in-source Functions support matrix in registerStorageMergeTree.cpp, the source of truth for the generated MergeTree documentation, now marks the bloom_filter column of the notEquals and notIn rows with a cross, which is what the note below that same table already said in prose by listing s != 1 under "Can not be optimized". The generated mergetree.mdx mirror sits inside an AUTOGENERATED region and is refreshed by the nightly docs generator, not here. Three contracts were argued from the source but never executed, so they get arms in 05055: - A negated atom inside a cross-index disjunction. Only the index whose atoms are all negated leaves the useful set, the other index still enters it from the same disjunction, and the results are unchanged. This arm reddens on the parent commit, where the negated atom reported can_be_true and the OR therefore kept the index. - A positive disjunction still prunes by combining both indexes, 8 of 16 granules. It pins use_skip_indexes_for_disjunctions and use_skip_indexes_on_data_read at query level because clickhouse-test randomizes both, and it reports 16 with combining off. - A doubly negated condition, which the documentation promises can be optimized. That promise now rests on the inversion pushdown folding not(notEquals()) back to equals, so this is a preservation arm and passes on the parent commit too. A throwaway canFoldToInverseRelation mutant that declines the fold for notEquals, notIn and globalNotIn turns both lines into INDEX_NOT_USED, which is what proves they pin the fold. The third sentence of the comment at the notIn decline is dropped: it described where the check sits, not the property that makes the check correct. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex, two passes, 4 findings) plus my own
Severity: ❌ blocker / Also checked and clean: the affected-test set was enumerated independently and reproduces exactly Session id: cron:clickhouse-review-slot-10:20260826-144000 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20260826-134200 |
|
cc @rschu1ze @alesapin, could you review this? A |
|
@groeneai the solution is to remove the index support for the negated functions? Are you sure it cant' be applied? |
|
Yes, that is the change, and yes, I am sure. What it removes is two atoms that could never decide a granule, not any pruning. A bloom filter answers set membership. To drop a granule for Measured before the change, 2M rows, Nothing prunable is lost. A negated atom beside a positive one still prunes, The shipped documentation already said this before the PR: the The source change is +12/-22 over three files, of which One sibling call is yours: |
|
Workflow [PR], commit [f5cc4ce] Summary: ✅
AI ReviewSummaryThis PR removes useless Findings💡 Nits
Final VerdictOne metadata fix is still needed before merge: do not auto-close LLVM Coverage ReportMeasured on commit f5cc4ce.
Changed lines: Changed C/C++ lines covered: 60/61 (98.36%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units9 translation units recompiled, 31 s compile time in total, 9 of them have a recent master baseline. |
|
📊 Cloud Performance Report ✅ AI verdict: This PR only changes how the MergeTree bloom_filter skip index handles negated predicates (!= and NOT IN), making such conditions no longer select the index, plus docs and tests. None of the three flagged ClickBench queries use a bloom_filter skip index or a negated predicate: Q28, Q33, and Q34 are full-scan GROUP BY aggregations, so the changed code path is never exercised. The 6-7% deltas are consistent-looking but off-path (Q28 is intrinsically noisy and Q34 sits on a pre-existing master trend), so all three are downgraded to not_sure as run-to-run variance rather than PR effects. No cross-query pattern ties these to the diff. clickbenchFlagged queries (3 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
CI finish ledger - 86fa6d7Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
Not PR-caused: this PR touches Session id: cron:our-pr-ci-monitor:20260828-020000 |
|
This PR looks like @nihalzp should check :-) |
| | [multiMatchAny](/reference/functions/regular-functions/string-search-functions#multiMatchAny) | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | ✔ | | ||
| | [in](/reference/functions/regular-functions/in-functions) | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | | ||
| | [notIn](/reference/functions/regular-functions/in-functions) | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ | ✗ | | ||
| | [notIn](/reference/functions/regular-functions/in-functions) | ✔ | ✔ | ✔ | ✔ | ✗ | ✔ | ✗ | |
There was a problem hiding this comment.
Please don't auto-close #116470 from this PR. That issue tracks the same useless negated-predicate index reads for tokenbf_v1 and ngrambf_v1 too, and this diff intentionally leaves those implementations alone, so Closes: would hide still-open work. Related: here (or a follow-up issue for the remaining text bloom-filter families) would match the actual scope.
There was a problem hiding this comment.
Changed: the relationship line now reads Related: in both the linked-issues block and the description, so merging this no longer auto-closes the issue.
Checked before changing it: the issue reports the same overhead for tokenbf_v1 and ngrambf_v1 with their own measured 245 / 245, and this diff touches only MergeTreeIndexBloomFilter.{cpp,h}, so those two index types genuinely stay open.
I am not taking the follow-up-issue alternative. @ PedroTadim scoped the fix to bloom_filter on the issue, because tokenbf_v1 and ngrambf_v1 are deprecated. sparse_grams is not deprecated and carries the identical atoms, but all three text-family types share MergeTreeConditionBloomFilterText through bloomFilterIndexTextCreator, so declining the atoms there cannot be scoped away from the deprecated pair. The description records that residual.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
A
bloom_filterskip index is no longer selected for a condition built only from!=orNOT IN. A bloom filter can only prove that a value is absent from a granule, so such a condition never skipped a granule while the whole index was still read.force_data_skipping_indicesnow reportsINDEX_NOT_USEDfor it.Description
Related: #116470
A
bloom_filterindex whose condition is only negated atoms was put into the useful index set and read in full, then skipped zero granules.Measured on the reporter's own fixture (2M rows, one
bloom_filter(0.01)index),WHERE v != <absent>:SelectedMarksread_rowsReadCompressedBytesThe 2511235 byte saving is the compressed size of the index to the byte, and no pruning is lost because the condition provably could not drop a granule. Read amplification only, never wrong results.
Cause: every atom pushes
BoolMask(match_rows, true), which was then negated for the two negated atom kinds. Since!BoolMaskis{can_be_false, can_be_true},can_be_truebecame unconditionally true, so the granule check could never return false.notEquals,notInandglobalNotInare now declined at condition construction, so the atoms have no producer and are removed. This is the shape the supportedtextindex already has afterd12905698ae. The in-source documentation matrix is flipped to match.Siblings checked. A negated atom beside a positive one still prunes (
v = 3 AND v != 100is an arm of the new test).equals,IN,hasandindexOfare unaffected, as are cross-index disjunctions. No data type is inspected, so type wrappers are structurally irrelevant.minmaxkeeps its negated atoms, correctly, because a range answers a negated condition exactly.Three residuals I am not changing:
NOT has(column, needle)has the same symptom and stays as04903_not_has_secondary_indexpins it;tokenbf_v1/ngrambf_v1carry the identical atoms but are deprecated, per @ PedroTadim's scope ruling on the issue; andsparse_grams, which is not deprecated, shares their condition implementation, so declining the atoms there cannot be scoped to it alone.Workflow [PR]
Sync PR [sync-upstream/pr/116544]