Repository navigation
Fix set operators (UNION/INTERSECT/EXCEPT) in subqueries inside mutations - #110196
Conversation
…ions `DELETE` and `ALTER TABLE ... UPDATE` failed with `UNION mode UNION_DEFAULT must be normalized` whenever their predicate or an assignment expression contained a set operation in a subquery, e.g. `DELETE FROM t WHERE c IN ((SELECT 1) UNION DISTINCT (SELECT 0))`. Mutation commands are stored as serialized SQL text and re-parsed on execution. Re-parsing resets set-operation nodes to their un-normalized form (`union_mode` becomes `UNION_DEFAULT`, and the in-memory `is_normalized` flag is lost), but the mutation execution path — unlike `executeQuery` — never re-ran the `SelectIntersectExceptQueryVisitor` and `NormalizeSelectWithUnionQueryVisitor` passes, so the analyzer rejected the query. Re-run the same normalization that `executeQuery` applies to top-level queries on the re-parsed mutation predicate and `UPDATE` assignment expressions. This affects both the new and old analyzer and covers `UNION`, `UNION ALL`, `UNION DISTINCT`, `EXCEPT` and `INTERSECT` for MergeTree as well as the engines (`KeeperMap`, `Redis`) that mutate through `MutationsInterpreter`. Closes: #72853 Co-Authored-By: Claude Fable 5 <[email protected]>
|
Workflow [PR], commit [6cf60e6] Summary: ✅
AI ReviewSummaryThis PR re-runs set-operation normalization on re-parsed mutation predicates and Final Verdict✅ No new findings. Approve. |
The Style check failed with `Gap (4515, 5020) > 100`: test number `05020` was too far ahead of the highest existing test (`04515`). Renumber to `04560` so the gap stays within the allowed threshold. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Address review on #110196: the previous change re-ran set-operation normalization (`UNION`/`INTERSECT`/`EXCEPT`) only on the main mutation execution path in `MutationsInterpreter`. Two other consumers reparse the same serialized mutation command and feed its predicate / `UPDATE` assignment expressions straight into `buildQueryTree` without normalization, so a mutation that legitimately contains a set operation in a subquery could still fail with `UNION mode UNION_DEFAULT must be normalized`: - `getUpdateAffectedColumns` in `PatchPartsLock.cpp`, used by lightweight `UPDATE` with the default `update_parallel_mode = 'auto'`. - `MergeTreeData::checkDropOrRenameCommandDoesntAffectInProgressMutations`, the guard that runs when a concurrent `ALTER ... DROP COLUMN` / `RENAME COLUMN` checks whether an unfinished mutation references the column. Expose `normalizeSetOperations` from `MutationsInterpreter.h` and call it in both places before building the query tree. Extend `04560_union_subqueries_in_mutations` to cover the lightweight-update path and the in-progress-mutation `DROP` / `RENAME COLUMN` guard. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 49/50 (98.00%) · Uncovered code |
Issue ClickHouse#81135 reported that `DELETE FROM t0 WHERE (SELECT true UNION DISTINCT SELECT true)` with `validate_mutation_query = 0` failed with `UNION mode UNION_DEFAULT must be normalized`. It no longer reproduces, so this adds a test only, with no source change. Mutation commands are stored as serialized SQL text and re-parsed when the mutation executes on a part. Re-parsing reset set-operation nodes to `UNION_DEFAULT` and lost the in-memory normalized flag, and the mutation path never re-ran the normalization that `executeQuery` applies to top-level queries, so `UnionNode` rejected the re-parsed command. The exception surfaced asynchronously through `waitForMutation` and `checkMutationStatus`. This was fixed by ClickHouse#110196, which closed ClickHouse#72853; @ PedroTadim suspected that relationship in the issue thread and was correct. The test is a real regression test rather than a tautology. On 25.8.29.39 it fails with `Code: 341` wrapping `Code: 36 ... UNION mode UNION_DEFAULT must be normalized`, at the same `checkMutationStatus` and `waitForMutation` frames the issue reports, and on master it passes with output matching the reference. Both `validate_mutation_query` arms are covered because they failed differently before the fix: `0` failed during execution on the part, and the default failed earlier at validation. `lightweight_deletes_sync = 1` is pinned because it is the setting that decides whether anything waits for the mutation, and the exception surfaced only while the mutation executed on the part. With `0` the exception is hidden. Pinning `mutations_sync` would have no effect, because `DELETE FROM` overwrites it from `lightweight_deletes_sync`. `04560_union_subqueries_in_mutations` already pins this root cause, but only for the `IN` position; nothing in the suite covered a set operation in a bare scalar-subquery predicate. Closes: ClickHouse#81135 Co-Authored-By: Claude Fable 5 <[email protected]>
Address review on ClickHouse#110196: the previous change re-ran set-operation normalization (`UNION`/`INTERSECT`/`EXCEPT`) only on the main mutation execution path in `MutationsInterpreter`. Two other consumers reparse the same serialized mutation command and feed its predicate / `UPDATE` assignment expressions straight into `buildQueryTree` without normalization, so a mutation that legitimately contains a set operation in a subquery could still fail with `UNION mode UNION_DEFAULT must be normalized`: - `getUpdateAffectedColumns` in `PatchPartsLock.cpp`, used by lightweight `UPDATE` with the default `update_parallel_mode = 'auto'`. - `MergeTreeData::checkDropOrRenameCommandDoesntAffectInProgressMutations`, the guard that runs when a concurrent `ALTER ... DROP COLUMN` / `RENAME COLUMN` checks whether an unfinished mutation references the column. Expose `normalizeSetOperations` from `MutationsInterpreter.h` and call it in both places before building the query tree. Extend `04560_union_subqueries_in_mutations` to cover the lightweight-update path and the in-progress-mutation `DROP` / `RENAME COLUMN` guard. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ries-in-mutations Fix set operators (UNION/INTERSECT/EXCEPT) in subqueries inside mutations
DELETEandALTER TABLE ... UPDATEmutations failed withUNION mode UNION_DEFAULT must be normalizedwhenever their predicate or an assignment expression contained a set operation in a subquery, for example:Mutation commands are stored as serialized SQL text and re-parsed on execution. Re-parsing resets set-operation nodes (
UNION/INTERSECT/EXCEPT) to their un-normalized form (union_modebecomesUNION_DEFAULTand the in-memoryis_normalizedflag is lost), but the mutation execution path — unlikeexecuteQuery— never re-ran theSelectIntersectExceptQueryVisitorandNormalizeSelectWithUnionQueryVisitorpasses, so the analyzer rejected the query.The fix re-runs the same normalization that
executeQueryapplies to top-level queries on the re-parsed mutation predicate andUPDATEassignment expressions. It affects both the new and old analyzer and coversUNION,UNION ALL,UNION DISTINCT,EXCEPTandINTERSECTforMergeTreeas well as the engines (KeeperMap,Redis) that mutate throughMutationsInterpreter.Closes: #72853
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed an exception (
UNION mode UNION_DEFAULT must be normalized) when aDELETEorALTER UPDATEmutation used a set operation (UNION/UNION ALL/UNION DISTINCT/EXCEPT/INTERSECT) inside a subquery in itsWHEREcondition or in anUPDATEassignment.