[improvement](nereids) Enhance LogicalJoin.computeUnique with unique-set union propagation#62980
Open
feiniaofeiafei wants to merge 2 commits intoapache:masterfrom
Open
[improvement](nereids) Enhance LogicalJoin.computeUnique with unique-set union propagation#62980feiniaofeiafei wants to merge 2 commits intoapache:masterfrom
feiniaofeiafei wants to merge 2 commits intoapache:masterfrom
Conversation
Contributor
|
Thank you for your contribution to Apache Doris. Please clearly describe your PR:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What problem does this PR solve?
Issue Number: close #xxx
Related PR: #xxx
Problem Summary:
LogicalJoin.computeUniquepreviously only propagated unique traits when the hash keys themselves were unique on both sides. This missed the very common case where a unique set contains non-join-keycolumns. For example, if the left side is unique on
{l.v, l.k}and the right side is unique on{r.v, r.k}, an inner/outer join is in fact unique on{l.v, l.k, r.v, r.k}, but the old implementation couldnot derive this. As a result, downstream rules that rely on unique information (
EliminateGroupByKey,EliminateJoinByUnique, SELECT DISTINCT elimination, etc.) lost optimization opportunities.This PR introduces unique-set union propagation:
Proof sketch (INNER): take two output rows
(l_a, r_a)and(l_b, r_b)that agree onU_L ∪ U_R. They agree onU_L, so by left-side uniquenessl_a = l_b. They agree onU_R, so by right-sideuniqueness
r_a = r_b. Thus the rows are identical. OUTER variants follow by considering the matched and unmatched sub-relations separately under SQL's "NULL is not equal to NULL" semantics for unique sets.SEMI / ANTI / MARK / ASOF are excluded via a join-type whitelist and keep their existing behavior.
To let downstream rules such as
EliminateGroupByKeyactually match the propagated unique sets, the PR also includes the following supporting changes:DataTrait.getAllUniqueSets()that enumerates all unique sets (including nullable ones), used by the join-side computation.UniqueDescription.removeNotContainis now equalSet-aware: when a slot in a unique set is projected away but an equivalent slot is still in the output, it is substituted with the equivalent one, soprojections do not unnecessarily drop unique information. Both single-slot
uniqueSlotsandcombinedUniqueSlotSetare handled symmetrically.Builder.rmDuplicateInUniqueSlotSetByEqualSetthat normalizes slots in unique sets to the equalSet root, collapsing redundant members. Combined with join equi predicates, this lets sets like{l.k, r.v}and{r.k, r.v}(withl.k ≡ r.k) collapse to the same canonical form, makingEliminateGroupByKeymore likely to succeed.UniqueDescription:slots → uniqueSlots,slotSets → combinedUniqueSlotSet, for readability.ImmutableEqualSet.Buildergains abuiltcache and a publicgetRoot(T). The cache is properly invalidated byaddEqualPair/addEqualSet/replace/removeNotContainto prevent staleresults from leaking through the public API.
A conservative
unionLimit = 16guards against pathological cartesian explosions in unique-set combinations (in practice the number of unique sets per side is far below this).Release note
None (internal optimizer enhancement; improves trait propagation precision, no user-visible behavior change beyond plan quality).
Check List (For Author)
UniqueTest#testJoinUniqueUnionPropagationcovering union propagation for INNER / CROSS / LEFT_OUTER / RIGHT_OUTER / FULL_OUTER, the single-slot equalSet collapse path, and a negativecase where dropping both join keys breaks uniqueness. Three previously over-conservative
assertFalsechecks inUniqueTest#testJoinare corrected toassertTruewith a comment explaining why.