Skip to content

Cherry pick #132507 to 26.10: Do not use mixed-type equivalence classes as DPsub merge keys - #132694

Merged
robot-ch-test-poll merged 6 commits into
backport/26.10/132507from
cherrypick/26.10/132507
Nov 25, 2026
Merged

robot-ch-test-poll merged 6 commits into
backport/26.10/132507from
cherrypick/26.10/132507

Conversation

@robot-ch-test-poll3

Copy link
Copy Markdown
Contributor

Original pull-request #132507

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

fkastrati and others added 5 commits November 17, 2026 22:03
`spanningClasses` returned every equivalence class with columns on both
sides of a join, including classes whose columns have different types.
`buildPhysicalPlan` then added a secondary merge key from such a class
(e.g. `Date` = `DateTime('Asia/Tokyo')`), which is not implied by the query
because equality of different types is not transitive, and
`cleanupJoinPredicates` dropped the query's own predicate as redundant.
The join returned too few rows.

Restrict `spanningClasses` to single-type classes, like `mergeKeyClasses`,
so the plan and the cost in `mergeJoinCost` and `isShardedMerge` agree.

Closes: #130142
Related: #128519

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Since the previous commit `spanningClasses` returns only single-type
classes, so `mergeJoinCost` no longer saw a written key of columns of
different types, such as `a.x = b.y` with `Date` and `DateTime('UTC')`.
The plan still joins on it, so both inputs sorted only by the merge key
are sorted again, but the cost charged nothing for it and a merge join
won over a hash join.

`joinKeyClasses` gives all keys of the merge: the synthesized single-type
ones and the classes of the written predicates. `mergeJoinCost` and
`isShardedMerge` use it. A mixed-type key is joined on a cast column after
the other keys, so no input counts as sorted by all keys. The per-class
single-type flags become the bit mask `single_type_classes`.

Related: #132507 (comment)
Closes: #130142

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… types

`probeReadFraction` intersects the `minmax` ranges of the columns of the
merge key class. For a class of columns of different types they are in
different units, such as days of a `Date` and seconds of a `DateTime`, so
the overlap came out near 0 and a hash join was charged almost no probe
work. Return 1.0 (unknown) for such a class.

Related: #132507
Related: #130142

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Since 9bf7d55 any key of columns of different types made
`mergeJoinCost` charge sorting within the groups of the merge key. A
widening cast such as `UInt32` to `UInt64` keeps the input order, so for
inputs sorted by both keys the merge needs no sorting, and the cost made a
hash join win that is slower.

`castsKeepOrder` checks, as the sort step does, that the cast of each
column to the common type of the join is monotonic and increasing on the
whole type (`Date` to `DateTime` is not). The classes whose written
equalities all pass are in `ordered_classes`, and `mergeJoinCost` counts
an input as sorted on them. `buildPhysicalPlan` places the written
equalities of such a key at its position in the input order instead of
synthesizing a key, so the plan follows the cost.

Related: #132507 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-keys

Do not use mixed-type equivalence classes as DPsub merge keys
@robot-ch-test-poll3 robot-ch-test-poll3 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request labels Nov 18, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! labels Nov 18, 2026
@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

Dear @fkastrati, this cherry-pick PR has not been updated for 3d0h7m20s. Please resolve the conflicts to backport #132507, or close this PR if the backport is no longer needed. This PR will be automatically closed after 7 days of inactivity.

…ck/26.10/132507

# Conflicts:
#	src/Processors/QueryPlan/Optimizations/joinOrderDPSub.cpp
@fkastrati
fkastrati force-pushed the backport/26.10/132507 branch from f242c09 to d3fa8f1 Compare November 25, 2026 02:26
@robot-ch-test-poll
robot-ch-test-poll merged commit 64aaeeb into backport/26.10/132507 Nov 25, 2026
2 checks passed
@robot-ch-test-poll
robot-ch-test-poll deleted the cherrypick/26.10/132507 branch November 25, 2026 02:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants