Skip to content

Rebuild projections that compute from a column whose type MODIFY COLUMN changes - #127977

Open
buggins wants to merge 12 commits into
ClickHouse:masterfrom
buggins:fix-projection-rebuild-on-modify-column-127952
Open

buggins wants to merge 12 commits into
ClickHouse:masterfrom
buggins:fix-projection-rebuild-on-modify-column-127952

Conversation

@buggins

@buggins buggins commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Related: #127952

On a type change of a column (ALTER TABLE ... MODIFY COLUMN, which runs a mutation), MutationsInterpreter::prepare rebuilt only the projections having the column in their primary key (projection.metadata->getPrimaryKeyColumns()), and the mutation kept the parts of all the others. That is enough for a projection that stores the column as is, whose values are converted when read like those of the table, but not for one that computes from the column:

  • an aggregate projection such as PROJECTION p (SELECT toHour(dt) AS h, count() GROUP BY h) stores h computed with the old type; its key is h, not dt. After MODIFY COLUMN dt DateTime64(0, 'Asia/Tokyo') on a DateTime('UTC') column, GROUP BY toHour(dt) through it returns the hours in UTC, 0 to 4, instead of 9 to 13;
  • a normal projection sorted by an expression of the column, such as PROJECTION p (SELECT id, toHour(dt) AS h ORDER BY h), stores id and dt, and reading dt is converted, but its order and primary index keep toHour(dt) of the old type, so WHERE toHour(dt) = 9 through it returns no rows.

A projection is now also rebuilt when its sorting key is an expression of the column (getSortingKey().expression->getRequiredColumns()), when its WHERE uses the column (where_clause_ast; such a projection keeps the rows that its filter selected with the old type, even if it stores the column as is), and when it reads the column and computes any of its columns (a column of its sample block that is not one of the columns it reads, such as an aggregate state). An input that is a subcolumn of the column, such as x.dt of a Tuple, counts as the column. A projection that only stores the column as is keeps its parts, as before, unless a wide part does not store the column: such a part computes the column from its default with the new type (a compact part is rewritten with the converted values, so it agrees with its projection), while the projection would convert the values it stores, such as 0 of an implicit UInt8 default read back as '0' instead of the '' of String, so the projection is rebuilt for such a part. The same applies to a wide part that does not store the column, e.g. one that predates ADD COLUMN dt ... DEFAULT, whose projection was computed from the default: the mutation does not write the column to such a part, but it now adds MATERIALIZE_PROJECTION for those projections, with an internal flag rebuild_existing_projection because MATERIALIZE_PROJECTION otherwise skips a part that already has the projection. A column that the projection reads and the part does not store, such as h of h UInt8 DEFAULT toHour(dt) added after the part was written, is computed from its default, so the projection is rebuilt when that default (also through the defaults of other such columns, and through an ALIAS, which is computed from its expression even when the part still stores data of a former column with its name) uses the changed column, even if it stores the column as is. Such a mutation now rebuilds more projections than before, which is needed for their results to be correct. MODIFY COLUMN to Nullable of the same type, with the default serialization and without statistics, and an extension of a Variant are applied to a part without rewriting the column (canSkipConversionToNullable, canSkipConversionToVariant); such a conversion still skips the column, but every projection of the part computed from it, by the same check, is now rebuilt with the same MATERIALIZE_PROJECTION command.

05458_projection_rebuilt_on_modify_column compares the results with the projection forced (force_optimize_projection = 1) and without projections after such a MODIFY COLUMN, for an aggregate projection, a normal projection sorted by toHour(dt), a projection with WHERE toHour(dt) = 9 that stores dt as is, an aggregate projection of a wide part that does not store dt, an aggregate projection over a column h DEFAULT toHour(dt) that the part does not store, an aggregate projection over w DEFAULT h after h becomes ALIAS toHour(dt), an aggregate projection over the subcolumn x.dt of a Tuple, an aggregate projection over the subcolumn x.v of x Tuple(v UInt8) DEFAULT tuple(toHour(dt)) that the part does not store, a projection that stores x or dt as is for a part that does not store it (with an implicit and an explicit default), a projection grouped by id + length(toTypeName(dt)) after dt becomes Nullable(UInt8), an aggregate projection over h DEFAULT id + length(toTypeName(x)) after x Variant(UInt8, String) is extended, and a projection that only stores dt. The first twelve differ on master.

Not covered: a MODIFY COLUMN that changes only the time zone runs no mutation at all, so such a projection is not rebuilt for it either. That part of #127952 is left for a follow-up, which can make such a change run a mutation when a projection computes from the column.

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

ALTER TABLE ... MODIFY COLUMN that changes the type of a column now rebuilds the projections that compute from it, such as an aggregate projection over toHour(dt), a projection sorted by toHour(dt) or a projection with WHERE toHour(dt) = 9. They used to keep the values computed with the old type, so queries served by them could return wrong results.

🤖 Generated with Claude Code


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

…UMN` changes

On a type change of a column, the mutation rebuilt only the projections having the column in their primary key, and
kept the parts of the others. A projection that computes from the column kept what it computed with the old type and
returned it for the queries it served: an aggregate projection `SELECT toHour(dt) AS h, count() GROUP BY h` its
`toHour(dt)` values, and a projection sorted by an expression of the column, such as `ORDER BY toHour(dt)`, its
order and primary index, so its pruning dropped matching rows. Also rebuild a projection whose sorting key is an
expression of the column, and one that reads the column and computes any of its columns. A projection that only
stores the column as is keeps its parts as before.

Related: ClickHouse#127952

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). external Author is not a member of the ClickHouse organization labels Oct 27, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 31, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fba5ab5]

Summary: ❌

job_name test_name status info comment
Upgrade check (amd_release) FAIL
Error message in clickhouse-server.log (see upgrade_error_messages.txt) FAIL cidb

AI Review

Summary

This PR rebuilds projections affected by type-changing ALTER TABLE ... MODIFY COLUMN mutations, including dependencies through filters, sorting expressions, defaults, aliases, and subcolumns. Reviewed head fba5ab55ce801f55662c1effc7d90e30b919f001 and all prior discussion. All eight previous findings are addressed; no new actionable findings remain. The core regression cases passed on the available PR binary, with all 22 output lines matching their reference.

Missing context / blind spots
  • ⚠️ The available binary was built at 95b2d19344aa3e060287eddb54a70ffa75975777. The latest Nullable and Variant shortcut fix and its two added regression cases were reviewed statically; running the complete 05458_projection_rebuilt_on_modify_column test against the current head would close this execution gap.
Final Verdict

Status: ✅ Approve. No unresolved findings or required code changes.

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

clickhouse-gh Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing fba5ab55c with master 43871b94f (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.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.63, programs/clickhouse-keeper ×0.69 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse DB::registerFunctionConversion(DB::FunctionFactory&) 5.6 s 9.8 s +6.3 s (+179%)
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.3 s 7.3 s +5.2 s (+250%)
Compile time of recompiled translation units

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

Job report

…OLUMN` changes

A projection with `WHERE` keeps the rows that the filter selected. After
`MODIFY COLUMN dt DateTime64(0, 'Asia/Tokyo')` on a `DateTime('UTC')`
column, `PROJECTION p (SELECT id, dt WHERE toHour(dt) = 9 ORDER BY id)`
still held the row of 9:00 UTC, and `WHERE toHour(dt) = 9` through it
returned no rows instead of the row of 0:00 UTC. It stores `dt` as is and
its key is `id`, so the previous commit did not rebuild it. Now a
projection is also rebuilt when its `WHERE` uses the column.

Found by the AI review of the PR.

Related: ClickHouse#127952

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ype `MODIFY COLUMN` changes

A wide part that predates `ADD COLUMN dt ... DEFAULT` does not store `dt`;
its values are computed from the default when read. A projection
materialized on such a part, e.g. `SELECT toHour(dt) AS h, count() GROUP BY h`,
is computed from those values. After `MODIFY COLUMN dt DateTime64(0, 'Asia/Tokyo')`
the reads compute `dt` with the new type, but `splitAndModifyMutationCommands`
dropped the `READ_COLUMN` for the part, since there is nothing to rewrite, so
the interpreter never decided to rebuild the projection, and `GROUP BY toHour(dt)`
through it kept returning the hours in UTC.

Now in that case the mutation adds `MATERIALIZE_PROJECTION` for the projections
of the part that compute from the column (the rule moved to
`MutationsInterpreter::projectionComputesFromColumn`). The interpreter skips
`MATERIALIZE_PROJECTION` for a part that already has the projection, so the
generated command carries the new internal flag `rebuild_existing_projection`.
The column is still not written to the part. A compact or packed part
already reads such a column for its projections, writing it, and goes through
the previous commit's check.

Found by the AI review of the PR.

Related: ClickHouse#127952

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…he column whose type `MODIFY COLUMN` changes

A wide part that stores `dt` but predates `ADD COLUMN h UInt8 DEFAULT toHour(dt)`
does not store `h`: it is computed from the default when read. A projection
`SELECT h, count() GROUP BY h` materialized on such a part stored the hours
computed with the old type. After `MODIFY COLUMN dt DateTime64(0, 'Asia/Tokyo')`
the reads compute `h` with the new time zone, but the projection reads `h`,
not `dt`, so it was not rebuilt, and `GROUP BY h` through it returned the
hours in UTC.

Now `projectionComputesFromColumn` also follows a column that the projection
reads and the part does not store through its default, also through the
defaults of other such columns, and the projection is rebuilt when the
default uses the changed column, even if the projection stores the column as
is. The interpreter checks the columns of the part, through the renames of
`AlterConversions`; the case of a part that stores neither column, handled by
`splitAndModifyMutationCommands`, checks the columns of the part there.

Found by the AI review of the PR.

Related: ClickHouse#127952

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-projections Projections: precomputed layouts for accelerating queries (projection parts, maintenance). and removed comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). labels Nov 1, 2026
buggins and others added 2 commits November 2, 2026 09:06
…e column whose type `MODIFY COLUMN` changes

The previous commit follows a column that the projection reads through its
default only when the part does not store the column. An `ALIAS` is computed
from its expression whenever it is read, but a part can still store the data
of a former column with its name: after `MODIFY COLUMN h UInt8 ALIAS toHour(dt)`
of a stored `h`, the old files stay until the next mutation. With
`w UInt8 DEFAULT h` and a projection `SELECT w, count() GROUP BY w`, the walk
stopped at `h`, and `MODIFY COLUMN dt DateTime64(0, 'Asia/Tokyo')` kept the
projection with the old hours. Now an `ALIAS` is always followed; the check
whether the part stores the column applies only to the other defaults.

Found by the AI review of the PR.

Related: ClickHouse#127952

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…`MODIFY COLUMN` changes

`projectionComputesFromColumn` compared the inputs of the projection and of
its keys with the name of the column, so a projection that reads a
subcolumn, such as `x.dt` of `x Tuple(dt DateTime('UTC'))` in
`SELECT toHour(x.dt) AS h, count() GROUP BY h`, was not rebuilt by
`MODIFY COLUMN x Tuple(dt DateTime64(0, 'Asia/Tokyo'))` and kept the old
hours. Now an input that the column metadata resolves to a subcolumn of the
column counts as the column, for the inputs of the projection and of its
primary and sorting keys; a column whose name has a dot is still a column of
its own.

Found by the AI review of the PR.

Related: ClickHouse#127952

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… computed from

A projection that reads a subcolumn of a column the part computes from its
default, as `toUInt16(x.v)` with `x Tuple(v UInt8) DEFAULT tuple(toHour(dt))`,
was kept by `MODIFY COLUMN dt`: the walk over the defaults looked up the
default of `x.v`, which has none, so it did not see that `x` is computed from
`dt`. The walk now resolves a subcolumn to its column, for the inputs of the
projection and for the identifiers in the defaults it follows, through the
column metadata, which tells a subcolumn from a column whose name has a dot.

Found by the AI review of the PR.

Related: ClickHouse#127952

Co-Authored-By: Claude Opus 5.5 <[email protected]>
buggins and others added 2 commits November 3, 2026 11:28
…not store the column

A projection that only stores the changed column was kept by `MODIFY COLUMN`,
which is right when the part stores the column: the part and the projection
convert the same values. A wide part that predates `ADD COLUMN` does not store
it and computes it from its default with the new type, while the projection
converted the values it stored, so `x UInt8` changed to `String` read
`['0','0','0']` through the projection and `['','','']` without it, and a
`DateTime('UTC') DEFAULT '2026-01-01 00:00:00'` changed to
`DateTime64(0, 'Asia/Tokyo')` had the hour `9` through the projection and `0`
without it, as on master. A projection that reads the column, or a subcolumn
of it, is now rebuilt for a part that does not store the column. Both callers
of the check use it: the branch of `splitAndModifyMutationCommands` for a
column that the part does not store, and `MutationsInterpreter`.

Found by the AI review of the PR.

Related: ClickHouse#127952

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…build-on-modify-column-127952

# Conflicts:
#	src/Interpreters/MutationsInterpreter.h
…pplies without rewriting it

`MODIFY COLUMN` to `Nullable` of the same type, with the default serialization and without statistics, and an extension of a
`Variant` are applied to a part without rewriting the column: `MutateTask::prepare` drops the command before the interpreter
sees it. A projection of the part computed from the column then kept what it computed from the values of the old type, also
through the default of a column that the part does not store: with `PROJECTION p (SELECT id + length(toTypeName(dt)) AS h,
count() GROUP BY h)`, after `MODIFY COLUMN dt Nullable(UInt8)` the query answered through the projection returned
`[5,6,7]` instead of `[15,16,17]`.

Now such a conversion still skips the column, but a projection of the part computed from it is rebuilt, as `MATERIALIZE
PROJECTION` builds it, by the same check as for a rewritten column.

Related: ClickHouse#127952
Related: ClickHouse#127977 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

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

Labels

can be tested Allows running workflows for external contributors comp-projections Projections: precomputed layouts for accelerating queries (projection parts, maintenance). external Author is not a member of the ClickHouse organization pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants