Skip to content

Cherry pick #118972 to 26.8: Decline a text-index lookup that cannot cover a padded needle - #123995

Merged
robot-clickhouse-ci-1 merged 9 commits into
backport/26.8/118972from
cherrypick/26.8/118972
Oct 6, 2026
Merged

robot-clickhouse-ci-1 merged 9 commits into
backport/26.8/118972from
cherrypick/26.8/118972

Conversation

@robot-ch-test-poll3

Copy link
Copy Markdown
Contributor

Original pull-request #118972

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 8 commits September 9, 2026 07:35
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. #117314
strips the needle's padding before extracting the search terms, which
fixed the plain row (#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.
…es a matching value

The padding decision was keyed on `ITokenizer::Type`, which is too coarse for
parameterized tokenizers: `splitByString(['\0'])` tolerates the padding while
`splitByString([','])` does not, and both fell into one bucket. The decision
now runs on the concrete tokenizer instance with its pre- and postprocessor:
the stripped needle is tokenized next to its padded spellings (one trailing
zero byte and the padding it really carries), and stripping is sound when the
terms of the stripped needle survive in the padded spellings, since `equals`
only requires all needle terms to be present in the granule. This keeps
`ngrams` pruning too, whose grams of a prefix are grams of the whole.

Probing the comparison semantics along the way corrected two assumptions:

- `equals` and `IN` with a `FixedString` on either side drop the trailing
  zeros of both sides, so a `String` value spelled with any number of trailing
  zero bytes matches, not just two spellings. `hasAny` and `hasAll` instead
  cast the needle to `String`, which drops its padding, and compare the value
  as it is, so only the stripped spelling matches and one lookup always
  covers it; they no longer decline.

- A `FixedString(N)` indexed column stores every value padded to N, and so do
  its terms, but the comparison strips both sides, so the only value that
  compares equal to a needle is the needle without its own padding re-padded
  to N. The previous code kept the needle as typed, which looked up a term
  the index never stored for a plain `String` needle or a `FixedString` needle
  of another width and silently dropped the matching rows:

      FixedString(6) column, tokenizer = array
      s = 'hello'                      truth [1,3]   with the index []
      s = toFixedString('hello', 10)   truth [1,3]   with the index []

  The needle is now re-padded to the column width, and a needle longer than
  the column declines the index. The `IN` set path follows the same rule and
  derives the element type from the set column.

The regression test now builds the index on the `Array(String)` column so the
`hasAny` and `hasAll` predicates really reach it, asserts pruning versus
declining with `force_data_skipping_indices`, and covers `splitByString` with
and without the zero byte as a separator, the `FixedString` column with
`String`, same-width, wider and too-long needles, and `Array(FixedString)`.
Renumbered to 05223 since 05167 was taken on master.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
- Add FixedStringPaddingSemantics and FixedStringNeedleContext data
  structures to make the implementation simpler.
- Rename functions to match ClickHouse naming style.
We might need to modify the internal data of `FixedString`,
however for other cases we can avoid copy.
…18669

Decline a text-index lookup that cannot cover a padded needle
@robot-ch-test-poll3 robot-ch-test-poll3 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 5, 2026
…onflicts

Conflicts in `MergeTreeIndexConditionText.cpp`:
- includes: keep both `DataTypeFixedString.h` and `DataTypeLowCardinality.h`.
- `traverseMapElementKeyNode`: take the release branch version, which reads the key via `tryGetConstant` (the cherry-picked change only reordered the code it replaces).
- `tryPrepareSetForTextSearch`: take the new `FixedString` padding normalization, but detect a `FixedString` set on `set_column_values` (the non-nullable nested column), as on master.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robot-clickhouse-ci-1
robot-clickhouse-ci-1 merged commit ad7cdef into backport/26.8/118972 Oct 6, 2026
1 check passed
@robot-clickhouse-ci-1
robot-clickhouse-ci-1 deleted the cherrypick/26.8/118972 branch October 6, 2026 02:33
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.

4 participants