Repository navigation
Look through lossless conversions of the indexed column and of constants in the text index - #120220
Conversation
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 ClickHouse#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 ClickHouse#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: ClickHouse#110018
Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Workflow [PR], commit [f655800] Summary: ✅
AI ReviewSummaryThis PR teaches text-index analysis to look through lossless PR Metadata
Findings
Tests
Final Verdict
LLVM Coverage ReportMeasured on commit f655800.
Changed lines: Changed C/C++ lines covered: 130/137 (94.89%) · Uncovered code |
`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: ClickHouse#120220 Co-Authored-By: Claude Opus 5 <[email protected]>
Ergus
left a comment
There was a problem hiding this comment.
The most important one is the preprocessor result divergence
# 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: ClickHouse#120220 (comment)
Related: ClickHouse#120220 (comment)
Related: ClickHouse#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 ClickHouse#120220 (comment) Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units340 translation units recompiled, 3601 s compile time in total, 340 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.06 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
The `bloom_filter`, `tokenbf_v1` and `ngrambf_v1` indexes now look through `CAST`, `_CAST`, `toNullable` and `toLowCardinality` around the indexed expression when the conversion never changes the value and never throws (adding or dropping `LowCardinality`, adding `Nullable`, at any depth of `Array`), the same as the text index does since ClickHouse#120220. Such wrappers come from explicit casts, from `join_use_nulls` in pushed-down filters, and from casts of typed JSON paths, e.g. `has(['a'], json.Name::Nullable(String))`. `unwrapLosslessConversion` moves from the text index condition to `RPNBuilder` so that all index conditions share it. `toLowCardinality` now declares itself monotonic, like `toNullable`, so `KeyCondition` (primary key, `minmax`) accepts it in the monotonic chain. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Cherry pick #120220 to 26.7: Look through lossless conversions of the indexed column and of constants in the text index
…dexed column and of constants in the text index
Cherry pick #120220 to 26.8: Look through lossless conversions of the indexed column and of constants in the text index
…dexed column and of constants in the text index
Cherry pick #120220 to 26.9: Look through lossless conversions of the indexed column and of constants in the text index
…dexed column and of constants in the text index
Backport #120220 to 26.8: Look through lossless conversions of the indexed column and of constants in the text index
Backport #120220 to 26.9: Look through lossless conversions of the indexed column and of constants in the text index
Backport #120220 to 26.7: Look through lossless conversions of the indexed column and of constants in the text index
Closes: #110018
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Skip indexes (
text,tokenbf_v1,ngrambf_v1) are now used when the indexed column or the searched constant is wrapped intoLowCardinality,Nullableor an equivalentCAST. For example,m['key'] = 'value'on aMap(String, LowCardinality(String))column andhasToken(s, toLowCardinality('x'))previously scanned all granules.Workflow [PR]
Sync PR [sync-upstream/pr/120220]
Version info
26.10.1.1114-master(included in26.10and later)26.9.9.12,26.8.16.18,26.7.20.10