Repository navigation
Provide _table and _database virtual columns to mutations - #117724
Conversation
`MergeTreeSequentialSource` filled the constant virtual fields `_part_index`, `_part_starting_offset` and `_sample_factor`, but not `_table` and `_database`, which `ReadFromMergeTree` publishes for regular reads. A mutation referencing either of them (`ALTER TABLE ... UPDATE v = _table`, `DELETE WHERE _table = ...`) failed with `Unexpected const virtual column: _table` (`NO_SUCH_COLUMN_IN_TABLE`) in `MutatePlainMergeTreeTask`. Seen in the Upgrade check of #114054: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=114054&sha=0957ea4e115236f885e360ac2dbca16ff2011411&name_0=PR&name_1=Upgrade%20check%20(amd_release) Closes: #102331 Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Workflow [PR], commit [bbea07f] Summary: ✅
AI ReviewSummaryThe PR supplies Findings❌ Blockers
Tests
Final VerdictStatus: ❌ Block — the dismissed queued-dependency correctness issue remains unresolved. The minimum required change is to preserve or revalidate those dependencies and cover the queue transition with a regression test. No additional inline comments were posted. |
|
The values match One observation, below the bar for this PR but maybe worth a follow-up: |
`04510_mutation_query_plan_only_virtual_columns` asserted that `ALTER TABLE ... DELETE WHERE _table != ''` fails with `UNFINISHED`, because the per-part read path could not materialize `_table`. This change makes that mutation succeed, so the test now asserts the fixed behavior: the row is deleted and one row remains. For the same reason the `Unexpected const virtual column: _table: While executing MergeTreeSequentialSource.` entry in the upgrade check error allowlist (added in #117218 as a workaround for exactly this bug) is removed: the pending mutation replayed after the upgrade now succeeds, so the message is no longer produced, and keeping the entry would mask a regression of this fix. The new test is renumbered to `05175_mutation_table_database_virtual_columns`, the next free number after the merge with `master`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 Thanks for the careful read - and for confirming that no other declared I agree with the determinism observation and agree it is out of scope here: the guard in Also in this push: merged |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes7 object files changed (+39.59 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units26 translation units recompiled, 316 s compile time in total, 26 of them have a recent master baseline. Median compile-time ratio to the baselines is ×0.95 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
The number 05175 has been taken by many tests on master since the PR was opened. Co-Authored-By: Claude Fable 5.1 <[email protected]>
… tables `MutationsInterpreter::validateNonDeterministicMutationsForStorage` only scanned for non-deterministic functions and subqueries, so a virtual column declared with `deterministic = false` (`_table`, `_database`, `_disk_name`, `_part_index`, ...) passed the guard. Replicas of one `ReplicatedMergeTree` table may have different local names or disks, so such a mutation produced different parts on different replicas and the persisted result depended on which replica committed first (`checkPartChecksumsAndCommit` then fetched the first finisher's part). Now that `_table` and `_database` are actually provided to mutations, the guard reports them the same way as a non-deterministic function, with `allow_nondeterministic_mutations` as the escape hatch. A real column with the same name shadows the virtual one and is not affected. Addresses the review finding on #117724 and #119212. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
🕵 Addressed the AI-review Blocker: mutations on Also merged |
…virtual-column # Conflicts: # tests/docker_scripts/upgrade_runner.sh
…e replicated mutation guard `findFirstNonDeterministicFunction` matched `ASTIdentifier::name`, the full compound text, so `t._table` or `db.t._database` bypassed the check on `Replicated` tables. Identifiers qualified with the mutated table's `StorageID` are now matched by their short name. Lambda parameters named like a non-deterministic virtual column (`arrayExists(_table -> ...)`) were falsely rejected. The lambda body is now visited with its parameters masked. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… in the replicated mutation guard Lambda parameters are now tracked separately from the set of non-deterministic virtual columns. Previously a parameter named `_table` removed `_table` from that set, which also hid table-qualified references (`t._table`) inside the lambda body and references inside the definitions of `ALIAS` columns. Now an identifier is treated as bound to a parameter only when its first part names one. The definition of an `ALIAS` / `EPHEMERAL` column is visited in the table scope, with no lambda parameters bound. This also stops a parameter named like an `ALIAS` column (`arrayExists(v -> v = 'x', [''])`) from being resolved as that column. A subcolumn of an `ALIAS` column (`a.s` for `a Tuple(s String) ALIAS tuple(_table)`), optionally qualified with the table, is now resolved to its parent column, and the parent's definition is followed. Subcolumns of aliases are not registered in `ColumnsDescription`, so they are looked up by prefix and checked against the type. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… with the analyzer The replicated mutation guard re-implemented identifier resolution on the AST and kept missing ways to reach `_table` / `_database`: a bare `Nested` prefix assembled from `ALIAS` subcolumns, and the table-qualified interpretation chosen under `analyzer_compatibility_prefer_alias_over_subcolumn`. Resolve the `UPDATE` / `DELETE` expressions with `QueryAnalyzer` against the table instead, the same way the mutation is executed, and refuse the mutation when any non-deterministic virtual column is read from the table, also through `ALIAS` definitions. `EPHEMERAL` definitions, which the analyzer does not expand, are resolved in turn. Since the mutation may run in the background with a different value of `analyzer_compatibility_prefer_alias_over_subcolumn`, both interpretations are checked. `_part_index` and `_part_starting_offset` are always 0 in a mutation, which reads every part on its own, so they are allowed like `_sample_factor`. `findFirstNonDeterministicFunction` is back to its version on `master`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nly the used qualifier interpretations
`findNonDeterministicVirtualColumn` resolved the expressions of all commands in one `ASTExpressionList`,
so an alias defined in a later command (`UPDATE value = ('safe' AS _table)`) could hide a read of the
`_table` virtual column in an earlier command. Each command, and each followed `EPHEMERAL` definition,
is now resolved in its own scope, the same way the commands are analyzed in separate stages on execution.
The check also resolved compound identifiers under both values of
`analyzer_compatibility_prefer_alias_over_subcolumn`, which made deterministic mutations fail when the
unused interpretation was ill-typed. Now only the value of the query and the value of the background
context, which executes the mutation, are checked.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…in the replicated mutation guard
A bare identifier that is both an alias and a column, such as `_table` in
`UPDATE value = ('safe' AS _table) WHERE _table != ''`, is resolved differently
depending on `prefer_column_name_to_alias`. The mutation is validated with the
settings of the query but executed with the settings of the background context,
so `findNonDeterministicVirtualColumn` now analyzes the expressions with the
name-resolution settings (`analyzer_compatibility_prefer_alias_over_subcolumn`
and `prefer_column_name_to_alias`) of both contexts.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…in the replicated mutation guard `MATERIALIZE COLUMN` of an `EPHEMERAL` column defined over `_database` or `_table` evaluates the column's default expression inside the mutation, so each replica would write its own local name into the new part. Treat the default expression of the materialized column as one more scope of the guard. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…g non-deterministic virtual columns A mutation calculates projections from the block read by `MergeTreeSequentialSource`, which now carries `_table` and `_database`, so `MATERIALIZE PROJECTION` (or any mutation rebuilding a projection) for a projection reading such a column would store different data on differently named replicas. Which projections are rebuilt depends on the state of each part, so every projection is checked unless the mutation only drops projections. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ojections reading non-deterministic virtual columns `MODIFY COLUMN` and other schema changes on a `ReplicatedMergeTree` table go through `StorageReplicatedMergeTree::alter`, which queues the generated mutation without the check of `validateNonDeterministicMutationsForStorage`. Such a mutation rebuilds the projections sorted by a column whose type changes, so a projection reading `_table` could store different data on differently named replicas. Run `MutationsInterpreter::findNonDeterministicVirtualColumn` on the generated mutation commands before any local state is changed, unless `allow_nondeterministic_mutations` is enabled. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…TER` metadata `StorageReplicatedMergeTree::alter` checked the mutation generated by a schema change against the current metadata, so it missed a projection reading `_table` added by the same `ALTER` (which the mutation then rebuilds), and refused `DROP PROJECTION p, MODIFY COLUMN ...`, because the generated `DROP_PROJECTION` command stores the projection name in `column_name`. Now `MutationsInterpreter::findNonDeterministicVirtualColumn` accepts the metadata the mutation will be executed with, and the `ALTER` passes `future_metadata`, which includes added projections and excludes dropped ones. Co-Authored-By: Claude Opus 5.5 <[email protected]>
MergeTreeSequentialSource, which reads parts for mutations and merges, filled the constant virtual fields_part_index,_part_starting_offsetand_sample_factor, but not_tableand_database, whichReadFromMergeTreepublishes for regular reads. A mutation referencing either column (ALTER TABLE ... UPDATE v = _table,ALTER TABLE ... DELETE WHERE _table = ..., lightweightDELETE) failed inMutatePlainMergeTreeTaskwithUnexpected const virtual column: _table(NO_SUCH_COLUMN_IN_TABLE) and kept retrying.The fix publishes both fields in
createMergeTreeSequentialSource, the same valuesReadFromMergeTreeuses. New test05227_mutation_table_database_virtual_columnscoversUPDATE ... = _table,UPDATE ... = _database,DELETE WHERE _table = ...and a lightweightDELETEfiltered on_database._table,_databaseand_disk_nameare declared withdeterministic = false: replicas of oneReplicatedMergeTreetable may have different local names or disks, so a mutation reading them would produce different parts on different replicas. The guardMutationsInterpreter::validateNonDeterministicMutationsForStorageonly scanned for non-deterministic functions and subqueries; it now also rejects non-deterministic virtual columns onReplicatedtables unlessallow_nondeterministic_mutations = 1, with a real column of the same name shadowing the virtual one. To find them, the mutation expressions are resolved with the analyzer against the table, the same way the mutation is executed, so a virtual column is caught however it is reached: through a table qualifier, anALIAS/EPHEMERALcolumn or its subcolumn, or aNestedprefix._sample_factor,_part_indexand_part_starting_offsetare constants in a mutation and stay allowed. New test05228_mutation_table_database_virtual_columns_replicatedcovers the rejection, the deterministic controls and the escape hatch.Found via the
Upgrade check (amd_release)red of #114054: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=114054&sha=0957ea4e115236f885e360ac2dbca16ff2011411&name_0=PR&name_1=Upgrade%20check%20(amd_release)Closes: #102331
Closes: #119212
Related: #114054
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
NO_SUCH_COLUMN_IN_TABLEexceptionUnexpected const virtual column: _tablewhen a mutation (ALTER TABLE ... UPDATE/DELETEor lightweightDELETE) on aMergeTreetable referenced the_tableor_databasevirtual column. Mutations onReplicatedtables that read a virtual column whose value may differ between replicas (_table,_database,_disk_name) are now rejected unlessallow_nondeterministic_mutationsis enabled, like non-deterministic functions. Closes #102331.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/117724]
Version info
26.11.1.1252-master(included in26.11and later)