Skip to content

Make query_plan_use_logical_join_step setting obsolete - #104017

Merged
vdimir merged 8 commits into
masterfrom
vdimir/obsolete-query-plan-use-logical-join-step
May 19, 2026
Merged

vdimir merged 8 commits into
masterfrom
vdimir/obsolete-query-plan-use-logical-join-step

Conversation

@vdimir

@vdimir vdimir commented May 4, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

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

Setting query_plan_use_logical_join_step (and its alias query_plan_use_new_logical_join_step) is now obsolete and has no effect; the logical join step is always used.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.806

@clickhouse-gh

clickhouse-gh Bot commented May 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [32ee3fd]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_msan) FAIL
Logical error: AggregateFunctionArray: parameters mismatch between Array wrapper 'A' and nested function 'B'. Wrapper has C parameter(s): [D], nested function has E parameter(s): [F] (STID: 4870-4f07) FAIL cidb, issue

AI Review

Summary

This PR makes query_plan_use_logical_join_step / query_plan_use_new_logical_join_step obsolete and removes the legacy join-planning path from PlannerJoinTree. The main remaining issue is test coverage: one deleted regression test removed the only focused guard for a still-live DirectKeyValueJoin type-equivalence path (LowCardinality key wrapping) used by the always-on logical planner.

Findings

⚠️ Majors

  • [tests/queries/0_stateless/04209_rocksdb_direct_join_lowcardinality_pk_equivalence.sql:1] Deleting this test drops focused coverage for EmbeddedRocksDB direct-join key equivalence when storage PK is LowCardinality(String) and the right key is plain String. That behavior is still active after this PR (JoinStepLogical still reaches tryDirectJoin, and tryDirectJoin still normalizes with recursiveRemoveLowCardinality in src/Planner/PlannerJoins.cpp:1108). Existing 04201_direct_join_type_mismatch_rocksdb checks mismatch fallback and plain-type matching, but not this LowCardinality equivalence case.
  • Suggested fix: keep/add one focused use-rocksdb test for LowCardinality-wrapped PK equivalence on the always-on path (without obsolete setting toggles), and assert EXPLAIN still selects DirectKeyValueJoin for that case.
Tests
  • ⚠️ Add a focused regression test proving that LowCardinality PK equivalence still preserves DirectKeyValueJoin selection (INNER and/or LEFT) instead of silently regressing to hash join.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
  1. Restore or reintroduce focused coverage for the deleted LowCardinality direct-join equivalence scenario on the always-on logical planner path.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label May 4, 2026
@vdimir
vdimir force-pushed the vdimir/obsolete-query-plan-use-logical-join-step branch from 8ef2929 to d4b469e Compare May 4, 2026 15:20
@Algunenano Algunenano self-assigned this May 4, 2026

@Algunenano Algunenano 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.

The error in performance test is related. Also, was the performance issue fixed (one_thousand_joins) for query_plan_use_logical_join_step=1?

@vdimir

vdimir commented May 13, 2026

Copy link
Copy Markdown
Member Author

The error in performance test is related. Also, was the performance issue fixed (one_thousand_joins) for query_plan_use_logical_join_step=1?

@Algunenano

one_thousand_joins is slower with query_plan_use_logical_join_step=1 (2.5 sec vs 1.5 sec), but that should be acceptable since the test covers a corner case and its main purpose is just to ensure we have reasonable analysis time for such queries. In real-life scenarios, queries like this would take much longer anyway, so a 1-second difference is negligible compared to the potential improvement from join reordering. I couldn't identify a single bottleneck, so the slowdown appears to come from the additional processing of the query plan and expressions.

I also noticed that runtime filters, which can only be used with logical joins, can increase query execution time to tens of seconds because we generate too many of them. I think we can address this by limiting the probe size, so runtime filters are not used for cases with small tables. We could also limit the number of runtime filters in the plan, although I'm not sure whether that would still make sense for queries with larger datasets.

In this PR, I disabled runtime filters for the test. I will add the limit in #104860.

@Algunenano

Copy link
Copy Markdown
Member

one_thousand_joins is slower with query_plan_use_logical_join_step=1 (2.5 sec vs 1.5 sec), but that should be acceptable since the test covers a corner case and its main purpose is just to ensure we have reasonable analysis time for such queries.

Makes sense. We can do another iteration of analysis optimization around it in the future if we need to

@Algunenano

Copy link
Copy Markdown
Member

@vdimir I just tested with master (LTO from CI and local built without it) and the query from one_thousand_joins with query_plan_use_new_logical_join_step=0 takes 1 second, and with query_plan_use_new_logical_join_step=1 it takes 60+ seconds.

@vdimir

vdimir commented May 13, 2026 •

Copy link
Copy Markdown
Member Author

@vdimir I just tested with master (LTO from CI and local built without it) and the query from one_thousand_joins with query_plan_use_new_logical_join_step=0 takes 1 second, and with query_plan_use_new_logical_join_step=1 it takes 60+ seconds.

Yes, I mentioned it in my comment above, reason for this is runtime filters are enabled by default with query_plan_use_new_logical_join_step=1, but not logical step itself. For now we can disable enable_join_runtime_filters in test and I'll implement some guard for runtime filters in #104860

@vdimir
vdimir force-pushed the vdimir/obsolete-query-plan-use-logical-join-step branch from 1dcd838 to 8f587e8 Compare May 13, 2026 18:07
@@ -1,71 +0,0 @@
-- Tags: use-rocksdb

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.

Removing this test drops the only focused regression coverage for LowCardinality-wrapped key equivalence when choosing DirectKeyValueJoin for EmbeddedRocksDB joins.

That behavior is still live after this PR: JoinStepLogical still calls chooseJoinAlgorithm, which still uses tryDirectJoin and its recursiveRemoveLowCardinality checks. Existing 04201_direct_join_type_mismatch_rocksdb coverage validates type mismatch fallback, but it does not cover the LowCardinality(String) primary-key equivalence path from this test.

Please keep this scenario on the always-on logical planner path (drop obsolete setting toggles, but keep the LowCardinality PK + explain assertions), otherwise a regression here can silently demote direct lookups to HashJoin with no test signal.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

For new planner hash join is chosen on type mismatch, not sure which is correct, I will change it in another pr if it will work well #105057

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.

Re-checking current HEAD, this concern is still live.

The deleted test covers the equivalence path (LowCardinality(String) storage PK vs plain String right key), not the type-mismatch path. The behavior is still active in the always-on logical planner because JoinStepLogical still reaches tryDirectJoin, and tryDirectJoin still relies on recursiveRemoveLowCardinality when deciding whether to keep DirectKeyValueJoin.

04201_direct_join_type_mismatch_rocksdb validates mismatch fallback and matching plain types, but it still does not cover LowCardinality PK equivalence. So this regression surface currently has no focused test protection.

Please keep one focused EmbeddedRocksDB test for this LowCardinality-equivalence scenario on the always-on path (without obsolete setting toggles).

@clickhouse-gh

clickhouse-gh Bot commented May 18, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.50% 76.60% +0.10%

Changed lines: 100.00% (60/60) · Uncovered code

Full report · Diff report

@vdimir
vdimir added this pull request to the merge queue May 19, 2026
Merged via the queue into master with commit ad54158 May 19, 2026
164 of 166 checks passed
@vdimir
vdimir deleted the vdimir/obsolete-query-plan-use-logical-join-step branch May 19, 2026 08:39
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 19, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Aug 2, 2026
When the bounds of a range predicate on a MergeTree primary or partition key arrive through
`JOIN ON` against a single-row constant side, the logical join step left the inequality as a
residual filter ABOVE the join, where index analysis never sees it. Every part was read.
Reported as ClickHouse#112586: on a ~365M-row table this was 1.4s versus 35-68s. Until 26.5 the
`query_plan_use_new_logical_join_step` setting could restore pruning; ClickHouse#104017 made it
obsolete, leaving no mitigation.

Such a condition is now retried against a clone of itself with the opposite side's
references replaced by the constants its input header carries. That makes it single-sided,
so the existing path places it below the join and `KeyCondition` sees it.

The substitution is declined unless it is provably safe:

  - only INNER, CROSS and comma joins with ALL strictness, where the residual is equivalent
    to a filter over the product;
  - never a key-forming equality, including one nested in a disjunction, so a join cannot
    become keyless and degrade to a product;
  - constness is read from the header column, so a one-row table stays unpruned;
  - duplicate column names resolve by source relation and header position, not by name;
  - every node must be safe to relocate: no stateful or non-deterministic function, no
    `arrayJoin`, and no function that can throw or is expensive. Below the join a predicate
    also sees the rows a surviving key rejects, so `ON t.k = b.k AND intDiv(1, t.x) >= b.lo`
    would raise ILLEGAL_DIVISION on an unmatched row that master answers. Safety is read
    from `isSuitableForShortCircuitArgumentsExecution`, the property short-circuit
    evaluation already consults to keep such arguments unevaluated.

One behaviour change: a relocatable inequality-only `ON` against a constant side is now
answered on the non-hash algorithms instead of raising INVALID_JOIN_ON_EXPRESSION, since the
predicate is enforced below the join and no key is needed. Literal bounds already answered,
so this removes an inconsistency. A predicate the guards decline keeps raising as before.

Nine pruning assertions flip without the fix and each guard is mutation-tested both ways.
An A/B sweep of 1195 join and index tests found no difference from pristine master. The
stateless test is tagged `no-parallel-replicas`: there the `ON` clause is re-serialized as
SQL for the remote legs, so the opposite side is a temporary table rather than a plan-time
constant and the plan under test never forms, measured identical on master.

Closes ClickHouse#112586.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance pr-improvement Pull request with some product 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.

3 participants