Repository navigation
Strip FixedString padding before extracting text-index search terms - #117314
Conversation
`String = FixedString(N)` ignores the constant's trailing zero padding, but the `text` and `ngrambf_v1` conditions tokenized the raw padded bytes. For an n-gram tokenizer that produces n-grams containing `\0`, which never occur in the stored data, so every granule was pruned and matching rows silently disappeared - at default settings, since `use_skip_indexes` and `enable_full_text_index` are on. Both the `equals` and the prepared-set (`IN`) paths of both index types now strip the padding the way the comparison does. Stripping can only remove terms from the search, never add one, so a `FixedString` indexed column stays sound as well. `MergeTreeIndexBloomFilter.cpp` already does the same for the `bloom_filter` index (#112206). `text(tokenizer = splitByNonAlpha)` and `tokenbf_v1` were unaffected - the zero bytes act as token separators there - and stay covered by the test. Closes: #117021
|
Workflow [PR], commit [2a9f6e1] Summary: ✅
AI ReviewSummaryThis PR strips trailing PR Metadata
Fixes Findings💡 Nits
Final Verdict✅ Approve with one non-blocking metadata nit. LLVM Coverage ReportMeasured on commit 2a9f6e1.
Changed lines: Changed C/C++ lines covered: 79/83 (95.18%) · 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 units8 translation units recompiled, 22 s compile time in total, 8 of them have a recent master baseline. |
|
@groeneai can you address my comments? |
|
@ahmadov I have no commits on What I can add is the measurement behind the thread you marked as a valid concern. Master
Rows 2 and 3 are the same defect in the other direction, and they are wrong on master today: the stored term keeps the padding, the probe does not. A strip cannot reach them, since a One rule covers all four rows: take the width from the indexed column, re-pad the constant to that width when the column has one, and decline index analysis when it does not (a The scope is also wider than n-grams. Same setup, I have the re-pad version implemented and verified locally for |
The stripping was applied unconditionally, but a `FixedString` indexed column is tokenized with its
padding: `text(tokenizer = array)` stores the whole padded value as a single term, so
`s = toFixedString('abc', 5)` would have searched for `abc` while the index holds `abc\0\0` and the
granule holding the matching row would be pruned. Restrict the stripping to the cases where the
terms of the unpadded value are terms of the padded one as well - the tokenizer splits on a zero
byte (`splitByNonAlpha`) or emits substrings (`ngrams`) - or the indexed column is not a
`FixedString`. `ngrambf_v1` and `tokenbf_v1` only ever use those two tokenizers, so their condition
is unaffected.
Also shorten the comments and take the set elements as a `std::string_view` instead of copying them
into a `String`, as asked in the review.
…test Adds the `text(tokenizer = array)` cases on both a `String` and a `FixedString` column - the latter is the one that must keep the padding - and an n-gram index on a `FixedString` column. Every query is paired with the same query at `use_skip_indexes = 0` as the oracle. Table definitions follow the style asked for in the review: one column per line and no explicit `GRANULARITY`, which is redundant because the text index is built for the whole part.
|
🕵 @groeneai thank you for the measurements - rows 2 and 3 of your table reproduce here as well, and the claim in the comment at What is pushed (dac4983, 0c8c2d2) keeps the strip only where it is provably sound: the tokenizer splits on a zero byte ( I did not take the re-pad rule here, because it is only valid for equality-shaped predicates. The same constant flows into So rows 2 and 3 - a |
|
🕵 @groeneai, investigate the failure: |
|
Understood. I will open the follow-up on top of master once this merges, restricted to The I will also re-measure which cases are still wrong against the merged result rather than against my earlier numbers, so the follow-up covers what is left and nothing more. |
|
Both rows are the runner-level guard and both are already owned. The tree in that report predates both fixes. The job log's own Per shard, from the kernel
The live rates agree. Distinct trees carrying the Neither job lost a test: One gap in #117564: a fifth long-lived container in these jobs is a Kafka schema registry, which is not one of those three modules. |
`has`, `mapContainsKey` and `mapContainsValue` compare a `FixedString` constant by its raw padded
bytes - `has(['hello'], toFixedString('hello', 10))` is 0 - and so do `startsWith` and `endsWith`.
The strip ran before the function-specific dispatch, so it rewrote their needles too, and
`text(tokenizer = array)` answers `has` and `mapContains*` by exact direct read, where the index
result replaces the predicate: `optimizeDirectReadFromTextIndex` would have returned rows the
predicate rejects.
Restrict the strip to the functions whose comparison goes through the `String` supertype, which is
what drops the padding. `hasAny` and `hasAll` are among them -
`hasAny(['hello'], [toFixedString('hello', 10)])` is 1, and on master the index returns 0 rows for it
for both `text` and `ngrambf_v1` - so they are fixed rather than excluded. Every remaining supported
function rejects a `FixedString` needle outright with `ILLEGAL_TYPE_OF_ARGUMENT`.
Also add `sparseGrams` to `tokenizerToleratesZeroPadding`: it emits a gram only while consuming the
byte at the gram's right border and hashes nothing past it, so extending a value keeps every gram of
the shorter one and only adds new ones. Without this, `text(tokenizer = sparseGrams(3))` on a
`FixedString` column still tokenized the padded probe and pruned the matching granule. `asciiCJK`
joins the list too; it treats a zero byte as a separator, so there stripping is a no-op.
Add `sparseGrams(3)` over a `FixedString` column, `hasAny`/`hasAll` over `Array(String)` for both `text(tokenizer = array)` and `ngrambf_v1`, and the `has`, `mapContainsKey`, `mapContainsValue`, `startsWith` and `endsWith` cases that must keep the padding. Where the index answers the predicate exactly the oracle adds `query_plan_direct_read_from_text_index = 0`, since `use_skip_indexes = 0` alone does not disable direct read. Every one of the new index queries returns the wrong count before this pull request except the padding-keeping ones, which would start returning rows the predicate rejects if the strip were ever moved back before the dispatch.
- clean up comments - adjust test for better verification
`DataTypeLowCardinality.h` was included twice, which clang-tidy reports as `readability-duplicate-include` and fails the `Build (arm_tidy)` job on master. Caused by: ClickHouse#117314 Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01JyZgVjwycGoE67Jf1wvve6
Conflicts in MergeTreeIndexBloomFilterText.cpp, both regions this branch touches, against three master changes that landed since the last push: FixedString padding stripping (ClickHouse#117314), the LIKE unknown-backslash-escape decline, and FixedString padding stripping for set elements. Resolution keeps both sides: * traverseTreeEquals: the constant is first padding-stripped by master's rule, then const_source_type tracks the type of whatever value survives, so the conversion into the index domain sees the same value the scan compares. The LIKE escape decline runs after both, unchanged. * tryPrepareSetBloomFilter: a set element on a non-string index domain is re-encoded, as before; every other element keeps master's path including the padding strip. A padded FixedString element on a non-string domain fails to convert and declines the atom, which costs pruning and cannot lose rows.
A `FixedString` needle compares equal to a `String` value both with and without its trailing zero bytes, and a term-preserving tokenizer (`array`, or `splitByString` with a separator that does not contain the zero byte) stores those two spellings as two different terms. ClickHouse#117314 strips the needle's padding before extracting the search terms, which fixed the plain row (ClickHouse#117021) and moved the loss onto the row whose own value carries literal zeros: the index looked up a term that granule never stored and pruned it, so the query returned fewer rows than the same query with `use_skip_indexes = 0`. No single lookup covers both spellings, so a needle that really carries padding now leaves the index unusable for that atom, which is what the `FixedString`-column case already did. The decision becomes explicit: strip for a tokenizer that treats the zero byte as a separator, keep the padding for a `FixedString` column whose terms carry it too, and decline for the term-preserving-over-`String` combination - and only when the needle actually has padding to strip, so ordinary needles keep their pruning. The `IN`-set path, which strips per element, follows the same rule.
Cherry pick #117314 to 26.8: Strip `FixedString` padding before extracting text-index search terms
…ng text-index search terms
Cherry pick #117314 to 26.7: Strip `FixedString` padding before extracting text-index search terms
…ng text-index search terms
Backport #117314 to 26.8: Strip `FixedString` padding before extracting text-index search terms
Backport #117314 to 26.7: Strip `FixedString` padding before extracting text-index search terms
05076 and 05077 are already taken by open pull requests ClickHouse#118128 and ClickHouse#118003, and 05081/05082 by ClickHouse#117314/ClickHouse#117318, so a merge of any of them would collide. Co-Authored-By: Claude Opus 5 <[email protected]>
Closes: #117021
Related: #112206
Related: #116407
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a
textorngrambf_v1index silently dropping matching rows when the indexed column iscompared for equality against a
FixedStringvalue, or searched withhasAnyorhasAlloverFixedStringneedles. Happens at default settings, sinceuse_skip_indexesandenable_full_text_indexare both on.Description
'hello' = toFixedString('hello', 10)is true - the comparison casts both sides toString, whichdrops the constant's trailing zero padding. The index conditions did not: they tokenized the raw
padded bytes, so the search terms contained
\0bytes that never appear in the stored data and everygranule looked unmatched. With
index_granularity = 1every matching row is lost.MergeTreeIndexBloomFilter.cppalready coerces such a constant for thebloom_filterindex(
coerceStringFieldLikeSearchFunction, #112206); the text-index paths never got it. BothMergeTreeIndexConditionTextandMergeTreeIndexBloomFilterTextnow strip the padding.Stripping is gated two ways, because it is not sound everywhere.
By function. Only the functions that compare through the
Stringsupertype ignore the padding:equals,notEquals,hasAnyandhasAll, plus the prepared-set (IN) path.has,mapContainsKey,mapContainsValue,startsWithandendsWithcompare the raw padded bytesinstead -
has(['hello'], toFixedString('hello', 10))is 0 whilehasAny(['hello'], [toFixedString('hello', 10)])is 1 - so their terms must keep it. This mattersbecause
text(tokenizer = array)answershasandmapContains*by exact direct read, where theindex result replaces the predicate, so a stripped term would return rows the predicate rejects.
Every remaining supported function rejects a
FixedStringneedle outright withILLEGAL_TYPE_OF_ARGUMENT.By tokenizer, for a
FixedStringindexed column. Its stored terms keep the padding, so theunpadded constant may only be searched when the terms of the unpadded value are terms of the padded
one as well. That holds for tokenizers that split on a zero byte (
splitByNonAlpha,asciiCJK) andfor those that emit substrings (
ngrams,sparseGrams), and not forarray, which stores the wholevalue as a single term, nor for
splitByString, which keeps the padding inside the last one.ngrambf_v1/tokenbf_v1allow onlyngrams,splitByNonAlphaandsparseGrams, so their conditionstrips unconditionally. Only
ngrambf_v1changes behavior:tokenbf_v1is alwayssplitByNonAlpha,which already splits on the padding, so the strip is a no-op there and its cases in the test are
regression coverage that the strip does not disturb them.
A
Stringconstant against aFixedStringindexed column with a tokenizer that keeps the paddinginside a term (
s = 'hello'overFixedString(6)withtext(tokenizer = array)) is wrong on masterfor the same reason, in the other direction. It is left alone here: reconciling it means padding the
constant to the column width, which is only valid for equality-shaped predicates -
startsWith(toFixedString('abcd', 6), 'ab')is true, and a padded needle would make the index prunethe matching granule.
Test
05081_text_index_fixed_string_constantpairs every query with the same query atuse_skip_indexes = 0(orquery_plan_direct_read_from_text_index = 0where the index answers thepredicate exactly) as the oracle. It covers
textwithngrams(3),splitByNonAlpha,arrayandsparseGrams(3)over bothStringandFixedStringcolumns, andngrambf_v1, inequalsandsubquery-
INform;hasAnyandhasAlloverArray(String)for both index types; thehas,mapContainsKey,mapContainsValue,startsWithandendsWithcases that must keep the padding;and asserts an unpadded constant still prunes.
Workflow [PR]
Sync PR [sync-upstream/pr/117314]
Version info
26.9.1.928-master(included in26.9and later)26.8.16.34,26.7.21.4