Skip to content

avoid trying cross join first in greedy join reordering - #106066

Closed
vdimir wants to merge 1 commit into
masterfrom
vdimir/greedy-chooses-cross-join
Closed

vdimir wants to merge 1 commit into
masterfrom
vdimir/greedy-chooses-cross-join

Conversation

@vdimir

@vdimir vdimir commented May 28, 2026

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...

@clickhouse-gh

clickhouse-gh Bot commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f8b82f5]

Summary: ❌

job_name test_name status info comment
Fast test FAIL
03835_join_transitive_predicates_edge_cases FAIL cidb
02000_join_on_const FAIL cidb
02001_join_on_const FAIL cidb
03786_outer_to_semi_or_anti FAIL cidb
03519_left_to_cross_incorrect FAIL cidb
01101_literal_column_clash FAIL cidb
03518_left_to_cross_incorrect FAIL cidb
03520_left_to_cross_incorrect FAIL cidb
03405_merge_filter_into_join FAIL cidb
03524_sign_argument FAIL cidb
Build (amd_debug) DROPPED
Build (amd_asan_ubsan) DROPPED
Build (amd_tsan) DROPPED
Build (amd_msan) DROPPED
Build (amd_binary) DROPPED
Build (arm_debug) DROPPED
Build (arm_asan_ubsan) DROPPED
Build (arm_tsan) DROPPED
Build (arm_msan) DROPPED

AI Review

Summary

This PR changes greedy join reordering so disconnected component pairs are skipped in the normal pair scan and only considered by the fallback path. That avoids choosing a CROSS JOIN too early in simple cases, but the fallback does not preserve the same validity search, so valid mixed CROSS JOIN / outer-join queries can now fail during optimization.

Findings

❌ Blockers

  • [src/Processors/QueryPlan/Optimizations/joinOrder.cpp:585] Skipping every disconnected pair makes the fallback below responsible for legal CROSS JOIN choices, but that fallback validates only the two smallest components. buildQueryGraph can record an outer-joined relation as depending on multiple other relations, for example t0, t1 LEFT JOIN t2 ON t0.k = t2.k AND t1.k = t2.k; the only legal first disconnected step is t0 CROSS JOIN t1. If statistics make t2 one of the two smallest components, the fallback checks t2 with only one dependency, isValidJoinOrder rejects it, and the optimizer raises Join restriction violated for a valid query. Pick the cheapest valid disconnected pair in the fallback instead of validating only the two globally smallest components.
Tests
  • ⚠️ Please add a focused regression test for greedy join reordering with a disconnected CROSS JOIN that is required before an outer-joined relation whose ON expression depends on both preserved-side relations. The test should fail if the fallback still checks only the two smallest components.
Final Verdict

Status: ❌ Block

Minimum required action: make the disconnected fallback scan/select a valid pair under isValidJoinOrder, then cover the mixed CROSS JOIN / outer-join case with a regression test.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label May 28, 2026
bool connected = !edge.empty()
|| query_graph.areTransitivelyConnected(left->relations, right->relations);
if (!connected && best_plan)
if (!connected)

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 makes the fallback below responsible for all disconnected joins, but that fallback picks the two smallest components before checking whether that pair is legal. For example, in t0, t1 LEFT JOIN t2 ON t0.k = t2.k AND t1.k = t2.k, buildQueryGraph records t2 as depending on both t0 and t1; the only valid first step is the disconnected t0 CROSS JOIN t1. If stats make t2 one of the two smallest components, the fallback chooses t2 with only one dependency, isValidJoinOrder returns false, and the optimizer raises Join restriction violated for a valid query. Please make the disconnected fallback scan/select the best valid pair instead of validating only the two smallest components.

@clickhouse-gh

clickhouse-gh Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Dear @vdimir, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@abashkeev abashkeev added the comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... label Sep 4, 2026
@vdimir vdimir closed this Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants