Skip to content

Extend outer to inner join conversion - #120253

Merged
alexey-milovidov merged 13 commits into
ClickHouse:masterfrom
m-selmi:extend-outer-to-inner-join-conversion
Oct 2, 2026
Merged

alexey-milovidov merged 13 commits into
ClickHouse:masterfrom
m-selmi:extend-outer-to-inner-join-conversion

Conversation

@m-selmi

@m-selmi m-selmi commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Re-implements: #114817
Related: #119275
Related: #119305
Related: #120119
Related: #120130
closes: #112456

Extends OUTER join to an INNER join conversion.
The existing query_plan_convert_outer_join_to_inner_join only looks at a filter that is the direct parent of the join:

SELECT count() FROM students AS s
LEFT JOIN enrollments AS e ON s.id = e.student_id
WHERE e.grade > 2;

converts to

SELECT count() FROM students AS s
INNER JOIN enrollments AS e ON s.id = e.student_id
WHERE e.grade > 2;

After the change the following cases are also supported:

  • The rows the join would extend with NULLs cannot survive because the conditions of an enclosing join rejects them:
SELECT count() FROM students AS s
LEFT JOIN enrollments AS e ON s.id = e.student_id
INNER JOIN lectures AS l ON e.lecture_id = l.id;

converts to

SELECT count() FROM students AS s
INNER JOIN enrollments AS e ON s.id = e.student_id
INNER JOIN lectures AS l ON e.lecture_id = l.id;
  • The rows the join would extend with NULLs cannot survive because a filter higher up the plan rejects them:
SELECT count() FROM students AS s
LEFT JOIN enrollments AS e ON s.id = e.student_id
INNER JOIN lectures AS l ON s.year = l.year
WHERE e.grade > l.min_grade;

converts to

SELECT count() FROM students AS s
INNER JOIN enrollments AS e ON s.id = e.student_id
INNER JOIN lectures AS l ON s.year = l.year
WHERE e.grade > l.min_grade;

How it works: the pass walks the plan top down, carrying the set of columns that cannot be NULL in a row that reaches the result. WHERE e.grade > l.min_grade adds e.grade, and ON e.lecture_id = l.id on an INNER join adds e.lecture_id. When the walk reaches an outer join and any column in the set comes from the side that join fills with NULLs, those rows are dead and the join kind is downgraded.

image

This replaces the approach of #114817, which derived IS NOT NULL filters and inserted marker steps into the plan. That was reverted because other optimizations could interact with the derived filters (move or split). This new design derives no filters and inserts no steps so it's less error prone. It also handles more cases like stranded filters higher in the plan.

Changelog category (leave one):

  • Performance Improvement

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

OUTER JOIN queries can now be faster: when a WHERE clause or another JOIN later in the query would discard the unmatched rows anyway, ClickHouse converts the join to an INNER JOIN. The new setting query_plan_convert_outer_join_to_inner_join_transitively controls this, and only takes effect when query_plan_convert_outer_join_to_inner_join is enabled.


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

Version info

  • Merged into: 26.10.1.1251-master (included in 26.10 and later)

@m-selmi m-selmi added the can be tested Allows running workflows for external contributors label Sep 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [163a69f]

Summary: ✅


AI Review

Summary

This PR adds a second-pass optimizer that converts OUTER JOIN to INNER JOIN when a null-rejecting proof comes transitively from higher plan nodes, and it adds a small isNullPropagating hook so the analysis can trace nullable expressions accurately. After reviewing the current diff, the full inline-thread history, and the relevant surrounding optimizer code, I did not find any remaining correctness, safety, or PR-metadata issues that warrant a new inline review comment.

Final Verdict

✅ No actionable findings in the current revision.

LLVM Coverage Report

Measured on commit 163a69f.

Metric Baseline Current Δ
Lines 87.90% 87.90% +0.00%
Functions 91.80% 91.70% -0.10%
Branches 79.30% 79.30% +0.00%

Changed lines: Changed C/C++ lines covered: 305/320 (95.31%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Sep 15, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... label Sep 15, 2026
/// arbitrarily higher in the plan (a filter, or the conditions of an enclosing INNER/SEMI join).
/// This pass is complementary to `tryConvertOuterJoinToInnerJoin`, which only sees the filter directly
/// above but handles all filter shapes where this one matches a subset.
void convertOuterJoinToInnerJoinTransitively(const QueryPlanOptimizationSettings & optimization_settings, QueryPlan::Node & root);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it implemented as a separate optimization, not as an extension of convert_outer_join_to_inner_join? Why is it implemented as a second path optimization?
Transitivity for convert_outer_join_to_inner_join could be guaranteed by filter push down, so separate optimisation looks like an overkill

@m-selmi m-selmi Sep 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you referring to deriving col IS NOT NULL filters from the upper INNER join and letting filter pushdown drive them to above the OUTER join for the exiting convert_outer_join_to_inner_join to handle them?
That would be the first implementation I had, details #114817. But removing the added filters from the plan after the conversion was done was messy.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least we have a feature flag. This is good.

@novikd novikd self-assigned this Sep 18, 2026
alexey-milovidov and others added 2 commits September 26, 2026 02:54
…er-join-conversion

Resolve the conflict with the move of the settings changes history into the setting declarations
(ee6b44f): the history records this PR added to `SettingsChangesHistory.cpp` now live as
trailing arguments of the `DECLARE` of each setting.

- retargeted PR record [email protected] to 26.10 (settings-history check rule)

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... and removed comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... labels Sep 27, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 36 queries analysed

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 098b87a0-fc12-483c-b83f-edbc9f1aecbd
  • MIRAI run: 4d86e0e2-8310-4ade-bfce-f76b3e6bbb22
  • PR check IDs:
    • clickbench_2286029_1790632519
    • clickbench_2286042_1790632518
    • clickbench_2286048_1790632519
    • tpch_adapted_1_official_2286058_1790632519
    • tpch_adapted_1_official_2286085_1790632519
    • tpch_adapted_1_official_2286099_1790632519

@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 163a69f4e with master d7c4d8c01 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 614ee95dc; 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

897 object files changed (+328.71 KiB total), 1 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Processors/QueryPlan/Optimizations/convertOuterJoinToInnerJoinTransitively.… new 111.62 KiB +111.62 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

1341 translation units recompiled, 8391 s compile time in total, 1340 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×1.07 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +528.8 s (+7%) in total before that adjustment.

Translation units without a recent master baseline:

  • src/Processors/QueryPlan/Optimizations/convertOuterJoinToInnerJoinTransitively.cpp: 6.7 s

Job report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic optimization. Love it!

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-performance Pull request with some performance improvements 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.

Outer-to-inner JOIN conversion based on null-rejecting ON condition of an enclosing join

4 participants