Repository navigation
Do not use mixed-type equivalence classes as DPsub merge keys - #132507
Conversation
`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]>
|
Workflow [PR], commit [b62cc1b] Summary: ✅
AI ReviewSummaryReviewed Final VerdictStatus: ✅ Approve |
groeneai
left a comment
There was a problem hiding this comment.
I checked this with the PR's CI amd_binary and the master build it was merged onto (d9849872).
- The new test fails on master (the first query returns no rows) and passes here, so it covers the reported bug.
- The bot's Major comes from this change. With its hints (100M rows per side, 1,000 values of
k, 30,000 dates),a.k = b.k AND a.x = b.ywithx Dateandy DateTime('UTC')getsHashJoinon master andFullSortingMergeJoinwithFinishSortingTransformon both inputs here. Withy Date, both builds pickHashJoin.
Three things outside the diff:
probeReadFractionhas the same mixed-class problem. It intersects the key ranges of the class's columns, and those are days for aDateand seconds for aDateTime, so the overlap is 0 and the hash join is charged no probe work. With realminmaxstatistics and the same values on both sides, the hash join of a 3M-row fact table (sorted byDateTime('UTC')) with a 100K-row dimension costs 779,933 when the dimension's key isDateand 3,659,646 when it isDateTime('UTC'). Master gives the same numbers, so this came with #128519. Returning 1.0 (unknown) when!dpsub_data.class_single_type[order_class]would avoid it. Your call whether it goes here or separately.- #128519 has
v26.10-must-backportand its cherry-pick #129412 is open. This fix needs the same label, or the backport brings the wrong result to 26.10. Closes:closes #130142 on merge. The description says the Phase 2 part (cleanupJoinPredicates) stays unfixed, and I found no other issue that tracks it.Related:would keep it open.
Repro for 1
SET query_plan_optimize_join_order_algorithm = 'dpsub', query_plan_join_order_use_interesting_orders = 1, use_statistics = 1,
allow_experimental_statistics = 1, materialize_statistics_on_insert = 1, send_logs_level = 'test';
CREATE TABLE f (y DateTime('UTC') STATISTICS(minmax, uniq), v UInt64) ENGINE = MergeTree ORDER BY y;
CREATE TABLE d_date (x Date STATISTICS(minmax, uniq), w UInt64) ENGINE = MergeTree ORDER BY w;
CREATE TABLE d_dt (x DateTime('UTC') STATISTICS(minmax, uniq), w UInt64) ENGINE = MergeTree ORDER BY w;
INSERT INTO f SELECT toDateTime('2000-01-01 00:00:00', 'UTC') + number * 300, number FROM numbers(3000000);
INSERT INTO d_date SELECT toDate('2000-01-01') + number % 10000, number FROM numbers(100000);
INSERT INTO d_dt SELECT toDateTime(toDate('2000-01-01') + number % 10000, 'UTC'), number FROM numbers(100000);
SELECT 1 FROM f JOIN d_date ON f.y = d_date.x FORMAT Null; -- costs: 779933
SELECT 1 FROM f JOIN d_dt ON f.y = d_dt.x FORMAT Null; -- costs: 3659646Since 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]>
|
Thanks, all three points hold.
The bot's Major (written mixed-type keys not costed for sorting) was fixed in 9bf7d55, which came after the commit you checked. |
|
Correction to point 3 above: the description is back to |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units7 translation units recompiled, 11 s compile time in total, 7 of them have a recent master baseline. |
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]>
Cherry pick #132507 to 26.10: Do not use mixed-type equivalence classes as DPsub merge keys
Backport #132507 to 26.10: Do not use mixed-type equivalence classes as DPsub merge keys
Closes: #130142
Related: #128519
With
query_plan_join_order_use_interesting_orders = 1, a sort-merge join planned by DPsub could lose rows when an equivalence class mixed column types, for exampleDate,DateTime('UTC'), andDateTime('Asia/Tokyo'). Equality across these types is not transitive.spanningClassesreturned all classes with columns on both sides of a join, without the single-type check thatmergeKeyClasseshas. For a join of two tables with no direct predicate,buildPhysicalPlanthen added a secondary merge key from the mixed-type class (CAST(x AS DateTime('Asia/Tokyo')) = z), which the query does not imply.cleanupJoinPredicatesthen removed the query's owny = zas redundant.Now
spanningClassesreturns only single-type classes, andmergeKeyClassesreuses it, sobuildPhysicalPlanno longer synthesizes mixed-type keys. Mixed-type predicates written in the query are still applied as ordinary join conditions.The cost follows the plan:
joinKeyClassesgivesmergeJoinCostandisShardedMergeall keys the merge joins on, including written mixed-type ones. A mixed-type key is joined on cast columns. If every cast is monotonic and increasing on the whole type (castsKeepOrder, the same test as the sort step), such asUInt32toUInt64, the input order holds.buildPhysicalPlanthen places the written equality at its position in the input order. Otherwise, as forDatetoDateTime, the inputs are charged for sorting within their merge-key groups.probeReadFractionno longer intersects theminmaxranges of a class of columns of different types. Those ranges are in different units (days of aDate, seconds of aDateTime), so the overlap came out near 0 and a hash join was charged almost no probe work.The bug came with #128519 and is not in any release.
Not fixed here: Phase 2 of
cleanupJoinPredicatescan still synthesize a mixed-type predicate for a join that has no predicates of its own, also with interesting orders off. It is older than #128519 (it came with #98479) and needs a separate fix.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix wrong result of a join reordered by DPsub with
query_plan_join_order_use_interesting_orderswhen a join key class mixed column types such asDateandDateTimein different time zones.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/132507]
Version info
26.11.1.2722-master(included in26.11and later)26.10.13.8