forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
245 additions
and
25 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
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
180 changes: 180 additions & 0 deletions
180
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownEncodeSlot.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,180 @@ | ||
// 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.rules.Rule; | ||
import org.apache.doris.nereids.rules.RuleType; | ||
import org.apache.doris.nereids.trees.expressions.Alias; | ||
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.expressions.functions.scalar.EncodeStr; | ||
import org.apache.doris.nereids.trees.plans.Plan; | ||
import org.apache.doris.nereids.trees.plans.logical.LogicalCatalogRelation; | ||
import org.apache.doris.nereids.trees.plans.logical.LogicalProject; | ||
import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Sets; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* push down encode_as_int(slot) down | ||
* example: | ||
* group by x | ||
* -->project(encode_as_int(A) as x) | ||
* -->Any(A) | ||
* -->project(A) | ||
* --> scan | ||
* => | ||
* group by x | ||
* -->project(x) | ||
* -->Any(x) | ||
* --> project(encode_as_int(A) as x) | ||
* -->scan | ||
* Note: | ||
* do not push down encode if encode.child() is not slot, | ||
* example | ||
* group by encode_as_int(A + B) | ||
* --> any(A, B) | ||
*/ | ||
public class PushDownEncodeSlot extends OneRewriteRuleFactory { | ||
|
||
@Override | ||
public Rule build() { | ||
return logicalProject() | ||
.when(this::containsEncode) | ||
.when(project -> !(project.child() instanceof LogicalCatalogRelation)) | ||
.then(project -> pushDownEncodeSlot(project)) | ||
.toRule(RuleType.PUSH_DOWN_ENCODE_SLOT); | ||
} | ||
|
||
private boolean containsEncode(LogicalProject<? extends Plan> project) { | ||
return project.getProjects().stream() | ||
.anyMatch(e -> e instanceof Alias && containsEncode(e.child(0))); | ||
} | ||
|
||
private boolean containsEncode(Expression expr) { | ||
return expr instanceof EncodeStr && expr.child(0) instanceof SlotReference; | ||
} | ||
|
||
private List<Alias> collectEncodeAlias(LogicalProject<? extends Plan> project) { | ||
List<Alias> encodeAlias = new ArrayList<>(); | ||
project.getProjects().forEach(e -> { | ||
if (e instanceof Alias && e.child(0) instanceof EncodeStr) { | ||
encodeAlias.add((Alias) e); | ||
} | ||
}); | ||
return encodeAlias; | ||
} | ||
|
||
private LogicalProject<? extends Plan> pushDownEncodeSlot(LogicalProject<? extends Plan> project) { | ||
List<Alias> encodeAlias = collectEncodeAlias(project); | ||
LogicalProject<? extends Plan> result = (LogicalProject<? extends Plan>) | ||
project.accept(EncodeSlotPushDownVisitor.visitor, encodeAlias); | ||
return result; | ||
} | ||
|
||
/** | ||
* push down encode slot | ||
*/ | ||
public static class EncodeSlotPushDownVisitor extends PlanVisitor<Plan, List<Alias>> { | ||
public static EncodeSlotPushDownVisitor visitor = new EncodeSlotPushDownVisitor(); | ||
|
||
@Override | ||
public Plan visit(Plan plan, List<Alias> encodeAlias) { | ||
// replaceMap: | ||
// encode_as_int(slot1) -> slot2 | ||
// slot1 -> slot2 | ||
Map<Expression, Slot> replaceMap = new HashMap<>(); | ||
List<Set<Slot>> byPassSlots = plan.children().stream() | ||
.map(this::getByPassSlot) | ||
.collect(Collectors.toList()); | ||
Map<Plan, List<Alias>> toBePushed = new HashMap<>(); | ||
for (Alias alias : encodeAlias) { | ||
EncodeStr encode = (EncodeStr) alias.child(); | ||
Expression strExpr = encode.child(); | ||
if (strExpr instanceof SlotReference) { | ||
for (int i = 0; i < byPassSlots.size(); i++) { | ||
if (byPassSlots.get(i).contains(strExpr)) { | ||
toBePushed.putIfAbsent(plan.child(i), new ArrayList<>()); | ||
toBePushed.get(plan.child(i)).add(alias); | ||
replaceMap.put(alias, alias.toSlot()); | ||
replaceMap.put(alias.child().child(0), alias.toSlot()); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
// rewrite plan according to encode expression | ||
// for example: project(encode_as_int(slot1) as slot2) | ||
// 1. rewrite project's expressions: project(slot2), | ||
// 2. push encode_as_int(slot1) as slot2 down to project.child() | ||
// rewrite expressions | ||
plan = plan.replaceExpressions(replaceMap); | ||
// rewrite children | ||
ImmutableList.Builder<Plan> newChildren = ImmutableList.builderWithExpectedSize(plan.arity()); | ||
boolean hasNewChildren = false; | ||
for (Plan child : plan.children()) { | ||
Plan newChild; | ||
if (toBePushed.containsKey(child)) { | ||
if (child instanceof LogicalProject && child.child(0) instanceof LogicalCatalogRelation) { | ||
LogicalProject project = (LogicalProject) child; | ||
List<NamedExpression> projections = new ArrayList<>(); | ||
projections.addAll(toBePushed.get(project)); | ||
projections.addAll(project.getProjects()); | ||
newChild = project.withProjects(projections); | ||
} else if (child instanceof LogicalCatalogRelation) { | ||
List<NamedExpression> newProjections = new ArrayList<>(); | ||
newProjections.addAll(child.getOutput()); | ||
newProjections.addAll(toBePushed.get(child)); | ||
newChild = new LogicalProject<>(newProjections, child); | ||
hasNewChildren = true; | ||
} else { | ||
newChild = child.accept(this, toBePushed.get(child)); | ||
} | ||
if (!hasNewChildren && newChild != child) { | ||
hasNewChildren = true; | ||
} | ||
} else { | ||
newChild = child; | ||
} | ||
newChildren.add(newChild); | ||
} | ||
|
||
if (hasNewChildren) { | ||
plan = plan.withChildren(newChildren.build()); | ||
} | ||
return plan; | ||
} | ||
|
||
private Set<Slot> getByPassSlot(Plan plan) { | ||
Set<Slot> outputSlots = Sets.newHashSet(plan.getOutput()); | ||
outputSlots.removeAll(plan.getInputSlots()); | ||
return outputSlots; | ||
} | ||
} | ||
|
||
} |
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
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
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
Oops, something went wrong.