-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[fix](fe) Reject mismatched project outputs in PullUpJoinFromUnionAll #65472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yujun777
wants to merge
2
commits into
apache:master
Choose a base branch
from
yujun777:fix-pullup-join-from-unionall
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+170
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
167 changes: 167 additions & 0 deletions
167
...core/src/test/java/org/apache/doris/nereids/rules/rewrite/PullUpJoinFromUnionAllTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package org.apache.doris.nereids.rules.rewrite; | ||
|
|
||
| import org.apache.doris.nereids.trees.expressions.Alias; | ||
| import org.apache.doris.nereids.trees.expressions.EqualTo; | ||
| import org.apache.doris.nereids.trees.expressions.Expression; | ||
| import org.apache.doris.nereids.trees.expressions.NamedExpression; | ||
| import org.apache.doris.nereids.trees.expressions.Slot; | ||
| import org.apache.doris.nereids.trees.expressions.SlotReference; | ||
| import org.apache.doris.nereids.trees.plans.JoinType; | ||
| import org.apache.doris.nereids.trees.plans.Plan; | ||
| import org.apache.doris.nereids.trees.plans.algebra.SetOperation.Qualifier; | ||
| import org.apache.doris.nereids.trees.plans.logical.LogicalFilter; | ||
| import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; | ||
| import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan; | ||
| import org.apache.doris.nereids.trees.plans.logical.LogicalProject; | ||
| import org.apache.doris.nereids.trees.plans.logical.LogicalUnion; | ||
| import org.apache.doris.nereids.util.MemoTestUtils; | ||
| import org.apache.doris.nereids.util.PlanChecker; | ||
| import org.apache.doris.nereids.util.PlanConstructor; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableSet; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| class PullUpJoinFromUnionAllTest { | ||
|
|
||
| @Test | ||
| void comparatorRejectsDifferentProjectOutputSizes() { | ||
| LogicalOlapScan smallScan = newScan(1, "common_small"); | ||
| LogicalOlapScan largeScan = newScan(1, "common_large"); | ||
| LogicalProject<LogicalOlapScan> smallProject = project(selectSlots(smallScan.getOutput(), 0), smallScan); | ||
| LogicalProject<LogicalOlapScan> largeProject = project(selectSlots(largeScan.getOutput(), 0, 1), largeScan); | ||
|
|
||
| PullUpJoinFromUnionAll.LogicalPlanComparator comparator = | ||
| new PullUpJoinFromUnionAll().new LogicalPlanComparator(); | ||
| Assertions.assertFalse(comparator.isLogicalEqual(largeProject, smallProject)); | ||
| } | ||
|
|
||
| @Test | ||
| void comparatorRejectsDifferentFilterChildOutputSizes() { | ||
| LogicalFilter<LogicalOlapScan> smallFilter = filter(newCachedOutputScan(1, "common_filter", 0)); | ||
| LogicalFilter<LogicalOlapScan> largeFilter = filter(newCachedOutputScan(1, "common_filter", 0, 1)); | ||
|
|
||
| PullUpJoinFromUnionAll.LogicalPlanComparator comparator = | ||
| new PullUpJoinFromUnionAll().new LogicalPlanComparator(); | ||
| Assertions.assertFalse(comparator.isLogicalEqual(largeFilter, smallFilter)); | ||
| } | ||
|
|
||
| @Test | ||
| void ruleSkipsJoinChildrenWithDifferentFilteredCommonSideOutputs() { | ||
| LogicalOlapScan commonSmallScan = newScan(10, "common_small"); | ||
| LogicalOlapScan commonLargeScan = newScan(10, "common_large"); | ||
| LogicalFilter<LogicalOlapScan> commonSmall = filter(commonSmallScan.withCachedOutput( | ||
| selectSlotsAsSlots(commonSmallScan.getOutput(), 0))); | ||
| LogicalFilter<LogicalOlapScan> commonLarge = filter(commonLargeScan.withCachedOutput( | ||
| selectSlotsAsSlots(commonLargeScan.getOutput(), 0, 1))); | ||
| LogicalOlapScan otherLeft = newScan(20, "other_left"); | ||
| LogicalOlapScan otherRight = newScan(30, "other_right"); | ||
|
|
||
| LogicalProject<Plan> unionChild1 = outputProject( | ||
| join(commonSmall, otherLeft), | ||
| "x", commonSmall.getOutput().get(0), | ||
| "y", otherLeft.getOutput().get(1)); | ||
| LogicalProject<Plan> unionChild2 = outputProject( | ||
| join(commonLarge, otherRight), | ||
| "x", commonLarge.getOutput().get(0), | ||
| "y", otherRight.getOutput().get(1)); | ||
|
|
||
| LogicalUnion union = union(unionChild1, unionChild2); | ||
|
|
||
| Plan rewritten = PlanChecker.from(MemoTestUtils.createConnectContext(), union) | ||
| .applyTopDown(new PullUpJoinFromUnionAll()) | ||
| .getPlan(); | ||
|
|
||
| Assertions.assertInstanceOf(LogicalUnion.class, rewritten); | ||
| LogicalUnion rewrittenUnion = (LogicalUnion) rewritten; | ||
| Assertions.assertEquals(2, rewrittenUnion.children().size()); | ||
| Assertions.assertInstanceOf(LogicalProject.class, rewrittenUnion.child(0)); | ||
| Assertions.assertInstanceOf(LogicalProject.class, rewrittenUnion.child(1)); | ||
| } | ||
|
|
||
| private static LogicalOlapScan newScan(long tableId, String tableName) { | ||
| return PlanConstructor.newLogicalOlapScan(tableId, tableName, 0); | ||
| } | ||
|
|
||
| private static LogicalOlapScan newCachedOutputScan(long tableId, String tableName, int... indexes) { | ||
| LogicalOlapScan scan = newScan(tableId, tableName); | ||
| return scan.withCachedOutput(selectSlotsAsSlots(scan.getOutput(), indexes)); | ||
| } | ||
|
|
||
| private static LogicalFilter<LogicalOlapScan> filter(LogicalOlapScan child) { | ||
| return new LogicalFilter<>(ImmutableSet.of(new EqualTo(child.getOutput().get(0), child.getOutput().get(0))), | ||
| child); | ||
| } | ||
|
|
||
| private static LogicalJoin<Plan, LogicalOlapScan> join(Plan commonSide, | ||
| LogicalOlapScan otherSide) { | ||
| Expression joinCondition = new EqualTo(commonSide.getOutput().get(0), otherSide.getOutput().get(0)); | ||
| return new LogicalJoin<>(JoinType.INNER_JOIN, ImmutableList.of(joinCondition), ImmutableList.of(), | ||
| commonSide, otherSide, null); | ||
| } | ||
|
|
||
| private static LogicalProject<Plan> outputProject(Plan child, String leftAlias, Slot leftSlot, | ||
| String rightAlias, Slot rightSlot) { | ||
| return new LogicalProject<>(ImmutableList.of( | ||
| new Alias(leftSlot, leftAlias), | ||
| new Alias(rightSlot, rightAlias)), child); | ||
| } | ||
|
|
||
| private static LogicalUnion union(LogicalProject<Plan> left, LogicalProject<Plan> right) { | ||
| ImmutableList.Builder<NamedExpression> outputs = ImmutableList.builder(); | ||
| for (Slot slot : left.getOutput()) { | ||
| outputs.add(new SlotReference(slot.getName(), slot.getDataType(), slot.nullable())); | ||
| } | ||
| return new LogicalUnion(Qualifier.ALL, outputs.build(), ImmutableList.of( | ||
| toSlotReferences(left.getOutput()), | ||
| toSlotReferences(right.getOutput())), ImmutableList.of(), false, ImmutableList.of(left, right)); | ||
| } | ||
|
|
||
| private static LogicalProject<LogicalOlapScan> project(List<NamedExpression> outputs, LogicalOlapScan child) { | ||
| return new LogicalProject<>(outputs, child); | ||
| } | ||
|
|
||
| private static List<NamedExpression> selectSlots(List<Slot> output, int... indexes) { | ||
| ImmutableList.Builder<NamedExpression> selected = ImmutableList.builder(); | ||
| for (int index : indexes) { | ||
| selected.add(output.get(index)); | ||
| } | ||
| return selected.build(); | ||
| } | ||
|
|
||
| private static List<Slot> selectSlotsAsSlots(List<Slot> output, int... indexes) { | ||
| ImmutableList.Builder<Slot> selected = ImmutableList.builder(); | ||
| for (int index : indexes) { | ||
| selected.add(output.get(index)); | ||
| } | ||
| return selected.build(); | ||
| } | ||
|
|
||
| private static List<SlotReference> toSlotReferences(List<Slot> slots) { | ||
| ImmutableList.Builder<SlotReference> references = ImmutableList.builder(); | ||
| for (Slot slot : slots) { | ||
| references.add((SlotReference) slot); | ||
| } | ||
| return references.build(); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests still wrap the mismatched common side in
LogicalProject, so they do not distinguish the new guard from the old code. Before this patch,comparePlanalready had aLogicalProject-local output-size check, so both this comparator test and the rewrite-skip test would still pass whencommonSmallhas one project output andcommonLargehas two. The moved guard matters for supported non-project nodes: for example, two same-tableLogicalOlapScancommon sides whose outputs have been pruned/cached to different arity, optionally under the sameLogicalFilter. Please make at least one test use that non-project shape so it fails without the new top-level output-size check.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the tests to use a non-project shape. The new cases cover LogicalFilter over same-table scans with different cached output arity, so they validate the top-level output-size guard instead of relying on the old LogicalProject-local check.