Skip to content

Revert "Revert "Convert OUTER join to INNER join by null-rejecting join conditions"" - #119305

Merged
m-selmi merged 6 commits into
masterfrom
revert-119275-revert-114817-convert-outer-to-inner-by-join-condition
Sep 14, 2026
Merged

m-selmi merged 6 commits into
masterfrom
revert-119275-revert-114817-convert-outer-to-inner-by-join-condition

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

Reverts #119275


Workflow [PR]
Sync PR [sync-upstream/pr/119305]

Version info

  • Merged into: 26.9.1.1483 (included in 26.9 and later)

@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c67bd70]

Summary: ✅


AI Review

Summary

This PR reintroduces planner-derived IS NOT NULL filters for null-rejecting join predicates, uses them to unlock more OUTER JOIN to INNER JOIN rewrites, and optionally promotes selective ones to executable filters. I reviewed the current diff and the touched optimizer code, read the existing PR discussion and inline thread history, and did not find a remaining correctness or compatibility issue on the current PR head.

Missing context / blind spots
  • ⚠️ I did not run a local build or stateless suite in this workspace. The only available CI report on the PR was the Build profile diff, and it was green.
Final Verdict

✅ No new review findings. The earlier CAST null-propagation concern on this PR is fixed in the current code, and its bot-authored thread was resolved in this run.

LLVM Coverage Report

Measured on commit c67bd70.

Metric Baseline Current Δ
Lines 89.00% 89.10% +0.10%
Functions 91.80% 91.80% +0.00%
Branches 81.50% 81.50% +0.00%

Changed lines: Changed C/C++ lines covered: 318/337 (94.36%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 10, 2026
…L filter

resolvePlannerOnlyFilters rebuilds a FilterStep with remove_filter = true, using
a surviving conjunct's result_name as the filter column name. Because
collectFilterConjuncts unwraps aliases, that conjunct can be a column of the
step's own output header: filter push-down wraps a pushed predicate in a
__filter<name> alias so the removal cannot hit the same-named stream column
(ActionsDAG.cpp:3768-3779), and the unwrap discards that protection.

checkAndRemoveFilterColumn then erases the column, shrinking the step's output
header. The pass replaces node.step outright, so no updateInputHeader reaches
the parent JoinStepLogical, whose expression DAG still holds an input node for
the erased column, and that node cannot be pruned because the column is also one
of the join's outputs. chooseJoinOrder is the first consumer to notice:
JoinExpressionActions requires the DAG's input count to equal the left plus
right header widths, and aborts with "Input nodes size mismatch in dag". With
the join reorder disabled the same inconsistent plan reaches execution and
throws NOT_FOUND_COLUMN_IN_BLOCK instead, and a NATURAL JOIN variant, where
both sides carry a column of that name, throws UNKNOWN_IDENTIFIER.

Re-establish the alias at the rebuild site when the chosen filter node's name
collides with the step's output header, which is the same operation, for the
same reason, as the push-down site.

A Nullable join key is needed to reproduce: it makes the join condition
null-rejecting, which is what makes deriveNotNullFiltersFromJoin emit the
planner-only marker.

Related: #114817

Co-Authored-By: Claude Opus 5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... label Sep 10, 2026
Comment thread src/Functions/FunctionsConversion.h Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing c67bd7034 with master 0d04f2bc2 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

919 object files changed (+477.48 KiB total), 3 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Functions/FunctionPlannerOnlyFilter.cpp.o new 99.09 KiB +99.09 KiB
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/resolvePlannerOnlyFilters.cpp.o new 93.91 KiB +93.91 KiB
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/deriveNotNullFiltersFromJoin.cpp.o new 53.03 KiB +53.03 KiB

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

2122 translation units recompiled, 12835 s compile time in total, 2122 of them have a recent master baseline.

Job report

rorylshanks pushed a commit to rorylshanks/ClickHouse that referenced this pull request Sep 12, 2026
…r optimizer

isNullPropagatingFunction in optimizeJoin.cpp decided null propagation from the
function name alone and listed CAST / _CAST unconditionally. For a cast that is a
property of the target type, not of the name: casting NULL to a non-Nullable type
raises CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN instead of returning NULL. So
strictOnRelations reported a relation as "evaluates to NULL when all of R's
columns are NULL" where the runtime actually raises. That claim flows through
predicateNullRejectingRelations to cda_nr_rels (gated by trust_null_rejection =
join_use_nulls) to ConflictJoinOp::nr_rels, where a set bit turns a
null-rejection-dependent assoc matrix entry from a conflict into a permitted
reordering.

Observable effect, with join_use_nulls = 1 and the LEFT/LEFT shape
t1 LEFT JOIN t2 ON t1.a = t2.a LEFT JOIN t3 ON CAST(t2.k AS Int64) = t3.k:
every configuration raises Code 349 except dpsub with conflict detector A or C,
which reassociate into t1 LEFT JOIN (t2 LEFT JOIN t3) and thereby skip the
throwing evaluation. This is a consistency hardening rather than a bug fix: there
is no wrong result in either direction, because a non-Nullable cast never yields
a value that could make a null-extended row match, and both experimental settings
default to false while the default solver is greedy, so nothing reads nr_rels in
a default build.

The gate uses isNullableOrLowCardinalityNullable(node.result_type), which is the
same predicate ClickHouse#119305 adds on FunctionConvert / ExecutableFunctionCast, so the
analysis and the runtime agree by construction. Once that PR lands, the cast
branch here can delegate to IFunction::isNullPropagating. Only CAST is gated on
its result type: the INPUT leaf and the comparison and arithmetic names must not
be, because under join_use_nulls = 1 a non-Nullable base column becomes Nullable
in the join output while the DAG node keeps its pre-join type, so gating those
would drop relations that are correctly reported today.

The new test asserts the Join/Read plan skeleton per arm at
query_plan_optimize_join_order_limit > 0, since the reordering under test is only
reachable with dpsub plus a conflict detector. It carries two non-vacuity
controls: a CAST(... AS Nullable(Int64)) arm that must still reassociate, which
reddens if every cast is rejected instead of only the non-Nullable ones, and a
plain-predicate arm proving the detector is what moves the plan. It asserts the
plan rather than the exception because coupling a test to error preservation
across plan shapes would assert a guarantee ClickHouse does not make. It is also
the first stateless coverage of the trust_null_rejection = true branch, as no
existing test combines a conflict detector with join_use_nulls = 1.

Requested by @ m-selmi at
ClickHouse#119305 (comment)

Co-Authored-By: Claude Opus 5 <[email protected]>
@m-selmi m-selmi self-assigned this Sep 14, 2026
@m-selmi
m-selmi added this pull request to the merge queue Sep 14, 2026
Merged via the queue into master with commit a88f878 Sep 14, 2026
348 of 349 checks passed
@m-selmi
m-selmi deleted the revert-119275-revert-114817-convert-outer-to-inner-by-join-condition branch September 14, 2026 18:16
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 14, 2026
JingYanchao pushed a commit to JingYanchao/ClickHouse that referenced this pull request Sep 15, 2026
`05182_join_order_conflict_detector_cast_null_propagation` asserts a join plan shape, and it makes
`t1` look expensive through `param__internal_join_table_stat_hints` to get the reordering it wants to
observe. When the table carries automatic statistics that do not include `basic`, the optimizer has
its own estimate, costs the join the other way, and both reassociation arms - the non-vacuity
controls - stop reassociating, so the test fails on the plan lines while every row and exception arm
still agrees.

`clickhouse-test` randomizes `auto_statistics_types` to a random subset of
`uniq, uniq_v2, tdigest, countmin, basic`, so a run reddens whenever the subset leaves `basic` out.
Measured against master `7fe32951` with `clickhouse local`: `uniq` alone, `uniq_v2` alone, `tdigest`
alone, `countmin` alone and `uniq_v2,tdigest` all flip the plan, while `''`, `basic`, `basic,uniq`,
`basic,tdigest`, `basic,countmin`, `basic, uniq_v2` (the default) and the full set of five keep the
plan the reference records. Creating the tables with `auto_statistics_types = ''` pins it: the whole
test output then matches its reference, and the three `CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN` arms
still raise.

The test only became reachable this way when ClickHouse#119305
re-landed the OUTER-to-INNER conversion, which is what makes these joins reorderable at all: CIDB has
0 failures in 12428 runs over 1267 commits before `a88f878d7`, and 26 in 506 runs over 120 commits
after it.

Caused by: ClickHouse#119305

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@alexey-milovidov

alexey-milovidov commented Sep 15, 2026 •

Copy link
Copy Markdown
Member Author

@m-selmi, it broke the CI again.

alexey-milovidov added a commit that referenced this pull request Sep 15, 2026
Edited following .claude/skills/edit-changelog/SKILL.md. Manual catch-up run of
the NightlyChangelog edit step, verified with `verify_edit` of
`ci/jobs/changelog_nightly.py`.

Changelog-revert: 119284 119279 Revert "Revert "Print file offsets in the bare fatal stack traces and build the release binary as PIE""
Changelog-revert: 120118 117137 Revert "Fix wrong results for `plus`/`minus` keys with a non-integer operand"
Changelog-revert: 120119 119305 Revert "Convert `OUTER` join to `INNER` join by null-rejecting join conditions" again
Changelog-deleted-entry: 114817 [Performance Improvement] [section] * Convert `OUTER JOIN` to `INNER JOIN` when a later `JOIN` condition filters out its `NULL`-extended rows, and filter `NULL` join keys directly at the table scan when column statistics show it is selective. Only conditions of the form `expr1` OP `expr2` are considered, where OP is one of `=`, `<`, `<=`, `>`, `>=`. Each side can be a column or an expression that propagates `NULL`s. Controlled by the new settings `query_plan_derive_not_null_filters_from_joins` (enabled by default), `query_plan_allow_derived_not_null_filters_execution` (enabled by default) and `query_plan_max_selectivity_for_not_null_filters_execution` (0.7 by default). [#114817](#114817) ([Hechem Selmi](https://github.com/m-selmi)). [#119305](#119305) ([Alexey Milovidov](https://github.com/alexey-milovidov)).
Changelog-deleted-entry: 117137 [Bug Fix (user-visible misbehavior in an official stable release)] [section] * Fix wrong results when a `GROUP BY`, `DISTINCT`, `LIMIT BY` or window `PARTITION BY` key was `plus` or `minus` of a partition column with a non-integer constant, for example `x + 1.0` on `Float64` or `d + INTERVAL 1 MONTH` on `Date`. Such a key was assumed injective, so each table partition was evaluated independently without the cross-partition merge; where the key collapses distinct values onto one (float rounding, end-of-month arithmetic, a DST transition) the query returned one row or window frame per partition instead of one per group. Affects the default-enabled `allow_aggregate_partitions_independently`, `allow_distinct_partitions_independently`, `allow_limit_by_partitions_independently`, `allow_creating_set_partitions_independently`, `allow_window_partitions_independently` and `optimize_distributed_group_by_sharding_key`. [#117137](#117137) ([Groene AI](https://github.com/groeneai)).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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 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.

4 participants