Repository navigation
Extract ConstantJoin from HashJoin - #108289
Conversation
|
Workflow [PR], commit [69a2538] Summary: ✅
AI ReviewSummaryThis PR extracts cartesian and constant-predicate joins into Findings
Final Verdict
|
03786_outer_to_semi_or_anti: ConstantJoin handled matched LEFT SEMI as “emit left row + default right columns”. That broke optimizer-converted ANY LEFT JOIN -> LEFT SEMI when a removed post-join filter still needed the matched right column value internally. HashJoin semantics for SEMI are “emit a matching row from the other side”, not defaults. 03788_hash_join_with_empty_right_side_skip_left_side: missing implementation of alwaysReturnsEmptySet
Goals: - make it behave as HashJoin with any joins - simplify logic
b158e7d to
50f93cf
Compare
| INSERT INTO constant_join_algorithm_t1 VALUES (1), (2); | ||
| INSERT INTO constant_join_algorithm_t2 VALUES (2), (3); | ||
|
|
||
| -- Constant joins use ConstantJoin independently of join_algorithm. |
There was a problem hiding this comment.
This is a behavioral change: previosly we returned NOT_IMPLEMENTED is the hash join algorithm wasn't enabled.
| any_left_true_take_last | ||
| 1 20 | ||
| 2 20 |
There was a problem hiding this comment.
Previously this returned 1 10 and 2 10, beause join_any_take_last_row wasn't considered for constant joins. In another PR I fixed the usage of join_any_take_last_row and there and it should return the same.
|
Because there was a merge conflict in sync PR, I have to merge latest master. |
| { | ||
| /// Probe streams race here: exactly one matching block keeps its first row, the losers emit nothing. | ||
| bool expected = false; | ||
| if (has_seen_matching_rows.compare_exchange_strong(expected, true)) |
There was a problem hiding this comment.
Once FirstRowOnly wins this CAS, the result is already fully determined, but nothing tells JoiningTransform to stop reading the remaining probe side. Queries like SELECT count() FROM huge_l ANY INNER JOIN (SELECT 1 AS r) rt ON 1 SETTINGS enable_analyzer = 1 will still scan all of huge_l even though every later probe block can only return ConstantJoinEmptyResult; the same wasted scan happens for RIGHT ANY / RIGHT SEMI with a non-empty right side.
Please add a post-first-match short-circuit (for example via a dedicated IJoin/JoiningTransform hook that cancels the left side once this flag is set) and cover it with a focused read_rows regression.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 687/734 (93.60%) · Uncovered code |
…ouse#108289 design Detect the constant-false ON condition and empty inputs on the logical join step (where they are still visible, before splitFilter lowers them), but keep the JoinStep in place and only replace the input sides that cannot contribute a row with an empty ReadNothing source. This preserves join validation (an invalid constant join over a Join-engine table still throws instead of being silently short-circuited, fixing the 02498_storage_join_key_positions regression the previous whole-join-replacement approach introduced) while still avoiding the read of the non-contributing side. The runtime ConstantJoin from ClickHouse#108289 only cancels the probe side after the build side is filled, so the build side of a LEFT/LEFT ANTI join over a large right table was fully read; this optimization removes it from the plan (read_rows 20000010 -> 10 for such queries). Co-Authored-By: Claude Opus 4.8 <[email protected]>
Master extracted cross joins out of `HashJoin` into `ConstantJoin` (#108289); the reasoning in `getMinRequiredVersion` is unchanged, only the class name in the comment was stale.
…ws-assert Resolve the conflict in src/Interpreters/HashJoin/HashJoin.cpp in favour of master. PR ClickHouse#108289 extracted the cross join into src/Interpreters/ConstantJoin.cpp and the code this PR changed no longer exists in HashJoin::addBlockToJoin: the spill branch became ConstantJoin::trySpillRightBlock, which already bails out on block_to_save.columns() == 0, and the two chassert(rows == block.rows()) guards are gone. Both fixes of this PR are therefore already upstream, so only the regression test remains.
|
@antaljanosbenjamin a setting for this optimization could help on testing? |
There is no setting here. If you write join conditions that are constant (eg.: |
The hoisted structure guard in optimizeUseNormalProjections was verified against one more carrier of the same defect, found by the AST fuzzer on an unrelated pull request (STID 0993-3a3c, AST fuzzer (amd_tsan) on ClickHouse#92472; also seen in Stress test (arm_debug) on ClickHouse#108289). Same root cause as the case already covered: ActionsDAG::updateHeader appends every input the rebuilt query.dag does not consume, so unconsumed columns of required_columns survive as pass-throughs and widen the projection-rewritten stream past the header of the subplan it replaces. What differs is the consumer. Here the leaked columns are real (not virtual), and the parent is the right side of the inner correlated-decorrelation join, whose contract is a single column (payload and id belong to the outer stream). So instead of the output port check in JoiningTransform::prepare, the widened block trips the right-side structure assert in ConstantJoin::addBlockToJoin. Which of the two fires depends only on the settings: correlated_subqueries_use_in_memory_buffer = 0 gives the ConstantJoin one, additionally setting correlated_subqueries_default_join_kind to 'left' gives the output port one. Reproduced on unmodified master for both asserts, byte-identical to the CI report (message, column dump including the sparse id column, and the whole frame chain). It only surfaces when query_plan_remove_unused_columns is off, because that optimization re-narrows the stream; CI reached it through compatibility = '25.3'. No source change was needed: removing only the hoisted blocksHaveEqualStructure block from the fixed build makes both asserts fire again with the original messages, and restoring it rebuilds bit-identically, which is what proves the existing guard is what covers this carrier. The check was deliberately made on the branch merged with current master rather than by comparing prebuilt binaries of different vintages, since ConstantJoin.cpp does not exist at this branch's original merge base. The test pins query_plan_remove_unused_columns = 0 (otherwise the check is vacuous), keeps id sparse-serialized and asserts that it is, and compares projection-on against projection-off results so the skip path is shown to lose no rows. No randomization tags are needed: 100 randomized runs pass. While verifying this I confirmed that issue ClickHouse#112128 (correlated EXISTS under OR with full_sorting_merge) is a different producer of the same symptom, since it is unaffected by optimize_use_projections = 0. It is tracked separately.
Extract ConstantJoin from HashJoin
…ouse#108289 design Detect the constant-false ON condition and empty inputs on the logical join step (where they are still visible, before splitFilter lowers them), but keep the JoinStep in place and only replace the input sides that cannot contribute a row with an empty ReadNothing source. This preserves join validation (an invalid constant join over a Join-engine table still throws instead of being silently short-circuited, fixing the 02498_storage_join_key_positions regression the previous whole-join-replacement approach introduced) while still avoiding the read of the non-contributing side. The runtime ConstantJoin from ClickHouse#108289 only cancels the probe side after the build side is filled, so the build side of a LEFT/LEFT ANTI join over a large right table was fully read; this optimization removes it from the plan (read_rows 20000010 -> 10 for such queries). Co-Authored-By: Claude Opus 4.8 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added
ConstantJoinfor cartesian joins and analyzer-planned constant-predicate joins, so these queries no longer fail because of an incompatiblejoin_algorithmsetting when the analyzer is used.Version info
26.8.1.93(included in26.8and later)