Repository navigation
Fix PREWHERE after rewriting a sharded `GLOBAL RIGHT JOIN - #119780
Conversation
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]>
|
Workflow [PR], commit [dc1e79a] Summary: ✅
AI ReviewSummaryThis PR keeps Final Verdict✅ No new review findings. LLVM Coverage ReportMeasured on commit dc1e79a.
Changed lines: Changed C/C++ lines covered: 102/103 (99.03%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units104 translation units recompiled, 729 s compile time in total, 104 of them have a recent master baseline. |
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]>
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]>
|
@ianton-ru Thanks for the PR! |
|
|
||
| subquery_query_node->getPrewhere() = parent_query_node->getPrewhere()->clone(); | ||
| parent_query_node->getPrewhere() = {}; | ||
| if (auto * table_expression_data = planner_context->getTableExpressionDataOrNull(prewhere_table_expression)) |
There was a problem hiding this comment.
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?
| void enterImpl(QueryTreeNodePtr & node) | ||
| { | ||
| if (node->as<QueryNode>()) | ||
| query_node_stack.push_back(node); |
There was a problem hiding this comment.
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.
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]>
Fix
PREWHEREafter rewriting a shardedGLOBAL RIGHT JOINCloses: #119621
Related: #118611
After #118611, a sharded
GLOBAL RIGHT JOINcan be rewritten as aGLOBAL LEFT JOINso the preserved side is not broadcast.PREWHEREis bound to a table before that swap. When the filter belonged to the original left table, it followed that table into aMemorytemporary table and the shard failed withILLEGAL_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 examplePREWHERE 0) has no table to recognize after the swap, so that case declines the rewrite and the initiator-side wrapper keeps the filter. RewritingPREWHEREtoWHEREwould be wrong here: withjoin_use_nulls = 1,PREWHEREon 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
PREWHEREthat belonged to the wrapped table.Changelog category (leave one):
Changelog entry:
Fixed
ILLEGAL_PREWHEREon shardedGLOBAL RIGHT JOINqueries that usedPREWHEREon the left table. The filter is applied to the broadcast side before the join instead of being sent to aMemorytemporary table that does not supportPREWHERE.PREWHEREon 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
26.10.1.930-master(included in26.10and later)