Skip to content

Fix PREWHERE after rewriting a sharded `GLOBAL RIGHT JOIN - #119780

Merged
harikrishnan94 merged 7 commits into
ClickHouse:masterfrom
ianton-ru:fix-global-right-join-prewhere
Sep 28, 2026
Merged

harikrishnan94 merged 7 commits into
ClickHouse:masterfrom
ianton-ru:fix-global-right-join-prewhere

Conversation

@ianton-ru

@ianton-ru ianton-ru commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Fix PREWHERE after rewriting a sharded GLOBAL RIGHT JOIN

Closes: #119621
Related: #118611

After #118611, a sharded GLOBAL RIGHT JOIN can be rewritten as a GLOBAL LEFT JOIN so the preserved side is not broadcast. PREWHERE is bound to a table before that swap. When the filter belonged to the original left table, it followed that table into a Memory temporary table and the shard failed with ILLEGAL_PREWHERE.
The filter is now moved into the temporary-table subquery when its columns come from the broadcast table, so the join still runs on the shards. A column-less PREWHERE (for example PREWHERE 0) has no table to recognize after the swap, so that case declines the rewrite and the initiator-side wrapper keeps the filter. Rewriting PREWHERE to WHERE would be wrong here: with join_use_nulls = 1, PREWHERE on one side of an outer join must run before the join.
The same ownership check is used when a remote table is wrapped in a subquery (including a remote non-leftmost table). Previously that wrap could drop PREWHERE that belonged to the wrapped table.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry:

Fixed ILLEGAL_PREWHERE on sharded GLOBAL RIGHT JOIN queries that used PREWHERE on the left table. The filter is applied to the broadcast side before the join instead of being sent to a Memory temporary table that does not support PREWHERE. PREWHERE on a remote table in a join is also preserved when that table is read through a subquery wrap.


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

Version info

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

ianton-ru and others added 3 commits September 12, 2026 21:36
The filter was bound to the left `Distributed` table before the join sides were swapped, then followed that table into a `Memory` temporary table which does not support `PREWHERE`. Relocate it into the broadcast subquery when that table owns it, keep it on the initiator wrap otherwise, and preserve it when a remote non-leftmost table is wrapped.

https: //github.com/ClickHouse/issues/119621
https: //github.com/ClickHouse/pull/118611
Co-authored-by: Cursor <[email protected]>
Cover the original no-op `PREWHERE` reproducer and preserve filters when a remote non-leftmost table is wrapped for a join.

https: //github.com/ClickHouse/issues/119621
https: //github.com/ClickHouse/pull/118611
Co-authored-by: Cursor <[email protected]>
@ianton-ru
ianton-ru marked this pull request as ready for review September 13, 2026 12:00
@clickhouse-gh clickhouse-gh Bot added the comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... label Sep 13, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Sep 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [dc1e79a]

Summary: ✅


AI Review

Summary

This PR keeps PREWHERE attached to the table that actually executes it after GLOBAL RIGHT JOIN rewrites and after remote-table subquery wrapping. The current diff also closes the two earlier regressions around duplicate nested GLOBAL IN materialization and stale outer prewhere_filter_actions. The added stateless distributed tests cover the main execution shapes touched here. I did not find any remaining blocker or major issue in the current code.

Final Verdict

✅ No new review findings.

LLVM Coverage Report

Measured on commit dc1e79a.

Metric Baseline Current Δ
Lines 87.90% 87.90% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.30% 79.30% +0.00%

Changed lines: Changed C/C++ lines covered: 102/103 (99.03%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 13, 2026
Comment thread src/Storages/buildQueryTreeForShard.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing dc1e79a16 with master ab6b45e4f (stripped binary size, per-symbol sizes and ThinLTO time; 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

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

Job report

@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Sep 14, 2026
After a sharded `GLOBAL RIGHT JOIN` relocates `PREWHERE` into the broadcast subquery, collected nested `GLOBAL IN` / `GLOBAL JOIN` nodes still point at the detached original tree. Skip those nodes so `executeSubqueryNode` does not prepare a second, unused temp table (and a not-ready `Set`).

https: //github.com/ClickHouse/pull/119780
Co-authored-by: Cursor <[email protected]>
Comment thread src/Storages/buildQueryTreeForShard.cpp Resolved
Otherwise `appendSetsFromActionsDAG` still treats the original `IN` sets as useful after the nested planner built a separate copy, which can leave a not-ready `Set`.

https: //github.com/ClickHouse/pull/119780
Co-authored-by: Cursor <[email protected]>
@harikrishnan94 harikrishnan94 self-assigned this Sep 18, 2026
@harikrishnan94

harikrishnan94 commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

@ianton-ru Thanks for the PR!

Comment thread src/Storages/buildQueryTreeForShard.cpp Outdated

subquery_query_node->getPrewhere() = parent_query_node->getPrewhere()->clone();
parent_query_node->getPrewhere() = {};
if (auto * table_expression_data = planner_context->getTableExpressionDataOrNull(prewhere_table_expression))

@harikrishnan94 harikrishnan94 Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this reset never runs. PlannerContext finds table data by node pointer, but every caller passes a copy of the tree. So the initiator still counts the original PREWHERE sets as needed. Could you remove these lines, or do the reset on the planner side?

Comment thread src/Storages/buildQueryTreeForShard.cpp Outdated
void enterImpl(QueryTreeNodePtr & node)
{
if (node->as<QueryNode>())
query_node_stack.push_back(node);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

query_node_depth is now always equal to query_node_stack.size(), because both change under the same condition in enterImpl and leaveImpl. Can we drop the counter and use query_node_stack.size() instead? The two blocks below that build the entry would then be the same, so they could share one small helper.

@harikrishnan94 harikrishnan94 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@harikrishnan94
harikrishnan94 added this pull request to the merge queue Sep 28, 2026
Merged via the queue into ClickHouse:master with commit f5463dc Sep 28, 2026
149 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 28, 2026
ianton-ru added a commit to Altinity/ClickHouse that referenced this pull request Oct 1, 2026
After the join is rewritten as a LEFT JOIN, a `PREWHERE` bound to the original left table followed it into a `Memory` table and shards threw `ILLEGAL_PREWHERE`. Move the predicate into the broadcast subquery when its columns belong to that table, and skip the swap when `PREWHERE` has no column so the initiator wrapper keeps the filter. When wrapping a remote table that owns `PREWHERE`, move the full predicate onto the wrap and drop the initiator actions so nested `GLOBAL IN` sets are not materialized twice.

Port of ClickHouse#119780

Co-authored-by: Cursor <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... external Author is not a member of the ClickHouse organization 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.

GLOBAL RIGHT JOIN swap moves PREWHERE'd left table into a Memory temp table

4 participants