Skip to content

Skip swapping join sides with join_any_take_last_row - #112458

Merged
vdimir merged 4 commits into
masterfrom
vdimir/any_join_take_last_row_no_swap
Aug 10, 2026
Merged

vdimir merged 4 commits into
masterfrom
vdimir/any_join_take_last_row_no_swap

Conversation

@vdimir

@vdimir vdimir commented Jul 29, 2026

Copy link
Copy Markdown
Member

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

  • Skip swapping join sides with join_any_take_last_row to avoid unexpected results when settings are not respected

Close #111645

@clickhouse-gh

clickhouse-gh Bot commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [631f060]

Summary: ✅


AI Review

Summary

This PR disables join-side swapping for ANY joins when join_any_take_last_row = 1 and adds a regression test. The core direction is right, but I do not think it is ready to merge yet: the new guard can now violate the existing "prepared lookup stays on the right" invariant for reordered storage-backed joins, and the added test still does not pin the optimizer settings needed to guarantee that it actually exercises the swap path.

Findings

⚠️ Majors

  • [src/Processors/QueryPlan/Optimizations/optimizeJoin.cpp:1238] The new skip_flip_any_join guard disables all flips for ANY + join_any_take_last_row, including the mandatory has_prepared_storage_at_left normalization. The rest of the pipeline still assumes JoinStepLogicalLookup lives on the right child: buildPhysicalJoinImpl, joinRuntimeFilter, splitFilter, filterPushDown, calculateHashTableCacheKeys, and findParallelReplicasQuery all special-case only children.back() / children[1]. In a reordered multi-join with a dictionary or StorageJoin, this can now leave the lookup on the left, silently drop the prepared-storage handling, and send an unsupported plan shape through later passes.
    Suggested fix: keep the forced has_prepared_storage_at_left flip, and only suppress the size-based swap when join_any_take_last_row makes ANY swaps result-changing.
  • [tests/queries/0_stateless/04670_any_join_take_last_row_no_swap.sql:10] The regression test still does not guarantee that chooseJoinOrder runs or that the first query exercises the default 'auto' case. clickhouse-test can randomize query_plan_optimize_join_order_limit down to 0, which skips the entire swap path, and it can also randomize query_plan_join_swap_table, so this test can pass without validating the bug fix.
    Suggested fix: pin the same prerequisites as 03593_any_join_swap_tables here: query_plan_optimize_join_order_limit = 10, query_plan_optimize_join_order_randomize = 0, and an explicit query_plan_join_swap_table = 'auto' on the default query.
Final Verdict

Changes requested. Preserve the prepared-lookup right-side invariant in the optimizer and pin the regression test so CI actually covers the fixed path.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 29, 2026
CREATE TABLE t_small (k UInt32) ENGINE = MergeTree ORDER BY k;
INSERT INTO t_small VALUES (1);

-- default (query_plan_join_swap_table = 'auto'): returns 2 — WRONG

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.

This regression can still pass without exercising the swap path. clickhouse-test randomizes both query_plan_optimize_join_order_limit (including 0) and query_plan_join_swap_table for unpinned tests. When the limit comes in as 0, optimizeJoinLogicalImpl returns before chooseJoinOrder, so even the 'true' query never reaches the forced-swap code; when query_plan_join_swap_table is randomized to 'false', the first query is no longer the default-'auto' case either. Please pin the same prerequisites as 03593_any_join_swap_tables here: at least query_plan_optimize_join_order_limit = 10 and an explicit query_plan_join_swap_table = 'auto' for the first query (and ideally query_plan_optimize_join_order_randomize = 0 too).

&& (!MergeJoin::isSupported(join_operator.kind, join_operator.strictness)
|| !MergeJoin::isSupported(reverseJoinKind(join_operator.kind), join_operator.strictness));
const bool suitable_swap_only_join = isSwapOnlyJoinStrictness(join_operator.strictness) && !should_worry_about_partial_merge_join;
bool skip_flip_any_join = join_settings.join_any_take_last_row && join_operator.strictness == JoinStrictness::Any;

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.

This also suppresses the flip when has_prepared_storage_at_left is the reason we were flipping, not just the size-based ANY swap. Today that forced flip is what keeps JoinStepLogicalLookup on the right child; the rest of the pipeline still assumes that shape (buildPhysicalJoinImpl, joinRuntimeFilter, splitFilter, filterPushDown, calculateHashTableCacheKeys, findParallelReplicasQuery all inspect children.back() / children[1] as the lookup side).

With join_any_take_last_row = 1, a reordered multi-join involving a dictionary / StorageJoin can now leave the lookup on the left, so the prepared storage is silently dropped and later passes run on a plan shape they do not support. Please keep the forced has_prepared_storage_at_left normalization and only disable the size-based swap for ANY.

@clickhouse-gh

clickhouse-gh Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

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

Full report · Diff report

@vdimir

vdimir commented Aug 7, 2026

Copy link
Copy Markdown
Member Author

@groeneai tag someone from the ClickHouse team to review

@groeneai

groeneai commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

cc @davenger @antaljanosbenjamin, could you review this? @davenger has done most of the recent join-order work in optimizeJoin.cpp, and the open AI-review concern is whether suppressing the flip also drops the forced has_prepared_storage_at_left normalization that keeps JoinStepLogicalLookup on the right child, which is @antaljanosbenjamin's area in JoinStepLogical.cpp.

@antaljanosbenjamin antaljanosbenjamin self-assigned this Aug 10, 2026
@vdimir
vdimir added this pull request to the merge queue Aug 10, 2026
Merged via the queue into master with commit 517c243 Aug 10, 2026
179 checks passed
@vdimir
vdimir deleted the vdimir/any_join_take_last_row_no_swap branch August 10, 2026 16:39
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 10, 2026
leshikus pushed a commit to leshikus/ClickHouse that referenced this pull request Oct 13, 2026
04665 is claimed by two live open PRs (ClickHouse#112846, ClickHouse#112849). The collision is
silent: the filenames differ so git reports no conflict, and
check_gaps_in_tests_numbers only rejects gaps of 100 or more, never
collisions. 04666, 04670, 04674 and 04675 are claimed too, by ClickHouse#112816,
ClickHouse#112458 and unpublished branches respectively.

Co-Authored-By: Claude Fable 5 <[email protected]>
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.

query_plan_join_swap_table swap silently ignores join_any_take_last_row: ANY JOIN returns the first row instead of the last

5 participants