Repository navigation
Fix constant filter conjunct pushed to non-preserved side of OUTER JOIN - #107084
alexey-milovidov merged 1 commit into
Conversation
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-10:20260610-195500 |
|
cc @vdimir @novikd — could you review this? It fixes a wrong result in OUTER JOIN filter push-down: a pure-constant WHERE conjunct (e.g. |
|
Workflow [PR], commit [a8177b3] Summary: ✅ AI ReviewSummaryThis PR fixes a wrong-result bug in Final VerdictStatus: ✅ Approve |
3500e5e to
709ad2a
Compare
Pre-PR validation gate (updated for HEAD
|
| # | Question | Answer |
|---|---|---|
| a | Deterministic repro? | Yes. 03594: with real data the EXPLAIN step output was Join/Join/Filter/Filter/... (extra Filter steps) on the previous HEAD vs the reference Join/Join/Filter. #106949: WHERE and(p,1) QUALIFY NULL over a Join(ALL,RIGHT) table returns 2 wrong rows on master, 0 with the fix. Both reproduce on demand on a debug build. |
| b | Root cause explained? | Yes. getConjunctionNodes() classifies a conjunct as pushable to a side when all its inputs are allowed inputs of that side; a no-input conjunct (constant 1, folded NULL) trivially satisfies 0==0 and is pushed to every side. For an OUTER join the non-preserved side is disabled (empty allowed-input set); pushing the constant there and dropping it from the post-join filter lets a falsy/NULL constant filter out that side, the join fabricates non-matched rows, and they escape the now constant-free post-join filter. The over-broad first version also blocked the constant on enabled sides, which removed a legitimate push-down 03594 checks. |
| c | Fix matches root cause? | Yes. The constant is dropped from a side's push-down set only when that side is disabled (allowed_nodes empty); enabled (preserved) sides keep the existing classification, because pushing a constant to a preserved side is equivalent to keeping it post-join. The both_streams set is gated when either side is disabled. No bounds widened, no no-random-* tags, no data reduction. |
| d | Test intent preserved / new tests added? | Yes. New regression test 04335_storage_join_right_join_const_conjunct_pushdown (the QUALIFY NULL and and(p,1) QUALIFY NULL forms: 2 wrong rows on master, 0 with fix) plus the baseline. The pre-existing 03594_push_more_filters_down_joins is unchanged and now passes (plan back to Join/Join/Filter, JoinResultRowCount=0). I removed one query from 04335 (WHERE and(left_pred,1,NULL) without QUALIFY): on master it also returns wrong rows, but with the fix the same query reaches a pre-existing, unrelated StorageJoin composite-key limitation (UNSUPPORTED_JOIN_KEYS: keys128 in JoinSource::createChunk), so it is not a clean demonstration of this fix and was dropped. |
| e | Both directions demonstrated? | Yes. #106949 and(p,1) QUALIFY NULL: 2 rows without fix, 0 with fix. 03594 EXPLAIN: extra Filter steps without fix, Join/Join/Filter with fix, JoinResultRowCount=0 both. All on the rebuilt debug binary. |
| f | Fix is general, not a narrow patch? | Yes. The gate is applied symmetrically to the left, right, and both-streams push-down sets in splitActionsForJOINFilterPushDown, so it covers LEFT/RIGHT/FULL OUTER joins and equivalent-set pushes, not just the one reproduced shape. It is not a defensive symptom guard: it corrects where the conjunct is placed (post-join filter vs disabled-side input). Regression set still green: 03130_convert_outer_join_to_inner_join, 04075_join_reorder_outer_join_filter_pushdown, 02861_filter_pushdown_const_bug, 03036_join_filter_push_down_equivalent_sets, 03143_join_filter_push_down_filled_join_fix, 03095_window_functions_qualify, 00118/00119_storage_join, 02382_join_and_filtering_set all pass. |
Session id: cron:clickhouse-worker-slot-22:20260610-231000
709ad2a to
032515d
Compare
A WHERE filter that is a conjunction containing a constant term, such as `p AND 1` or anything that folds a NULL literal into the predicate (for example `WHERE p QUALIFY NULL`, where the analyzer merges the QUALIFY NULL into the WHERE filter), produced wrong results over a RIGHT JOIN whose right side is a `Join` engine table. getConjunctionNodes() in splitActionsForJOINFilterPushDown classifies a conjunct as pushable to a join side when all of its inputs are allowed inputs of that side. A conjunct with no inputs (a pure constant) satisfies this trivially, so constant conjuncts were classified as pushable to any side. For an OUTER join the non-preserved side is disabled for push-down (its allowed input set is empty), yet the constant conjunct was still pushed into that side input and removed from the post-join filter. A falsy/NULL constant then filtered out the side input, the join fabricated non-matched rows with the side columns defaulted, and those rows escaped the now constant-free post-join filter. Fix: when a join side is disabled for push-down (empty allowed input set), do not push a conjunct that depends on no input of that side. Such constant conjuncts stay in the post-join filter, where they constrain all rows (matched and fabricated non-matched). Sides that are enabled keep the existing behaviour, so legitimate constant push-down into a preserved side is unaffected (push-down of a constant to a preserved side is equivalent to keeping it in the post-join filter). The regression test forces the new analyzer with `SET enable_analyzer = 1` because the repro uses a QUALIFY clause, which the old analyzer does not support; this keeps the test running on every CI variant rather than skipping it via a no-old-analyzer tag. It uses only local Memory and Join engine tables with deterministic, stable result checks. Closes ClickHouse#106949. Co-Authored-By: Claude Opus 4.8 <[email protected]>
032515d to
a8177b3
Compare
|
Fixup (HEAD Rather than adding a Verified on the debug binary: with the original test under |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 34/34 (100.00%) | Lost baseline coverage: none · Uncovered code |
alexey-milovidov
left a comment
There was a problem hiding this comment.
Looks correct.
First I thought - doesn't it disable push down of constant filters into preserved side of the JOIN (like lhs of LEFT JOIN). But the code looks correct.
|
Hi — this looks like a backport candidate for |
…-null-right-join-pushdown Fix constant filter conjunct pushed to non-preserved side of OUTER JOIN
…07084 Review on ClickHouse#123702 asked to keep the fix local to ClickHouse#107084, so the plan-level ReadNothing rule is replaced by an exception in that guard. The guard keeps a conjunct that reads no column off a join input that push-down is disabled for: pushing it there and dropping it from the post-join filter changes the result when it is NULL (04335) or true but pruning (`indexHint`). When no row passes the filter and the join kind allows filtering one input, that input receives the conjunct and the join output is empty whatever the other input holds, so the guard is skipped and the disabled input is filtered too, as before 26.7. "No row passes" is `filterResultForNotMatchedRows` with no input defaulted, which the outer join conversions already use, and the filter must not be sensitive to its evaluation count. This also drops the ReadNothingStep row estimate and the oracle change in 04516_join_reorder_duplicate_names_merge_expression, which only the ReadNothing rule needed. The WITH TOTALS row lost by the constant-false ON short-circuit is unrelated to ClickHouse#107084 and goes to a separate PR. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…passes Since the constant-conjunct guard of ClickHouse#107084, a no-input conjunct of a filter above a JOIN is kept off the side the filter cannot be pushed to (the non-preserved side of an OUTER join, or a side none of whose columns is used above the join). A UNION ALL branch made false by an outer WHERE on its constant column (`materialize(k4)` from the UNION conversion) therefore read, aggregated and hash-built that side in full (issue ClickHouse#123702). When the filter above a JoinStepLogical is false for every row, replace each input it cannot be pushed to with ReadNothing, using the helper extracted from the constant-false ON short-circuit. A `FULL JOIN` under such a `WHERE` has both inputs replaced. The helper keeps an input that carries more than rows: a JoinStepLogicalLookup, also below single-child wrappers, a subtree that can produce totals (TotalsHavingStep, a remote read, or a query result cache read), since a join joins its inputs' totals, or a subtree holding one half of a shared subplan (`CommonSubplanStep` / `CommonSubplanReferenceStep`, which decorrelation pairs across a join). The totals case also restores the totals row the ON short-circuit dropped since 26.8. Join-order estimation now treats ReadNothing as an exact empty relation, as it already does for an exact empty MergeTree read, so a replaced input keeps its exact zero estimate. `04516_join_reorder_duplicate_names_merge_expression` now ignores the `[0]` row estimate an empty table gets in its EXPLAIN label. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a wrong result in an OUTER JOIN when the
WHEREfilter is a conjunction that contains a constant term (for examplep AND 1, orWHERE p QUALIFY NULLwhereQUALIFY NULLis merged intoWHERE). The constant conjunct could be pushed into the non-preserved side of the join and removed from the post-join filter, so the join produced non-matched rows with the side columns defaulted and those rows incorrectly passed the filter.Description
Reported by SQLancer (#106949).
Reproducer (
tjminis aJoin(ALL, RIGHT, ...)engine table):Without the extra
AND 1the query returns 0 rows (correct). With it, thesame query returns all right-side rows with the left columns defaulted.
Root cause:
getConjunctionNodes()(used byActionsDAG::splitActionsForJOINFilterPushDown) classifies a conjunct aspushable to a join side when all of its children are allowed inputs of that
side. A conjunct with no inputs (a pure constant such as
1or a foldedNULLliteral) satisfies this trivially, so constant conjuncts were treatedas pushable to any side, including a side that is disabled for push-down. For
an OUTER join the non-preserved side is disabled (its allowed-input set is
empty), yet the constant conjunct was still pushed into that side input and
dropped from the post-join filter. A falsy/
NULLconstant then filtered outthe side input, the join fabricated non-matched rows with the side columns
defaulted, and those rows escaped the now constant-free post-join filter.
Fix: push a conjunct to a join side only if it actually depends on at least
one allowed input of that side. Constant conjuncts now stay in the post-join
filter, where they are applied to the join output and correctly constrain all
rows (matched and non-matched).
Regression test added in
tests/queries/0_stateless/04335_storage_join_right_join_const_conjunct_pushdown.sql.It returns 2 wrong rows on current master and 0 rows with this fix.
Version info
26.7.1.378(included in26.7and later)