forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat](Nereids): Put the Child with Least Row Count in the First Posi…
…tion of Intersect (apache#34290) (apache#35339) In this pull request, we optimize the ordering of children in the Intersect operator to improve query performance. The proposed change is to place the child with the least row count in the first position of the Intersect operator. The rationale behind this optimization is that the Intersect operator works by first evaluating the leftmost child and then iterating through the results of the other children to find matching rows. By placing the child with the least row count first, we can minimize the number of iterations required to find the matching rows, thereby reducing the overall execution time of the query.
- Loading branch information
Showing
14 changed files
with
198 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
69 changes: 69 additions & 0 deletions
69
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/exploration/IntersectReorder.java
This file contains 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,69 @@ | ||
// 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.exploration; | ||
|
||
import org.apache.doris.nereids.rules.Rule; | ||
import org.apache.doris.nereids.rules.RuleType; | ||
import org.apache.doris.nereids.trees.expressions.SlotReference; | ||
import org.apache.doris.nereids.trees.plans.GroupPlan; | ||
import org.apache.doris.nereids.trees.plans.Plan; | ||
import org.apache.doris.nereids.trees.plans.algebra.SetOperation.Qualifier; | ||
|
||
import com.google.common.collect.Lists; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* this rule put the child with least row count to the first child of intersect op | ||
*/ | ||
public class IntersectReorder extends OneExplorationRuleFactory { | ||
|
||
public static final IntersectReorder INSTANCE = new IntersectReorder(); | ||
|
||
@Override | ||
public Rule build() { | ||
return logicalIntersect() | ||
.when(logicalIntersect -> logicalIntersect.getQualifier().equals(Qualifier.DISTINCT)) | ||
.then(logicalIntersect -> { | ||
int minChildIdx = 0; | ||
double minRowCount = Double.MAX_VALUE; | ||
for (int i = 0; i < logicalIntersect.children().size(); i++) { | ||
GroupPlan child = (GroupPlan) logicalIntersect.child(i); | ||
if (child.getGroup().getStatistics().getRowCount() < minRowCount) { | ||
minChildIdx = i; | ||
minRowCount = child.getGroup().getStatistics().getRowCount(); | ||
} | ||
} | ||
if (minChildIdx == 0) { | ||
return null; | ||
} | ||
List<Plan> children = Lists.newArrayList(logicalIntersect.children()); | ||
List<List<SlotReference>> regularOutput = | ||
Lists.newArrayList(logicalIntersect.getRegularChildrenOutputs()); | ||
children.set(0, logicalIntersect.child(minChildIdx)); | ||
children.set(minChildIdx, logicalIntersect.child(0)); | ||
if (regularOutput.isEmpty()) { | ||
return logicalIntersect.withChildren(children); | ||
} | ||
regularOutput.set(0, logicalIntersect.getRegularChildOutput(minChildIdx)); | ||
regularOutput.set(minChildIdx, logicalIntersect.getRegularChildOutput(0)); | ||
return logicalIntersect.withChildrenAndTheirOutputs(children, regularOutput); | ||
}) | ||
.toRule(RuleType.REORDER_INTERSECT); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...e-core/src/test/java/org/apache/doris/nereids/rules/exploration/IntersectReorderTest.java
This file contains 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,69 @@ | ||
// 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.exploration; | ||
|
||
import org.apache.doris.nereids.CascadesContext; | ||
import org.apache.doris.nereids.memo.Group; | ||
import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan; | ||
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; | ||
import org.apache.doris.nereids.util.LogicalPlanBuilder; | ||
import org.apache.doris.nereids.util.MemoPatternMatchSupported; | ||
import org.apache.doris.nereids.util.MemoTestUtils; | ||
import org.apache.doris.nereids.util.PlanChecker; | ||
import org.apache.doris.nereids.util.PlanConstructor; | ||
import org.apache.doris.statistics.Statistics; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.HashMap; | ||
|
||
class IntersectReorderTest implements MemoPatternMatchSupported { | ||
|
||
private final LogicalOlapScan scan1 = PlanConstructor.newLogicalOlapScan(0, "t1", 0); | ||
private final LogicalOlapScan scan2 = PlanConstructor.newLogicalOlapScan(1, "t2", 0); | ||
|
||
@Test | ||
void test() { | ||
LogicalPlan plan = new LogicalPlanBuilder(scan1) | ||
.intersect(ImmutableList.of(scan2, scan1)) | ||
.build(); | ||
|
||
CascadesContext ctx = MemoTestUtils.createCascadesContext(plan); | ||
for (Group group : ctx.getMemo().getGroups()) { | ||
if (group.getLogicalExpression().getPlan() instanceof LogicalOlapScan) { | ||
LogicalOlapScan scan = (LogicalOlapScan) group.getLogicalExpression().getPlan(); | ||
if (scan.equals(scan1)) { | ||
group.setStatistics(new Statistics(1, new HashMap<>())); | ||
} | ||
if (scan.equals(scan2)) { | ||
group.setStatistics(new Statistics(2, new HashMap<>())); | ||
} | ||
} | ||
} | ||
PlanChecker.from(ctx) | ||
.applyExploration(IntersectReorder.INSTANCE.build()) | ||
.printlnExploration() | ||
.matchesExploration( | ||
logicalIntersect( | ||
logicalOlapScan().when(scan -> scan.equals(scan1)), | ||
logicalOlapScan().when(scan -> scan.equals(scan2)) | ||
) | ||
); | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.