Skip to content

Add columns token_key and token_value to table function mergeTreeTextIndex - #120183

Merged
CurtizJ merged 13 commits into
ClickHouse:masterfrom
CurtizJ:text-index-keyvaluepairs-decode-token
Sep 22, 2026
Merged

CurtizJ merged 13 commits into
ClickHouse:masterfrom
CurtizJ:text-index-keyvaluepairs-decode-token

Conversation

@CurtizJ

@CurtizJ CurtizJ commented Sep 15, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

The table function mergeTreeTextIndex now returns columns token_key and token_value with the decoded parts of each token for text indexes with the keyValuePairs tokenizer.


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

Version info

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

CurtizJ and others added 2 commits September 15, 2026 10:42
Add `KeyValuePairsTokenizer::decodeToken`, the inverse of `encodeToken`: it reads the
reversed varint trailer backwards and splits a token into its key, value and `is_rest` flag.

Use it in three places that previously printed the raw token with its binary trailer:

- `MergeTreeIndexConditionText::getDescription` shows `"key": "value"` pairs in
  `EXPLAIN indexes = 1`.
- The step description of `ReadFromTextIndexCount` (trivial count from the index) shows
  `token = 'key': 'value'`.
- The table function `mergeTreeTextIndex` gets two additional columns `token_key` and
  `token_value` for indexes with the `keyValuePairs` tokenizer.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Add the virtual `ITokenizer::formatTokenForLogs`. The default renders a token as `"token"`;
`KeyValuePairsTokenizer` overrides it to render the `Map` entry the token encodes: `{"key": "value"}`.

Use it in `MergeTreeIndexConditionText::getDescription` (`EXPLAIN indexes = 1`), in the step
description of `ReadFromTextIndexCount` and in the `Reading tokens` log line, so no call site
branches on the tokenizer type. The trivial count description now quotes plain tokens as
`"alpha"` instead of `'alpha'`, matching the condition description.

In the `mergeTreeTextIndex` table function, `token_key` and `token_value` are filled from a
single `decodeToken` pass per dictionary block instead of decoding every token once per column.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [79b1e18]

Summary: ❌

job_name test_name status info comment
Stress test (amd_tsan) FAIL
Logical error: Chunk should have AggregatedChunkInfo in TotalsHavingTransform. (STID: 3546-33fc) FAIL cidb IGNORED
Upgrade check (amd_release) FAIL
The mutation submitted before the upgrade was not finished by the new server (see mutation_across_upgrade.txt) FAIL cidb IGNORED
Error message in clickhouse-server.log (see upgrade_error_messages.txt) FAIL cidb IGNORED
Performance Comparison (arm_release, master_head, 1/6) FAIL Performance dashboard
order_by_read_in_order #1 FAIL dashboard IGNORED
polymorphic_parts_m #0 unstable query history IGNORED

AI Review

Summary

This PR adds decoded token_key / token_value columns to mergeTreeTextIndex for keyValuePairs indexes and routes token rendering in EXPLAIN / trivial-count descriptions through the tokenizer. The basic feature is implemented and covered by focused tests, but two correctness gaps remain: the table function is still not stable across analysis-time vs execution-time index redefinition, and the new decoded columns assume per-part text-index identity that the read path still does not prove.

Findings

⚠️ Majors

  • [src/TableFunctions/TableFunctionMergeTreeTextIndex.cpp:159-174] [dismissed by author -- https://github.com/Add columns token_key and token_value to table function mergeTreeTextIndex #120183#discussion_r4065945337] getActualTableStructure and executeImpl still resolve the source index independently, and executeImpl ignores cached_columns. ITableFunction::execute will therefore analyze a query against one schema, cache that schema in StorageTableFunctionProxy, and later build the nested storage from a fresh index snapshot. If idx changes from keyValuePairs to a non-keyValuePairs text index between those two moments, a query that was valid at analysis time can reach execution with token_key / token_value still in the cached contract and fail inside the nested read with missing-column / conversion errors instead of preserving the analyzed snapshot or rejecting the drift explicitly.
    Suggested fix: keep the analyzed structure and the runtime MergeTreeIndexText in the same snapshot, or compare cached_columns with the freshly resolved index shape and fail before building the proxy.
  • [src/Storages/StorageMergeTreeTextIndex.cpp:173-289] [dismissed by author -- https://github.com/Add columns token_key and token_value to table function mergeTreeTextIndex #120183#discussion_r4026912621] The new decoded columns assume that every part carrying skp_idx_<name> for the current index name really stores the current keyValuePairs token encoding, but advanceToNextPart still accepts parts purely by file-name existence and fillDecodedTokenColumns unconditionally runs KeyValuePairsTokenizer::decodeToken on every token. The repo already documents the stale-name-reuse gap after a killed DROP INDEX in tests/queries/0_stateless/04165_skip_index_stale_type_after_alter.sql; with this PR, reusing the same text-index name with a different tokenizer can now make mergeTreeTextIndex(...) throw INCORRECT_DATA or return bogus token_key / token_value from stale bytes.
    Suggested fix: prove per-part index identity before decoding (for example by persisting and checking tokenizer/index identity), or reject same-name text-index reuse until stale files cannot survive.
Final Verdict

Status: ⚠️ Request changes

Minimum required actions: make mergeTreeTextIndex analysis and execution use the same index shape (or reject drift before the nested storage is built), and fence token_key / token_value decoding against stale same-name text-index files.

LLVM Coverage Report

Measured on commit 79b1e18.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.30% 79.40% +0.10%

Changed lines: Changed C/C++ lines covered: 130/137 (94.89%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Sep 15, 2026
@CurtizJ CurtizJ changed the title Decode keyValuePairs text index tokens in mergeTreeTextIndex, EXPLAIN and logs Add columns token_key and token_value to table function mergeTreeTextIndex Sep 15, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-text-index Full-text search: inverted/text index in MergeTree (tokenizers, posting lists, GIN-style index bu... label Sep 15, 2026
Comment thread src/Interpreters/ITokenizer.cpp Resolved
CurtizJ and others added 5 commits September 15, 2026 20:49
The "distributed plan" job enables `serialize_query_plan`, with which
`ReadFromTextIndexCount` is not injected into the plan, so the test lost
the "Trivial count from text index" lines. Pin `serialize_query_plan = 0`
like `02346_text_index_optimize_trivial_count` does.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=120183&sha=dbc857f72d2caeff1896a80abe3c6ab815e4b6fb&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20distributed%20plan%2C%20parallel%2C%20selected%20tests%29
PR: ClickHouse#120183

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…n formatting

- `resolveSourceIndex` requires `SHOW TABLES` on the source table, the
  grant that already reveals the index definition in
  `system.data_skipping_indices` and is implied by a grant on any column,
  so `DESCRIBE mergeTreeTextIndex(...)` no longer tells the table, the
  index type or the tokenizer apart without any privilege.
- `executeImpl` derives the columns from the same resolved index object
  the storage reads with (`getTableStructure`), instead of resolving the
  metadata a second time.
- `formatTokenForLogs` quotes with `doubleQuoteString`, so quotes,
  backslashes and control bytes in a token (or in a `keyValuePairs` key
  or value) do not break the `EXPLAIN` and trivial-count descriptions.
- Test 05219 covers the structure-resolution grants; test 05218 covers
  the escaping.

PR: ClickHouse#120183

Co-Authored-By: Claude Fable 5.1 <[email protected]>
`formatTokenForLogs` uses `quoteString`, so a plain token renders as
`'token'` and a `keyValuePairs` token as the `Map` literal
`{'key': 'value'}`; quotes, backslashes and control bytes stay escaped.
Twelve references with `Condition:`, trivial-count or `Reading tokens`
lines are updated accordingly. Test 05218 stores the special-character
pair, because the trivial-count step does not fire for a token that is
absent from every part.

PR: ClickHouse#120183

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…uepairs-decode-token

# Conflicts:
#	src/TableFunctions/TableFunctionMergeTreeTextIndex.cpp
…aster

After the merge, resolving the index also runs
`StorageMergeTreeTextIndex::checkAccess`, so `DESCRIBE` with only
`SHOW TABLES` on the source table is denied like reading is.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@rschu1ze rschu1ze self-assigned this Sep 17, 2026
alexey-milovidov and others added 2 commits September 19, 2026 18:02
Revert the token rendering introduced in
ClickHouse#120183: `formatTokenForLogs`
uses `doubleQuoteString` again, so a plain token renders as `"token"` and a
`keyValuePairs` token as `{"key": "value"}` in `EXPLAIN`, in the description
of `ReadFromTextIndexCount` and in the `Reading tokens` log line. Escaping of
quotes, backslashes and control bytes is unchanged.

Twelve references are updated back. Test 05218 keeps the row with the
special-character pair, because the trivial-count step does not fire for a
token that is absent from every part. Test 04099 keeps the escaped form
`"[\"foo\",\"bar\"]"`: its old reference predates `formatTokenForLogs`, which
renders inner quotes escaped.

Co-Authored-By: Claude Opus 5 <[email protected]>
Comment thread src/Interpreters/ITokenizer.h Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 79b1e1817 with master f59c98752 (stripped binary size, per-symbol sizes and ThinLTO time; 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

12 object files changed (+41.48 KiB total), 0 added.

Object file Master PR Δ
src/TableFunctions/CMakeFiles/clickhouse_table_functions.dir/TableFunctionMergeTreeTextIndex.cpp.o 137.52 KiB 155.43 KiB +17.91 KiB (+13.03%)

716 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

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

Median compile-time ratio to the baselines is ×1.09 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +12.7 s (+10%) in total before that adjustment.

Job report

@CurtizJ
CurtizJ added this pull request to the merge queue Sep 22, 2026
Merged via the queue into ClickHouse:master with commit edc986f Sep 22, 2026
172 of 176 checks passed
@CurtizJ
CurtizJ deleted the text-index-keyvaluepairs-decode-token branch September 22, 2026 21:22
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 22, 2026
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.

4 participants