Repository navigation
Conversation
…utations `now`, `today`, `CURRENT_DATE`, `user` and other functions that allow omitting parentheses are resolved by the analyzer as calls when no column, alias or lambda parameter binds the name. The determinism checks of a mutation walk the raw AST and only look at function nodes, so such an identifier passed as a column: - partition pruning folded it to its submission-time value and narrowed the mutation to the partitions matching then, so rows of a partition that matches when the mutation runs were not mutated while the mutation reached is_done; - the Replicated* guard accepted it with allow_nondeterministic_mutations = 0; - mutations_execute_nondeterministic_on_initiator did not replace it. Pruning now also gives up when the analyzed ActionsDAG of the predicate holds a non-deterministic node: the analysis has already resolved the calls, including those inside ALIAS definitions and lambda bodies. The linear DAG scan is used instead of a recursive walk from the predicate node, which would revisit shared subtrees of a diamond-shaped ALIAS chain. The guard and the initiator rewrite run on the raw AST, and the rewrite has to edit it, so they share one helper that makes such calls explicit on a copy, resolving a name like the analyzer: a table column, a lambda parameter or an alias of the same scope binds it first, and an alias is not visible inside its own expression. The rewrite runs before the column commands of the same ALTER, so a column that the ALTER adds or renames binds the name as well, while a column that an earlier command of it drops or renames away does not. The guard runs after them, so it reads the metadata past the per-query cache, which still holds the snapshot taken before the ALTER. The rewrite keeps the alias of a call that it replaces with its value, so the rest of the expression can still refer to it. The apply_mutations_on_fly read path is unchanged: it re-checks already stored commands on every read. Closes ClickHouse#124002 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the column-tracking comment in parseAlterCommandSegments to one line and drop an obvious member comment. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 5 rounds, 11 findings) plus my own review (5 rounds, 10 findings).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261006-014200 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261006-005300 |
|
Workflow [PR], commit [78f6f6f] Summary: ✅
AI ReviewSummaryThis PR makes mutation validation and partition pruning recognize non-deterministic functions written without parentheses, including calls inside Final VerdictStatus: ✅ Approve. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 60 s compile time in total, 8 of them have a recent master baseline. |
|
@groeneai this seems to be an over-engineered solution. Can you just parse the functions written without parentheses as a parenthesized expression? |
|
A parser-level rewrite would change queries that use these names as columns. I agree the helper is too much, though: it re-implements the analyzer's name binding. I'll replace it with the analyzer itself: the |
Replace the AST helper that re-implemented the analyzer's name binding (columns, lambda parameters, aliases, Nested, columns added, renamed or dropped by the same ALTER) with the analyzer itself: for UPDATE and DELETE commands on Replicated tables with allow_nondeterministic_mutations = 0, the check resolves the assignments and the predicate against the current table metadata and rejects a non-deterministic function node, including a constant folded from one. A column is not followed into its ALIAS definition, as before. The mutations_execute_nondeterministic_on_initiator rewrite stays as on master: it replaces explicit calls such as today(), and a bare `today` is rejected by the check instead. The metadata is read bypassing the query metadata cache, so a column added by an earlier command of the same ALTER is seen. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Done in dcdacd5: the helper and the |
The analyzer resolves a reference to an `ALIAS` column into a `ColumnNode` whose child is the resolved definition. The walk stopped at every column, so `DELETE WHERE d < r` for `r Date ALIAS today` (or `ALIAS today()`) was still accepted on a `Replicated*` table with `allow_nondeterministic_mutations = 0`, and each replica evaluated `today` at its own time. Partition pruning already treats such a definition as non-deterministic; the check now does too. Each reference to an `ALIAS` column is a clone of its definition, so the walk is linear in the size of the tree the analyzer already built. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| ENGINE = ReplicatedMergeTree('/clickhouse/tables/{database}/t_guard', 'r1') PARTITION BY d ORDER BY k; | ||
| INSERT INTO t_guard (d, k) VALUES ('2000-01-01', 1), ('2100-01-01', 2); | ||
| SYSTEM STOP REPLICATION QUEUES t_guard; | ||
| ALTER TABLE t_guard DELETE WHERE d < today(); -- { serverError BAD_ARGUMENTS } |
There was a problem hiding this comment.
Yes. On a Replicated* table with the default allow_nondeterministic_mutations = 0, these are accepted on master and rejected with BAD_ARGUMENTS here:
- a function written without parentheses (
today,now,CURRENT_DATE,user, ...) in the predicate or in anUPDATEexpression. 26.8.10.6 accepts it, 26.3.33.24 rejects it as a missing column. - a reference to an
ALIAScolumn with a non-deterministic definition, includingr Date ALIAS today().
This line itself (today()) is rejected on master too.
I changed the changelog category to Backward Incompatible Change, and the entry says that allow_nondeterministic_mutations = 1 runs them as before. If you would rather not break them, I can drop the check change and keep only the partition-pruning fix in MergeTreeData.cpp: it accepts everything master accepts and still fixes the DELETE that reaches is_done = 1 with matching rows left, but the allow_nondeterministic_mutations part of #124002 then stays as on master. Should I drop it?
…iladic-functions-without-parentheses
`validateNonDeterministicMutationsForStorage` reports a non-deterministic function or a subquery found in the AST even when an expression of the mutation cannot be analyzed (`InterpreterAlterQuery` calls it ahead of the full validation for that reason). The analyzer-based check ran inside the same per-command loop, so a command that does not analyze threw `UNKNOWN_IDENTIFIER` before a later command's `now()` was reported: `ALTER TABLE t DELETE WHERE missing = 1, UPDATE v = toUnixTimestamp(now()) WHERE 1` on a `ReplicatedMergeTree` returned code 47 instead of 36. Resolve the expressions in a second loop, after the AST checks of every command and before the virtual column check. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A mutation is executed with the settings of the background context, which may resolve an identifier differently from the query that submitted it. With `analyzer_compatibility_prefer_alias_over_subcolumn = 1` in the query and the default in the background, `t.r` was checked as the physical column `r` but executed as the element of an `ALIAS` column `t Tuple(r ...) ALIAS tuple(rand64())`, so a non-deterministic mutation was accepted with `allow_nondeterministic_mutations = 0`. The check of virtual columns already analyzed both interpretations. Move the creation of their contexts into a helper and use it for the check of functions as well. The helper also varies `analyzer_compatibility_allow_compound_identifiers_in_unflatten_nested`, the remaining setting that changes what an identifier binds to in a single-table scope: with it disabled in the background profile, a bare `now` next to a column `now.a.b` was checked as the `Nested` column but executed as `now()`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #124002
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
ALTER UPDATE,ALTER DELETEandDELETE FROMonReplicated*tables with the defaultallow_nondeterministic_mutations = 0now reject a function written without parentheses (now,today,CURRENT_DATE,CURRENT_TIMESTAMP,user) and a reference to anALIAScolumn with a non-deterministic definition, as they already rejectednow(). Setallow_nondeterministic_mutations = 1to run them as before. Such a mutation is also no longer pruned to the partitions that match at submission time, which could skip rows. Closes #124002.Description
now,today,CURRENT_DATE,userand similar functions can be written without parentheses: the analyzer resolves such an identifier as a call when no column, alias or lambda parameter binds the name. The mutation determinism checks look only for AST function nodes, so they took it for a column:ALTER TABLE t DELETE WHERE d < todayon aReplicatedMergeTreeis pruned toblock_numbers.partition_id = ['20000101'](today()gets both). On master,DELETE WHERE ts < nowwithPARTITION BY tsreachesis_done = 1and leaves a row matchingts < now()when the mutation runs.allow_nondeterministic_mutations = 0, it is accepted on aReplicated*table (d < today()is rejected), so each replica evaluates it at its own time.Both checks now use the analyzer's resolution of the command against the table, so a column, alias or lambda parameter named
nowstill binds the name:ActionsDAGof the predicate has a non-deterministic node, including calls insideALIASdefinitions and lambdas.allow_nondeterministic_mutationscheck resolves the assignments and the predicate against the current metadata (a column added by the sameALTERis seen) and rejects a non-deterministic function, including one in anALIAScolumn definition (d < rforr Date ALIAS today()is accepted on master). Like the check of virtual columns, it analyzes the command with the name resolution settings of both the query and the background context that executes the mutation.The
mutations_execute_nondeterministic_on_initiatorrewrite is unchanged: it replacestoday(), and a baretodayis rejected by the check. Theapply_mutations_on_flyread path is not changed.The new test fails on master
4f752f7e6fcfand passes with the fix (50/50 randomized runs).Workflow [PR]
Sync PR [sync-upstream/pr/124127]