Skip to content

Do not pass a text-index virtual column to the on-fly mutation interpreter - #117277

Merged
tiandiwonder merged 4 commits into
masterfrom
fix-text-index-on-fly-mutation
Sep 4, 2026
Merged

tiandiwonder merged 4 commits into
masterfrom
fix-text-index-on-fly-mutation

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

Closes: #116832

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix NO_SUCH_COLUMN_IN_TABLE for every query using a text index while a mutation is pending with
apply_mutations_on_fly = 1. Until the mutation materialized, hasToken, hasAnyTokens and
hasAllTokens on the indexed column could not be executed at all.

Description

query_plan_direct_read_from_text_index (default on) rewrites the search predicate into a column
synthesized by the index reader, which produces it instead of reading the column data, for example
__text_index_idx_hasToken_<hash>. When a part has on-fly mutations to apply,
MergeTreeReadPoolBase::buildReadTaskInfo passes the query's read column list to
AlterConversions::getMutationSteps, whose MutationsInterpreter resolves those names against the
storage snapshot in prepareMutationStages — and StorageSnapshot::getColumn throws for a name that
is not a table column.

The trigger is any pending on-fly mutation whose stage has to carry the query's read columns, not
DELETE specifically: a DELETE adds a filter stage, and an ALTER UPDATE that
AlterConversions::filterMutationCommands keeps — because its assignment target is one of the read
columns — does the same. Both spellings throw the identical exception without this fix (verified
against a binary built without it).

The mutation steps read the real columns their own expressions need, and the index column is produced
on top of their output, so it is filtered out of the list handed to them. The names to filter out are
taken from index_read_tasks, which is where the index reader's columns were synthesized, rather
than from a test of the __text_index_ name prefix: that prefix is not reserved, so a table may
declare a column of its own with such a name, and dropping a declared column here would hide it from
filterMutationCommands, which would then discard a pending ALTER UPDATE of it while the reader
still reads the column — answering from stale on-disk values.

Test 05059_text_index_direct_read_pending_mutation covers hasToken, hasAnyTokens and
hasAllTokens against the three settings that used to be the only way to make the query run
(query_plan_direct_read_from_text_index = 0, use_skip_indexes = 0,
apply_mutations_on_fly = 0), the same query after the mutation materializes, the pending
ALTER UPDATE spelling, and a table declaring a physical __text_index_* column of its own under a
pending ALTER UPDATE of it.


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

Version info

  • Merged into: 26.9.1.670 (included in 26.9 and later)

…reter

A direct read from a text index rewrites the search predicate into a virtual column produced by
the index reader (`__text_index_*`). With `apply_mutations_on_fly` and a pending `ALTER DELETE`,
that name was included in the column list handed to `AlterConversions::getMutationSteps`, whose
`MutationsInterpreter` resolves names against the storage snapshot - so `StorageSnapshot::getColumn`
threw `NO_SUCH_COLUMN_IN_TABLE` and no `hasToken`/`hasAnyTokens`/`hasAllTokens` query could run at
all until the mutation materialized. Plain `SELECT`s and `count()` alike failed.

The mutation steps read the real columns their own expressions need; the index column is produced
on top of their output, so it is simply filtered out of that list.

Closes: #116832
@clickhouse-gh

clickhouse-gh Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [feb2372]

Summary: ✅


AI Review

Summary

This PR fixes the right boundary: the on-fly mutation interpreter should receive the storage columns that a mutation chain may need to read or rewrite, but not the synthetic __text_index_* columns created only for direct text-index reads. I reviewed the current head, traced index_read_tasks through the read-pool and mutation-planning paths, and checked the existing discussion against the updated code and test. The earlier prefix-based shadowing problem is fixed, the pending ALTER UPDATE case is covered, and I did not find an additional correctness gap in the current diff.

Final Verdict

✅ Approve. No additional issues found in the current diff.

LLVM Coverage Report

Measured on commit feb2372.

Metric Baseline Current Δ
Lines 88.70% 88.70% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 81.00% 81.00% +0.00%

Changed lines: Changed C/C++ lines covered: 14/14 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 31, 2026
@tiandiwonder tiandiwonder self-assigned this Aug 31, 2026
@zlareb1

zlareb1 commented Aug 31, 2026

Copy link
Copy Markdown
Member

The strip is stage-agnostic (it happens before getMutationSteps, and MergeTreeReadPoolBase::buildReadTaskInfo is the only caller), so the fix also covers the pending on-fly UPDATE variant — but the description's claim that "Only DELETE triggers it … a pending UPDATE does not" is not right, and the test never exercises the UPDATE spelling.

A pending on-fly ALTER UPDATE whose assignment target is one of the query's read columns throws the identical NO_SUCH_COLUMN_IN_TABLE before this fix: AlterConversions::filterMutationCommands keeps an UPDATE whenever its written column intersects the read set, and the interpreter then resolves the whole read list including the __text_index_* virtual (verified on 26.8: ALTER TABLE t UPDATE c = c + 1 WHERE 1 with mutations_sync = 0 on a non-indexed column c, then SELECT c FROM t WHERE hasToken(s, 'tok1') with the text index serving the direct read — same exception as the DELETE case; see the verification in #116832 (comment)). The trigger is any on-fly mutation stage that must carry the query's read columns, not DELETE specifically.

Suggest (a) rewording the description accordingly and (b) adding that spelling to 05077_text_index_direct_read_pending_mutation.sql, e.g.:

ALTER TABLE t UPDATE c = c + 1 WHERE 1;  -- pending, mutations_sync = 0
SELECT c FROM t WHERE hasToken(s, 'tok1'); -- throws NO_SUCH_COLUMN_IN_TABLE before this fix

For the UPDATE-touches-the-indexed-column direction there is no silent-stale window: direct read is independently disabled via canUseIndex against getAllUpdatedColumns, so stripping the virtual is safe there — worth a comment in the test, since that is the invariant that makes the strip sound.

@clickhouse-gh

clickhouse-gh Bot commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing feb237282 with master 6113efbf0 (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.

Compile time of recompiled translation units

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

Job report

…s name prefix

`isTextIndexVirtualColumn` is a test of the `__text_index_` name prefix, which is not reserved: a
table may declare a column of its own with such a name, even with no text index at all. Dropping
that column from the list handed to `AlterConversions::getMutationSteps` would hide it from
`filterMutationCommands`, which would then discard a pending `ALTER UPDATE` of it while the reader
still reads the column - answering from stale on-disk values.

Take the names from `index_read_tasks` instead, which is where the index reader's columns were
synthesized, so only a column that really is produced by the index reader is dropped.

The test covers that shadowing column, and also a pending `ALTER UPDATE` of a column in the read
set: `DELETE` is not the only trigger, an `UPDATE` kept by `filterMutationCommands` carries the read
columns into its stage just the same and threw the identical `NO_SUCH_COLUMN_IN_TABLE` (verified
against a binary without the fix). `query_plan_direct_read_from_text_index` is pinned, because the
runner randomizes it off in a fraction of the runs, which would silently degrade every arm into a
copy of the `query_plan_direct_read_from_text_index = 0` arm.

Test renumbered `05077` -> `05059`.
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @zlareb1 you are right, and I reproduced it: with a binary built without the fix, a pending ALTER UPDATE c = c + 1 WHERE 1 (mutations_sync = 0) on a non-indexed column, followed by SELECT sum(c) ... WHERE hasToken(s, 'tok1'), throws the very same NO_SUCH_COLUMN_IN_TABLE. The description said the opposite; it is corrected now, and 05059_text_index_direct_read_pending_mutation has that arm, with a comment on the canUseIndex / getAllUpdatedColumns invariant you named — the strip is sound precisely because an UPDATE of the indexed column disables the direct read in the first place.

Also, following the review of the prefix heuristic, the filter no longer tests the __text_index_ prefix at all: it drops exactly the columns listed in index_read_tasks, which is where the index reader's columns are synthesized.

@tiandiwonder
tiandiwonder added this pull request to the merge queue Sep 4, 2026
Merged via the queue into master with commit de1002b Sep 4, 2026
174 checks passed
@tiandiwonder
tiandiwonder deleted the fix-text-index-on-fly-mutation branch September 4, 2026 06:27
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 4, 2026
@clickgapai

Copy link
Copy Markdown
Contributor
Reproducer
SET apply_mutations_on_fly = 1;   -- default

CREATE TABLE t (id UInt64, s String, INDEX idx s TYPE text(tokenizer = splitByNonAlpha))
ENGINE = MergeTree ORDER BY id;
INSERT INTO t SELECT number, concat('tok', toString(number % 10), ' word') FROM numbers(1000);

SYSTEM STOP MERGES t;
ALTER TABLE t DELETE WHERE id < 100 SETTINGS mutations_sync = 0;

SELECT count() FROM t WHERE hasToken(s, 'tok1');
-- 26.8.2.7 / 26.7.6.57 / 26.6.4.55 / 26.3.29.7 / 26.2.19.43:
--   Code: 16. DB::Exception: There is no column
--   __text_index_idx_hasToken_3eb56adb17617f51540c807a788a564f in table. (NO_SUCH_COLUMN_IN_TABLE)
-- expected: 90
-- Runs only with query_plan_direct_read_from_text_index = 0, use_skip_indexes = 0,
-- or apply_mutations_on_fly = 0. Same throw for a pending ALTER UPDATE of a read column.

Results:

  • Culprit PR: not pinned — introduced in the 25.8–26.2 window (details in Analysis metadata)
  • Reproduces on: 26.8, 26.7, 26.6, 26.3
  • Tested versions: master, 26.8, 26.7, 26.6, 26.3
  • Also tested (unsupported, context only): 26.2 reproduces

CC component owner: @CurtizJ @ahmadov

Analysis metadata

comp-text-index · Severity P2 · Finding phase_d_pr117277

  • Verified against master build b0bbe7a30730 (aarch64)
  • Could not test (binary unavailable): master
Introducing change: first appears in the `26.2` line
  • The reproducer fails on 25.8 and older (Unknown setting — the affected code path does not exist there) — so this is not pre-existing: the introducing change is in the 25.8–26.2 window, most likely the change that added the code path.
  • Secondary components: comp-testing

@clickgapai

Copy link
Copy Markdown
Contributor

@alexey-milovidov @tiandiwonder — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

5 participants