Skip to content

Fix logical error reading a text-indexed column after a lazy or unfinished ALTER - #116549

Merged
Avogar merged 2 commits into
masterfrom
fix-text-index-lazy-alter-conversion
Sep 3, 2026
Merged

Avogar merged 2 commits into
masterfrom
fix-text-index-lazy-alter-conversion

Conversation

@Avogar

@Avogar Avogar commented Aug 26, 2026 •

Copy link
Copy Markdown
Member

Related: #112213

Reading a column that has a text (full-text) index through the index — the exact-equals direct read (with the array tokenizer) or the LIKE fallback — threw the Unexpected return type from materialize logical error when the part still needed a read-time type conversion that no mutation had materialized yet. This happens after a metadata-only or not-yet-finished ALTER MODIFY COLUMN whose mutation is absent — killed, dropped, or never created (lazy JSON type hints via allow_experimental_json_lazy_type_hints).

MergeTreeReadersChain::executeActionsBeforePrewhere skipped performRequiredConversions when perform_alter_conversions was false and columns_overwritten_by_chain was empty. That set is populated only from chained UPDATE/DELETE, so a metadata-only / lazy MODIFY (no UPDATE/DELETE) left the on-disk column (e.g. a JSON path still stored as Dynamic) unconverted under its post-MODIFY type. The fix always runs performRequiredConversions in that branch, skipping only the overwritten-and-not-consumed columns.

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):

Fixed the Unexpected return type from materialize logical error when reading a column that has a text index through the index after a metadata-only or not-yet-materialized ALTER MODIFY COLUMN (including lazy JSON type hints) that requires a read-time type conversion.


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

Version info

  • Backported to: 26.8.3.27, 26.7.7.18, 26.6.5.43

When a MergeTree part still needs a read-time alter conversion for a column
(a lazy JSON type hint, or any not-yet-materialized `ALTER MODIFY COLUMN`
whose mutation is absent - killed, dropped, or metadata-only), reading that
column through the text index code paths (exact-equals direct read with the
`array` tokenizer, or the LIKE fallback) did not apply the conversion. The
on-disk column (e.g. a JSON path still stored as `Dynamic`) reached
`materialize` in `evaluateMissingDefaults` advertising the post-`MODIFY` type
and threw `Unexpected return type from materialize` (or crashed / requested a
huge allocation, depending on the target type).

`MergeTreeReadersChain::executeActionsBeforePrewhere` skipped
`performRequiredConversions` entirely when `perform_alter_conversions` was
false and `columns_overwritten_by_chain` was empty. That set is populated only
from chained `UPDATE`/`DELETE`, so a metadata-only / lazy `MODIFY` (no
`UPDATE`/`DELETE`) leaves it empty while the on-fly alter step sets
`perform_alter_conversions = false`. Always run `performRequiredConversions`
in that branch, skipping only the overwritten-and-not-consumed columns; an
empty skip set now converts every read column.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4f8df11]

Summary: ❌

job_name test_name status info comment
LLVM Coverage FAIL
Generate LLVM Coverage Diff Report FAIL cidb IGNORED
Print Uncovered Code FAIL cidb IGNORED
Parser memory check ERROR
Resolve master binary ERROR IGNORED

AI Review

Summary

This PR makes MergeTreeReadersChain::executeActionsBeforePrewhere apply performRequiredConversions for text-index/index-read steps that previously skipped conversion when perform_alter_conversions was false and no columns were marked as overwritten, which is the right fix for the stale-type exception after a lazy or unfinished ALTER MODIFY COLUMN. The implementation itself looks coherent, but the new lazy-ALTER regression test does not preserve the partially-materialized ADD INDEX state it depends on, so it can pass without exercising the buggy path.

Findings

⚠️ Majors

  • [tests/queries/0_stateless/04935_text_index_lazy_alter_conversion.sql:25] The lazy-JSON case assumes the old part still lacks materialized text-index data, but the test never freezes or verifies that state. If the ADD INDEX mutation finishes on the old part before the SELECTs run, the query no longer needs to evaluate the virtual column from the physical doc.a.id, and the fixed performRequiredConversions path stops being covered. Suggested fix: mirror the nearby partially-materialized text-index tests by stopping merges immediately after ADD INDEX and/or asserting that at least one active part still has secondary_indices_marks_bytes = 0 before the direct-read / LIKE checks.
Final Verdict

Needs changes: the code fix looks sound, but the regression coverage should make the partially-materialized text-index state deterministic before merge.

LLVM Coverage Report

⚠️ No coverage measurement for commit 4f8df11: bash ci/jobs/scripts/generate_diff_coverage_report.sh failed (its output is on the Generate LLVM Coverage Diff Report result).

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 26, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 4f8df11b1 with master 84ac4b282 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of bb3c0a852; 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, 12 s compile time in total, 7 of them have a recent master baseline.

Job report

The stateless harness randomizes `query_plan_direct_read_from_text_index`;
when it injects `0`, the `=` / `LIKE` queries fall back to a regular scan
(which converts correctly) and return the right count without exercising the
text-index read path the fix is about. Pin it to `1` on the direct-read
assertions so the test always covers the fixed path.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@Avogar Avogar added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Sep 2, 2026
@vdimir vdimir self-assigned this Sep 2, 2026
ALTER TABLE t_text_lazy_alter MODIFY COLUMN doc JSON(`a.id` Nullable(String));

-- Text index with the `array` tokenizer -> exact-equals direct read (drops the residual equals).
ALTER TABLE t_text_lazy_alter ADD INDEX idx doc.a.id TYPE text(tokenizer = array) GRANULARITY 100000000;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This lazy-JSON case only hits the bug while the old part is still partially materialized for idx: doc.a.id must be read from the physical column so evaluateMissingDefaults sees the stale Dynamic storage under the post-MODIFY type. Nothing here prevents the ADD INDEX mutation on that 4k-row part from finishing before these SELECTs run, and once it does the query can stay green without ever exercising the fixed path.

The nearby partially-materialized text-index tests freeze this state with SYSTEM STOP MERGES immediately after ADD INDEX and/or assert that at least one active part still has secondary_indices_marks_bytes = 0. Please do the same here (or kill the index-materialization mutation) before the direct-read / LIKE assertions so the regression stays deterministic.

@Avogar
Avogar added this pull request to the merge queue Sep 3, 2026
Merged via the queue into master with commit 20ba9fd Sep 3, 2026
338 of 344 checks passed
@Avogar
Avogar deleted the fix-text-index-lazy-alter-conversion branch September 3, 2026 18:12
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Sep 3, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 3, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 3, 2026
Backport #116549 to 26.6: Fix logical error reading a text-indexed column after a lazy or unfinished ALTER
clickhouse-gh Bot added a commit that referenced this pull request Sep 3, 2026
Backport #116549 to 26.8: Fix logical error reading a text-indexed column after a lazy or unfinished ALTER
clickhouse-gh Bot pushed a commit that referenced this pull request Sep 3, 2026
clickhouse-gh Bot added a commit that referenced this pull request Sep 3, 2026
Backport #116549 to 26.7: Fix logical error reading a text-indexed column after a lazy or unfinished ALTER
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 4, 2026
…indexed column after a lazy or unfinished ALTER
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Sep 4, 2026
…indexed column after a lazy or unfinished ALTER
robot-clickhouse-ci-1 added a commit that referenced this pull request Sep 4, 2026
Cherry pick #116549 to 26.3: Fix logical error reading a text-indexed column after a lazy or unfinished ALTER
robot-clickhouse added a commit that referenced this pull request Sep 4, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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