Repository navigation
Cherry pick #105847 to 26.4: Fix on-fly mutation LowCardinality pass-through column conversion - #106993
Merged
robot-clickhouse-ci-1 merged 18 commits intoJun 10, 2026
Conversation
When `apply_mutations_on_fly = 1` is used with an UPDATE/DELETE mutation
queued BEFORE a pending `ALTER MODIFY COLUMN <c> LowCardinality(...)`,
`AlterConversions::getMutationSteps` sets `perform_alter_conversions =
false` on the UPDATE/DELETE step. This is correct for columns the
mutation overwrites — pre-casting them on read can fail (e.g. `CAST('x'
AS UInt64)` for a value the UPDATE is about to replace with `100`) and
the action's DAG embeds its own CAST anyway.
But the step's reader also produces pass-through columns the mutation
never touches. Skipping `performRequiredConversions` for those leaves
the block advertising the post-MODIFY metadata type (e.g.
`LowCardinality(String)`) over a bare `ColumnString`. Downstream
operators trust the declared type and trip on the mismatch
(`MergingSortedTransform`'s `ColumnLowCardinality::insertFrom` throws
`ILLEGAL_COLUMN: Expected ColumnLowCardinality, got String`;
`NativeWriter`'s `typeid_cast<ColumnLowCardinality>` throws
`LOGICAL_ERROR: Bad cast from type DB::ColumnString to
DB::ColumnLowCardinality`; function execution's
`executeWithoutLowCardinalityColumns` SEGVs on the same mismatch).
Carry the set of columns the on-fly chain will overwrite through
`PrewhereExprStep::columns_overwritten_by_chain` (populated from
`AlterConversions::all_updated_columns`). In
`MergeTreeReadersChain::executeActionsBeforePrewhere`, when a step has
`perform_alter_conversions = false`, run `performRequiredConversions`
on the read columns with the overwritten ones temporarily nulled out
and restored afterwards — so pass-through columns get cast to the
metadata type while overwritten columns still flow into the action's
DAG in their on-disk form.
Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
`columns_overwritten_by_chain` was set to `all_updated_columns`, which also tracks `MODIFY COLUMN` (`READ_COLUMN`) targets. Those columns are not actually applied on-fly — they only set the `version_of_alter_mutation` fence — so they still need `performRequiredConversions`. Skipping conversion for them left the column at its on-disk type while the block header advertised the post-MODIFY type, e.g. an `ORDER BY` over a `String → LowCardinality(String)` column threw `Code 44. Expected ColumnLowCardinality, got String` in `MergingSortedTransform`. Build the set from `UPDATE`/`DELETE` commands only. Update the test to drive that exact code path: `UPDATE` queued before `MODIFY COLUMN c LowCardinality(String)` and an `ORDER BY c` with `query_plan_max_limit_for_lazy_materialization = 10` to keep lazy materialization off (matching the production setting).
Lightweight DELETE queued before MODIFY COLUMN: with the narrowed `columns_overwritten_by_chain` the MODIFY'd column still goes through `performRequiredConversions`, so the read returns the post-MODIFY type for the rows that survive the delete. Guards against re-broadening the set.
…tation-lc-conversion
`MutationCommand::column_to_update_expression` was removed from the struct and replaced by the free function `getColumnToUpdateExpression(ASTAlterCommand &)`. Update the on-fly chain target collection to call the helper.
Documents the scenario raised by `clickhouse-gh[bot]` in PR review discussion r3346245939: a later `UPDATE` whose target gets dropped by `filterMutationCommands` while a `MODIFY COLUMN` is pending on the same column. I could not reproduce a user-visible failure but locking in the current behaviour so future changes to `AlterConversions::getMutationSteps` or `filterMutationCommands` have to re-evaluate the case.
…sion' into fix-1818-on-fly-mutation-lc-conversion
Derive `columns_overwritten_by_chain` from the commands that actually survived `filterMutationCommands` for the current `read_columns`, not from the full `mutation_commands` list. A later `UPDATE` whose target is dropped because the query does not need it was still leaving that target in the skip set, which re-creates the type/header mismatch this PR fixes when the dropped target is also affected by a pending `MODIFY COLUMN`. `filterMutationCommands` converts a lightweight `UPDATE _row_exists = 0` into a typed `DELETE` command without the original assignment, so add `_row_exists` explicitly for any filtered `DELETE`. Plain `ALTER DELETE` is unaffected because `_row_exists` does not need on-disk conversion. Replace the placeholder 04308 sanity test with the reproducer suggested by `clickhouse-gh[bot]` in PR review discussion r3350248306. Without the fix the read throws `LOGICAL_ERROR: Unexpected return type from materialize`.
`columns_overwritten_by_chain` used to be a chain-wide union attached to every pre-`MODIFY` step. A step that read a column as a source but did not write it still skipped `performRequiredConversions` for that column, reintroducing the type/header mismatch the PR sets out to fix whenever a later step in the chain overwrites the column. `SELECT sum(b), any(a)` over `UPDATE b = isNotNull(materialize(a))` then `UPDATE a = 'x'` then `MODIFY a LowCardinality(Nullable(String))` reproduces the `LOGICAL_ERROR: Unexpected return type from materialize` reported by `clickhouse-gh[bot]` in PR review r3350564104. Carry the overwritten set per step. `MutationsInterpreter::getMutationActions` now populates `MutationActions::overwritten_columns` with the filter column for a filter step or the `UPDATE` assignment targets for an expression step. `AlterConversions::getMutationSteps` uses that per-step set when `perform_alter_conversions` is false. The chain-wide derivation, including the lightweight-delete `_row_exists` carve-out, is no longer needed since the per-stage filter step already carries its filter column. Drop the `else if (!columns_overwritten_by_chain.empty())` guard in `MergeTreeReadersChain::executeActionsBeforePrewhere` so an empty skip set still triggers a full conversion pass for the step. Extend `04308_on_fly_mutation_filtered_update_chain` with a second query (`SELECT sum(b), any(a)`) that fails without the per-step skip set.
`clickhouse-gh[bot]` (PR review r3350748853) flagged that combining the two assignments into a single `UPDATE b = isNotNull(materialize(a)), a = 'x'` puts `a` in the per-step skip set while the same step's expression also reads it as a source, which could re-trigger the type/header mismatch. I could not reproduce a user-visible failure with this shape, runtime `materialize` handles `a` arriving as the on-disk `Nullable(String)` even when the action DAG was analysed against `LowCardinality(Nullable(String))`. Locking the scenario into `04308_on_fly_mutation_filtered_update_chain` as a sanity check so any future change to action analysis or to the per-step skip set has to re-evaluate it.
`clickhouse-gh[bot]` (PR review r3350836226) suggested `isNull(a)` as a stricter function-dispatch path than the variant-3 `isNotNull(materialize(a))`. Same single-stage shape: `UPDATE b = isNull(a), a = 'x'` followed by `MODIFY COLUMN a LowCardinality(Nullable(String))`. Still no user-visible failure under the per-step skip set. Runtime function dispatch checks the actual column class rather than the DAG-declared type, so `a` arriving as `ColumnNullable` does not enter the `isLowCardinalityNullable` branch. Locked in as variant 4 of `04308_on_fly_mutation_filtered_update_chain`.
`columns_overwritten_by_chain` was built from the full `mutation_commands` list. A later `UPDATE` whose target the query did not need was still contributing to the skip set, so an earlier surviving step that reads that column as a source saw the on-disk type while the block already advertised the post-`MODIFY` type. Build the set from the chain that `filterMutationCommands` returns for the current `read_columns` so dropped commands no longer contribute. `filterMutationCommands` rewrites a lightweight `UPDATE _row_exists = 0` into a `DELETE` typed command without the original assignment. Add `_row_exists` explicitly for any filtered `DELETE` so the lightweight delete chain still skips it from conversion. Trim `04308_on_fly_mutation_filtered_update_chain` to cover the filtered chain case; the per-step variants depend on a deeper change in mutation action analysis and are left to a follow-up. Also drop the per-stage / per-path branching in `MutationsInterpreter::getMutationActions` by collecting both analyser paths into a single vector of `ActionsAndProjectInputsFlagPtr`.
Revert the `MutationsInterpreter::getMutationActions` consolidation. The change was cosmetic, not part of the fix, and shrinks the backport surface to keep only the chain-wide skip set in `AlterConversions`. Clarify the comment on the `RowExistsColumn::name` insertion: it fires for any chained `DELETE`, lightweight or plain. Plain `ALTER DELETE` does not have an on-disk `_row_exists` column, so the explicit insert is harmless for `performRequiredConversions`. Add a note that the skip is keyed on storage column names. Assignment targets are top-level columns today; per-subcolumn `Nested` assignments would need a matching change on the reader-side key. Drop the `enable_block_number_column = 1, enable_block_offset_column = 1` clause from `04268_on_fly_mutation_delete_modify_chain`. Verified the bug reproduces identically with the settings on and off, and `clickhouse-test` randomises both. The `dag.getOutputs()` simplification from the review does not work: each step's outputs list every column the step produces, including pass-through sources, so the skip set would over-skip and bring back the original LowCardinality pass-through mismatch.
The current implementation does not put a per-step `overwritten_columns` set on `MutationActions`; the skip set is computed once in `getMutationSteps` and attached to every step's `PrewhereExprStep::columns_overwritten_by_chain`. Removing the out-of-date helper comment to match.
…c-conversion Fix on-fly mutation LowCardinality pass-through column conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original pull-request #105847
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job