Repository navigation
Add notHas function - #109926
Add notHas function#109926
notHas function#109926Conversation
|
Workflow [PR], commit [8e936d3] Summary: ✅
AI ReviewSummaryThis PR adds Findings❌ Blockers
Tests
Final Verdict❌ Changes requested: the new negative LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 97/109 (88.99%) · Uncovered code |
|
I'm curious to lean more about how the relaxation propagation is different between having one atom and having two, as I would have naively guessed they'd be the same. Is this something that fixing or reworking relaxedness can fix? |
Currently, if the CREATE TABLE t (x UInt64) ENGINE = MergeTree ORDER BY intDiv(x, 2) SETTINGS index_granularity = 1;
INSERT INTO t SELECT intDiv(number, 4) FROM numbers(24);
SELECT trimLeft(explain) FROM (EXPLAIN indexes = 1 SELECT count() FROM t WHERE x NOT IN (1)) WHERE explain LIKE '%Condition%' OR explain LIKE '%Granules:%/%';
-- Condition: (intDiv(x, 2) notIn 1-element set)
-- Granules: 24/24
SET optimize_rewrite_has_to_in = 0;
SELECT trimLeft(explain) FROM (EXPLAIN indexes = 1 SELECT count() FROM t WHERE NOT has([1], x)) WHERE explain LIKE '%Condition%' OR explain LIKE '%Granules:%/%';
-- Condition: not((intDiv(x, 2) in 1-element set))
-- Granules: 24/24But there is no need to. We can just skip creating atom in such cases because it does not help at all. In that PR #96225, I will add this optimization and that's why need to be able to detect |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 76/82 (92.68%) · Uncovered code |
`KeyCondition` can only build a set atom out of `has(constant_array, key)` - `tryPrepareSetIndexForHas` bails on anything else - so that is the only shape where folding `NOT has(...)` into the single `notHas` leaf buys anything. Folding it unconditionally cost the text index its direct read. The text index condition is built from the canonicalized DAG, so the `notHas` leaf made it `FUNCTION_UNKNOWN`; `alwaysUnknownOrTrue` then kept the index out of `skip_indexes.useful_indices`, and `collectTextIndexReadInfos` builds `text_index_read_infos` from exactly that list, so `optimizeDirectReadFromTextIndex` had nothing left to rewrite. Measured against a master binary, `SELECT max(id) FROM t WHERE NOT has(arr, 'w3')` stopped reading the index through `__text_index_idx_has_...` entirely. Granule counts were never affected: a negated containment atom can never prune a bloom filter or a text index. `has` pushes `BoolMask(may_be_present, /* can_be_false */ true)`, so `not(has(...))` pushes `BoolMask(true, may_be_present)` - `can_be_true` is unconditionally true, exactly like an unrecognized atom - and `can_be_true` of `and`/`or` never reads `can_be_false`.
Covers the secondary index paths outside the primary key one: `bloom_filter`, `tokenbf_v1` and `text` with the array tokenizer, with the direct read from the text index both off and on. Asserts that `has`, `NOT has` and `notHas` agree with each other and with `use_skip_indexes = 0`, that the granule counts are unchanged, that a positive sibling in the same conjunction still prunes next to the negated atom, and that `NOT has` still reaches the text index through its virtual column.
Master is `v26.8.1.1-new` and `26.7` is already branched, so the version this function first appears in is 26.8, not 26.7.
`notHas` takes the same arguments as `has`, so it belongs in the containment group the fuzzer swaps function names within.
|
🕵 The major finding is real, and it is now fixed. I measured it instead of reasoning about it: I ran the same skip-index queries against a master binary ( The chain is a bit longer than "the analyzers only whitelist Granule counts are a different story: a negated containment atom can never prune a bloom filter or a text index. So the fix is scoped to where the fold actually buys something: Added One thing I did not fold in: a user-written Also merged Two small follow-ups in the same push: Locally: the 5 reference tests this PR updates, |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Object file sizes6 object files changed (+177.61 KiB total), 1 added.
737 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 units11 translation units recompiled, 41 s compile time in total, 10 of them have a recent master baseline. |
Correct the expected result for tuple containment with `NaN` in `04509_not_has_function`. CI: ClickHouse#109926
Preserve `NULL` set members for nullable primary keys in `tryPrepareSetColumnsForIndex` and cover `notHas` and `NOT IN` pruning.\n\nhttps://github.com/ClickHouse/pull/109926
Construct the nullable set column with mutable columns and use the data type nullability predicate. This resolves the `KeyCondition.cpp` build failures reported by CI.\n\nhttps://github.com/ClickHouse/pull/109926
Treat `Nothing` array elements as source NULLs when preparing a set index for a nullable primary key. This keeps the `notIn` atom for `notHas([NULL], key)` instead of mistaking the values for failed casts. ClickHouse#109926
|
|
||
| const DataTypePtr & array_nested_type = array_data_type->getNestedType(); | ||
|
|
||
| /// `has` uses accurate equality for array elements, while MergeTreeSetIndex compares floating-point |
There was a problem hiding this comment.
This new notHas / folded NOT has fast path still treats float/NaN as the only semantic mismatch, but constant-array has diverges from set-style casting for several non-float type pairs too. RewriteHasToInPass already has that broader guard (DateTime vs Date, String vs Enum, IPv4 vs UInt32, and similar), because has on a constant array compares raw Fields rather than the casted key type.
Here we go straight into tryPrepareSetColumnsForIndex, which will retain representable values and then expose notHas(..., key) as FUNCTION_NOT_IN_SET. On a keyed Enum8('a' = 1, 'b' = 2) column, for example, notHas(['a'], x) should be true for every row (the same constant-array semantics already covered by 03916_has_to_in), but this path casts 'a' to the enum value and prunes the x = 'a' granule as if the predicate were x NOT IN ('a').
I think tryPrepareSetIndexForHas needs the same type-compatibility gate as RewriteHasToInPass before it builds any set atom: exact same non-float type after stripping LowCardinality, or both native integers.
There was a problem hiding this comment.
This can be addressed separately.
There was a problem hiding this comment.
🕵 Fixed in #115233. KeyCondition now uses a set atom only when the constant-array element and primary-key types share raw Field equality semantics; the regression covers String elements against an Enum8 key.
Master's notHas work (PR ClickHouse#109926) changed two behaviours this suite pinned, and both changes are master's own, measured identical on an A/B where the only difference is this branch's KeyCondition.cpp: - 947f172 / 495d342 refuse a set atom for any array with a floating-point element, because has() uses accurate equality while MergeTreeSetIndex compares with compareAt. The `Point` cell was written when the outer custom name decided the outcome; the float ban now decides it first, so the cell asserts the decline instead of the atom. - 6f549ee / 1418e54 preserve NULL set members for a nullable key, so the two `lossy ... NOT has` oracle columns agree with the Memory oracle where they previously diverged. No source change. Both cells were re-verified as live oracles by mutating them and confirming the file reddens, and the suite's arm discipline is unchanged: 7 of the 10 family files still fail on a pristine-master binary, and the 3 that pass hold only agreement controls, each proven live by the same mutation check.
…-explain-tests Resolve the KeyCondition.cpp conflict in tryPrepareSetColumnsForIndex. Master's ClickHouse#109926 rewrote the Nullable-source block and now handles the non-Nullable-key case this branch had addressed on its own, in a strictly more general form: it also preserves a NULL set member for a Nullable key and treats a Nothing element as a source NULL. Master's version of that block is taken whole, and the branch's own source-NULL filtering, which master subsumes, is dropped. What this branch still contributes is the equality-preservation of the set conversion, which master does not cover: keyToSetConversionMayCollapse declines an atom whose runtime key-to-set cast can map two distinct keys onto one set value, and set_is_approximate relaxes an atom whose forward set-to-key cast is not equality-preserving. Both are kept, rebased onto master's variable names, and the approximate flag is now raised outside the per-row loop next to master's source_null_map handling. Master independently updated 03733_has_function_key_condition_explain to the same expected set sizes this branch had produced, so that reference no longer differs.
This is needed to bridge the gap between
NOT INandNOT has()during index analysis. Currently,NOT INbecomesnotIn(a single atom) butNOT has()becomesnot(has())(two atoms); as a result, relaxation propagation is weaker and we will prune less in some cases after #96225. This PR allowsNOT INandNOT has()to be later made symmetric to index analysis.Adds the function
notHas(haystack, needle)— the negation ofhasfor arrays, maps, and JSON. It is computed asnot(has(haystack, needle)).RewriteHasToInPass(optimize_rewrite_has_to_in, enabled by default) now also rewritesnotHas(constant_array, x)tonotIn(x, constant_array), under the same safety guards as the existinghastoinrewrite (constant NULL-free array, no Nullable/LowCardinality needle, types that compare identically underhasandIN).During index analysis,
NOT has(constant_array, key)is folded into the singlenotHasleaf, which is what makes it symmetric withnotIn.NOT has(column, needle)is deliberately left asnot(has(...)):KeyConditioncannot build a set atom out of that shape anyway, while the text index and the bloom filter index analyzers understandhasbut notnotHas, so folding it would cost them the atom - and, for a text index, the direct read from the index.When the rewrite does not apply,
notHasexecutes as-is with fullhassemantics.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Add function
notHas, the negation ofhasfor arrays, maps, and JSON. When the haystack is a constant array,notHas(constant_array, x)is rewritten tox NOT IN constant_arraybyoptimize_rewrite_has_to_in(enabled by default), so it executes via a set lookup and can prune by the primary key index likeNOT IN.Version info
26.8.1.1576(included in26.8and later)