Skip to content

Ignore WITH TOTALS of an IN subquery rewritten by rewrite_in_to_join - #124096

Merged
alexey-milovidov merged 4 commits into
masterfrom
fix-rewrite-in-to-join-with-totals
Oct 8, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
fix-rewrite-in-to-join-with-totals

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

With rewrite_in_to_join = 1, x IN (subquery) is rewritten into a correlated EXISTS. The regular IN set ignores the totals row of a subquery with WITH TOTALS, but the rewrite kept the subquery's TotalsHaving step on the right side of the join built by the decorrelation. As a result:

  • the totals row leaked into the result of an outer query that has no WITH TOTALS (wrong result), e.g. SELECT number FROM numbers(3) WHERE number IN (SELECT number FROM numbers(10) GROUP BY number WITH TOTALS);
  • when the outer query had WITH TOTALS itself (e.g. ... GROUP BY number WITH TOTALS HAVING number IN (SELECT ... WITH TOTALS)), it failed with the LOGICAL_ERROR exception Totals having transform was already added to pipeline.

The fix drops WITH TOTALS from the subquery when the rewrite applies, so the result matches rewrite_in_to_join = 0.

Found by the AST fuzzer in #118315: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118315&sha=eaa8e0b5913bdc2bf50caccbefee4e1589987921&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29 (it also reproduces on master).

Related: #118315

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

Fix a wrong result (a leaked totals row) and a LOGICAL_ERROR exception with rewrite_in_to_join when the subquery of IN uses WITH TOTALS.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.1793-master (included in 26.10 and later)

…join`

The regular `IN` set ignores the totals row of its subquery, but the `rewrite_in_to_join`
rewrite into a correlated `EXISTS` kept the `TotalsHaving` step on the right side of the
join built by the decorrelation. That leaked the totals row into the outer query and,
when the outer query had `WITH TOTALS` as well, failed with the `LOGICAL_ERROR`
`Totals having transform was already added to pipeline`.

Found by the AST fuzzer in #118315:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118315&sha=eaa8e0b5913bdc2bf50caccbefee4e1589987921&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29

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

clickhouse-gh Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [86bdaae]

Summary: ✅


AI Review

Summary

This PR makes the rewrite_in_to_join path drop WITH TOTALS recursively across the rewritten RHS subquery join tree so it matches regular IN semantics, including nested joined and UNION-based subqueries. The current code addresses the previously reported nested-totals leak, the added stateless regression test covers both the original reproducer and the nested shapes, and the Praktika report for PR 124096 is all green; I did not find any remaining correctness or safety issues in the changed surface.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 86bdaae.

Metric Baseline Current Δ
Lines 88.30% 88.20% -0.10%
Functions 91.80% 92.10% +0.30%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 8/8 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 5, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). label Oct 5, 2026
alexey-milovidov and others added 3 commits October 6, 2026 15:02
…subquery rewritten by `rewrite_in_to_join`

The regular `IN` ignores the totals of the whole subquery plan, so totals of
queries nested in the join tree of the subquery have to be dropped too.

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

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=124096&sha=f45b63faee323184e9de7ace4a9b0347e66f2b2c&name_0=PR&name_1=Upgrade%20check%20%28amd_release%29 (Database test_3 is being dropped or detached, will not restart replica / unkilled mutation on t_update_materialized) and provide a fix in a separate PR. It is not related to this PR (an analyzer change). If the fix is already in progress (possibly #123841), link it here.

@groeneai

groeneai commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

This is the lock holder that #123214 removed, but on the previous release: the stress phase runs the v26.9.11.2 server and tests, and 26.9 does not have #123214. In this job, the DROP TABLE t_resize_object_storage (an S3 table on localhost:19999) in 26.9's 05059_storage_read_output_resize_clamped_to_max_threads was skipped by ignore_drop_queries_probability. It ran TRUNCATE instead and retried S3 until the server stopped (attempt 239/501). 03053_analyzer_join_alias then ran DROP DATABASE IF EXISTS test_3, which shut t_update_materialized down and blocked behind that TRUNCATE. While a DROP DATABASE is running, SYSTEM RESTART REPLICA refuses with ABORTED, so the read-only replica could not be recovered and its mutation could not be killed.

The fix is #123841. Its KILL QUERY WHERE 1 SYNC cancels both queries (the S3 retry loop stops on cancellation), and the recovery step then restarts the replica and kills the mutation. I checked it on a local rig with the official 26.9.11.2 server and these two fixtures. With master's upgrade_runner.sh, 3 of 3 runs give all three FAIL rows of this job. With #123841, 3 of 3 runs pass (The stress phase left no unfinished mutation to the upgrade) in 3 s.

The stress phase stops doing this once a 26.9 release has #123214. I asked for v26.9-must-backport there: #123214 (comment)

@clickhouse-gh

clickhouse-gh Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 86bdaae33 with master 2788f1048 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of f1481ae11; 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.

Compile time of recompiled translation units

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

Job report

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Thanks, @groeneai. Understood: the Upgrade check failure comes from the 26.9 stress phase and is fixed by #123841 (and, once backported, #123214). The rerun of this PR's CI is green, so nothing to change here.

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

Perfect.

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

Labels

comp-query-analyzer Semantic analysis + logical/physical planning + rewrites (Analyzer and Planner modules). 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.

3 participants