Skip to content

Answer m['key'] IN (...) from a keyValuePairs text index - #120010

Merged
CurtizJ merged 15 commits into
ClickHouse:masterfrom
Ergus:map-kv-in-support
Oct 11, 2026
Merged

CurtizJ merged 15 commits into
ClickHouse:masterfrom
Ergus:map-kv-in-support

Conversation

@Ergus

@Ergus Ergus commented Sep 14, 2026 •

Copy link
Copy Markdown
Member

Related: #115579

Follow-up to #115579 IN on a map element is the same lookup
repeated: every value of the list becomes one (key, value) token, and the union
of their posting lists is exactly the set of matching rows, so the predicate is
answered by the index alone (exact direct read) instead of by a brute-force scan.
An OR chain of equals benefits too, since the analyzer folds it into IN.

Implemented as a hard partition in tryPrepareSetForTextSearch, so the generic
per-element path — which tokenizes set elements as strings and cannot produce a
pair token — is unreachable for this tokenizer.

These cases keep the predicate and fall back to a scan, because a token union
cannot answer them or the tokens would not match what execution compares:

  • an empty string in the list, since m['key'] = '' also holds for rows that
    lack the key and therefore have no token;
  • a tuple left-hand side, (m['key'], id) IN (('error', 1));
  • a FixedString element, whose zero padding IN compares as part of the value;
  • NOT IN and nullIn (the form produced with transform_null_in = 1), as for
    every other tokenizer.

A set from a subquery still skips granules, but gets no direct read: a non-literal
set has no AST representation for the default expression of a part where the index
is not materialized.

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

A text index with the keyValuePairs tokenizer now answers map['key'] IN ('value1', 'value2') directly from the index, as it already did for map['key'] = 'value'. An IN list containing the empty string, a tuple left-hand side or a FixedString element still falls back to a brute-force scan.


Workflow [PR]
Sync PR [sync-upstream/pr/120010]

Version info

  • Merged into: 26.10.1.2101-master (included in 26.10 and later)

@clickhouse-gh

clickhouse-gh Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9fec73c]

Summary: ✅


AI Review

Summary

This PR teaches the keyValuePairs text index to answer map['key'] IN (...) from pair tokens, and the current revision does a good job covering the obvious correctness traps locally: mutable ENGINE = Set right-hand sides, empty strings, tuple left-hand sides, FixedString elements, duplicate keys, LowCardinality, and parts where the index is not materialized. One important contract break still remains in distributed-plan / worker execution: once an IN (SELECT ...) set is shipped as tuple values, the new exact-rewrite path can no longer tell that it came from a subquery.

Findings
  • ⚠️ Majors
  • [src/Storages/MergeTree/MergeTreeIndexConditionText.cpp:2281] [dismissed by author -- https://github.com/Answer m['key'] IN (...) from a keyValuePairs text index #120010#discussion_r4073135268] The new keyValuePairs fast path is gated only on rhs.tryGetPreparedSet() plus buildOrderedSetInplace, so a worker that receives a ready subquery set as TupleValues (src/Processors/QueryPlan/SetsSerialization.cpp:124, src/Processors/QueryPlan/SetsSerialization.cpp:288) sees a plain FutureSetFromTuple instead of FutureSetFromSubquery. convertSetColumnToAST then reconstructs that carrier as a literal tuple (src/Processors/QueryPlan/Optimizations/optimizeDirectReadFromTextIndex.cpp:300, src/Processors/QueryPlan/Optimizations/optimizeDirectReadFromTextIndex.cpp:977), which re-enables exact __text_index_* rewriting even though the PR's contract says subquery sets never direct-read and use_index_for_in_with_subqueries = 0 should disable index use for them. Please preserve the set origin through serialization or restrict the new exact rewrite to sets that were literal from the start.
Final Verdict

Needs one more fix before merge: the local IN optimization looks solid, but distributed / worker planning still loses subquery origin and can violate the advertised IN (SELECT ...) behavior.

LLVM Coverage Report

Measured on commit 9fec73c.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 91.80% 92.10% +0.30%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 49/53 (92.45%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements comp-text-index Full-text search: inverted/text index in MergeTree (tokenizers, posting lists, GIN-style index bu... labels Sep 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9fec73c8f with master 83c1e5c74 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of e73771ba3; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

28 object files changed (+66.96 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Processors/Transforms/MergeJoinTransform.cpp.o 320.70 KiB 356.36 KiB +35.67 KiB (+11.12%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

31 translation units recompiled, 310 s compile time in total, 31 of them have a recent master baseline.

Job report

@rschu1ze rschu1ze self-assigned this Sep 28, 2026
@Ergus
Ergus requested a review from rschu1ze October 5, 2026 13:06

@CurtizJ CurtizJ left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines +2280 to +2282
/// The generic path below tokenizes elements as strings, which never yields a pair token.
if (tokenizer->getType() == ITokenizer::Type::KeyValuePairs)
return traverseMapElementKeyValueSetNode(lhs, rhs, function_name, out);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to support that optimization for the array tokenizer as well. Maybe in a follow-up PR.

@CurtizJ
CurtizJ added this pull request to the merge queue Oct 11, 2026
@CurtizJ CurtizJ self-assigned this Oct 11, 2026
Merged via the queue into ClickHouse:master with commit 2699c3e Oct 11, 2026
152 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-text-index Full-text search: inverted/text index in MergeTree (tokenizers, posting lists, GIN-style index bu... pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants