Skip to content

Cherry pick #120220 to 26.7: Look through lossless conversions of the indexed column and of constants in the text index - #123233

Merged
robot-clickhouse-ci-1 merged 9 commits into
backport/26.7/120220from
cherrypick/26.7/120220
Oct 1, 2026
Merged

robot-clickhouse-ci-1 merged 9 commits into
backport/26.7/120220from
cherrypick/26.7/120220

Conversation

@robot-ch-test-poll3

Copy link
Copy Markdown
Contributor

Original pull-request #120220

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

CurtizJ and others added 8 commits September 15, 2026 14:11
The analyzer wraps the indexed expression into `_CAST`, `toNullable` or
`toLowCardinality` in several cases: `optimize_functions_to_subcolumns`
rewrites `m['key']` on `Map(K, LowCardinality(V))` into
`_CAST(m.key_<key>, 'V')`, and `join_use_nulls` pushes filters down as
`toNullable(column)`. `MergeTreeIndexConditionText` matched the wrapped
expression by name and declined, so the text index was not used.

Strip such conversions in `traverseFunctionNode` and in the `IN` path
while they cannot change the value or throw: adding or dropping
`LowCardinality` and adding `Nullable`, at any depth of `Array`.
Dropping `Nullable` is not looked through because the cast throws on
NULL. Serializing casts such as `FixedString` to `String` or
`Array(String)` to `String` change the bytes the tokenizer sees, so
`canBeSafelyCast` is deliberately not used here.

Also unwrap a `Nullable` set column in the `IN` path and decline sets
that contain NULL.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
`RPNBuilderTreeNode::tryGetConstant` now returns the type of the value
the constant holds: `LowCardinality` is removed, and `Nullable` is
removed when the value is not NULL. A `Field` is the plain value, so
every consumer of `tryGetConstant` (`KeyCondition`, the text index, the
bloom filter indexes, the selectivity estimator) sees `String` for
`toLowCardinality('x')` and `CAST('x', 'LowCardinality(Nullable(String))')`.
The per-consumer stripping added by #110017
in `MergeTreeIndexConditionText` and `MergeTreeConditionBloomFilterText`
is removed, together with the special case that made a `LowCardinality`
`hasToken` needle with a separator behave differently from a `String` one.

`MergeTreeIndexConditionText::traverseMapElementKeyNode` accepts a
predicate result of `Nullable(UInt8)` or `LowCardinality(UInt8)`, which a
`Nullable` map key produces; NULL reads as false, as in WHERE. The
constant key is read through `tryGetConstant` instead of unwrapping the
column by hand. This supersedes #110031.

The absent-key guards in `MergeTreeConditionBloomFilterText` compare the
constant with its `FixedString` padding stripped. `FixedString` reports
'' as its default while the constant holds N zero bytes, so
`m['missing'] = CAST('', 'FixedString(N)')` used the `mapKeys` index and
returned no rows instead of all rows.

`RPNBuilderTreeNode` holds a const reference to the tree context, so a
node can be built from a const function node.

Closes: #110018

Co-Authored-By: Claude Fable 5.1 <[email protected]>
`clickhouse-test` randomizes `query_plan_convert_outer_join_to_inner_join`
to `false` in 5% of the runs. The filter is pushed below the join only
when the NULL-rejecting `WHERE` turns the `LEFT JOIN` into an `INNER`
one; with the conversion disabled the `Filter` step stays above the join,
nothing reaches `ReadFromMergeTree` and no index is consulted, so the
`EXPLAIN indexes` count was 0 instead of 1.

Pin the setting on that query. The case is still the one this change is
about: with the conversion enabled the pushed-down filter arrives as
`hasToken(toNullable(s), 'network')`, so the conversion of the indexed
column is what has to be looked through.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120220&sha=ad41b843541a878cc0821080d3db7cfda2f739dd&name_0=PR&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20targeted%29
Related: #120220

Co-Authored-By: Claude Opus 5 <[email protected]>
# Conflicts:
#	src/Storages/MergeTree/MergeTreeIndexConditionText.cpp
…versions

The text index is analyzed on the expression under lossless conversions of the
haystack, e.g. `s` in `hasToken(toNullable(s), 'Foo')`, and applies its
preprocessor to the needle. The row-level rewrite in `processTextIndexFunction`
matched the wrapped haystack against the preprocessor expression, found no
match and kept the case-sensitive function, so the result depended on
`query_plan_direct_read_from_text_index` and on whether a part had the index
materialized. Unwrap the conversions there too, sharing the definition of a
lossless conversion with the index analysis.

Also add a test for the `bloom_filter` index on `mapKeys` / `mapValues`
compared with `CAST('' AS LowCardinality(Nullable(String)))`: taking the
default of the constant's value type instead of its declared type makes the
index decline, as it must for the default value.

Related: #120220 (comment)
Related: #120220 (comment)
Related: #120220

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…-lossless-conversion

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Addresses review comment #120220 (comment)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…nversion

Look through lossless conversions of the indexed column and of constants in the text index
@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 1, 2026
…olve-cp/26.7/120220

# Conflicts:
#	src/Processors/QueryPlan/Optimizations/optimizeDirectReadFromTextIndex.cpp
#	src/Storages/MergeTree/MergeTreeIndexBloomFilterText.cpp
#	src/Storages/MergeTree/MergeTreeIndexConditionText.cpp
#	tests/queries/0_stateless/04365_token_index_lowcardinality_constant.reference
#	tests/queries/0_stateless/04365_token_index_lowcardinality_constant.sql
@robot-clickhouse-ci-1
robot-clickhouse-ci-1 merged commit 555a27d into backport/26.7/120220 Oct 1, 2026
1 check passed
@robot-clickhouse-ci-1
robot-clickhouse-ci-1 deleted the cherrypick/26.7/120220 branch October 1, 2026 22:30
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