Skip to content

Fix set operators (UNION/INTERSECT/EXCEPT) in subqueries inside mutations - #110196

Merged
alexey-milovidov merged 7 commits into
masterfrom
fix-union-subqueries-in-mutations
Jul 16, 2026
Merged

alexey-milovidov merged 7 commits into
masterfrom
fix-union-subqueries-in-mutations

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

DELETE and ALTER TABLE ... UPDATE mutations failed with UNION mode UNION_DEFAULT must be normalized whenever their predicate or an assignment expression contained a set operation in a subquery, for example:

DELETE FROM t WHERE c IN ((SELECT 1) UNION DISTINCT (SELECT 0));
ALTER TABLE t UPDATE c = (SELECT sum(x) FROM ((SELECT 1 AS x) UNION DISTINCT (SELECT 2 AS x))) WHERE c = 1;

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_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.

The fix re-runs the same normalization that executeQuery applies to top-level queries on the re-parsed mutation predicate and UPDATE assignment expressions. It 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

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

Fixed an exception (UNION mode UNION_DEFAULT must be normalized) when a DELETE or ALTER UPDATE mutation used a set operation (UNION/UNION ALL/UNION DISTINCT/EXCEPT/INTERSECT) inside a subquery in its WHERE condition or in an UPDATE assignment.

…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]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6cf60e6]

Summary: ✅


AI Review

Summary

This PR re-runs set-operation normalization on re-parsed mutation predicates and UPDATE assignment expressions, and extends that fix to the two additional consumers that still build query trees from serialized mutation commands: lightweight UPDATE column analysis in PatchPartsLock and the in-progress mutation dependency check in MergeTreeData. I did not find any remaining correctness issues in the current head. The earlier inline gaps are fixed, and the added stateless coverage now exercises the main mutation path, lightweight updates, and the concurrent DROP COLUMN guard. The only residual testing gap is that KeeperMap and Redis are covered indirectly through the shared MutationsInterpreter path rather than with engine-specific regressions.

Final Verdict

✅ No new findings. Approve.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 13, 2026
alexey-milovidov and others added 2 commits July 13, 2026 05:40
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]>
alexey-milovidov and others added 4 commits July 13, 2026 11:36
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]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.20% 85.90% +2.70%
Functions 92.50% 92.70% +0.20%
Branches 75.40% 78.10% +2.70%

Changed lines: Changed C/C++ lines covered: 49/50 (98.00%) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acceptable.

@alexey-milovidov alexey-milovidov self-assigned this Jul 16, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 16, 2026
Merged via the queue into master with commit 0b5bd4d Jul 16, 2026
179 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-union-subqueries-in-mutations branch July 16, 2026 15:49
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 16, 2026
pull Bot pushed a commit to CrazyForks/ClickHouse that referenced this pull request Jul 30, 2026
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]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
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]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ries-in-mutations

Fix set operators (UNION/INTERSECT/EXCEPT) in subqueries inside mutations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Union subqueries inside mutation queries

2 participants