Skip to content

Cherry pick #117314 to 26.7: Strip FixedString padding before extracting text-index search terms - #123480

Merged
robot-clickhouse merged 10 commits into
backport/26.7/117314from
cherrypick/26.7/117314
Oct 2, 2026
Merged

robot-clickhouse merged 10 commits into
backport/26.7/117314from
cherrypick/26.7/117314

Conversation

@robot-ch-test-poll

Copy link
Copy Markdown
Contributor

Original pull-request #117314

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

alexey-milovidov and others added 9 commits August 31, 2026 10:39
`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
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.
`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
…g-padding

Strip `FixedString` padding before extracting text-index search terms
@robot-ch-test-poll robot-ch-test-poll added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Oct 2, 2026
@CurtizJ CurtizJ self-assigned this Oct 2, 2026
…olve-cp/26.7/117314

# Conflicts:
#	src/Storages/MergeTree/MergeTreeIndexBloomFilterText.cpp
#	src/Storages/MergeTree/MergeTreeIndexConditionText.cpp
@robot-clickhouse
robot-clickhouse merged commit 507104c into backport/26.7/117314 Oct 2, 2026
2 checks passed
@robot-clickhouse
robot-clickhouse deleted the cherrypick/26.7/117314 branch October 2, 2026 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants