From 0eeb18458157d7d7e90faf329f0dc94998dee83c Mon Sep 17 00:00:00 2001 From: minghong Date: Fri, 29 Nov 2024 17:45:17 +0800 Subject: [PATCH] update comments --- .../nereids/properties/LogicalProperties.java | 4 -- .../rules/rewrite/PushDownEncodeSlot.java | 39 ++++++++++++++----- .../rewrite/PushDownFilterThroughProject.java | 4 +- .../functions/scalar/EncodeAsBigInt.java | 2 +- .../functions/scalar/EncodeAsInt.java | 2 +- .../functions/scalar/EncodeAsLargeInt.java | 2 +- .../functions/scalar/EncodeAsSmallInt.java | 2 +- .../{EncodeStr.java => EncodeString.java} | 4 +- .../doris/nereids/trees/plans/Plan.java | 4 -- .../trees/plans/logical/LogicalProject.java | 18 --------- 10 files changed, 37 insertions(+), 44 deletions(-) rename fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/{EncodeStr.java => EncodeString.java} (88%) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/properties/LogicalProperties.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/properties/LogicalProperties.java index b889ec6abc7c6c4..3ac5a38c9143672 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/properties/LogicalProperties.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/properties/LogicalProperties.java @@ -94,10 +94,6 @@ public LogicalProperties(Supplier> outputSupplier, ); } - public LogicalProperties withOutputSupplier(Supplier> outputSupplier) { - return new LogicalProperties(outputSupplier, dataTraitSupplier); - } - public List getOutput() { return outputSupplier.get(); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownEncodeSlot.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownEncodeSlot.java index 1ddc44dfb3152b4..c97fd876962b2a8 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownEncodeSlot.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownEncodeSlot.java @@ -24,7 +24,7 @@ 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.expressions.functions.scalar.EncodeString; 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; @@ -78,13 +78,13 @@ private boolean containsEncode(LogicalProject project) { } private boolean containsEncode(Expression expr) { - return expr instanceof EncodeStr && expr.child(0) instanceof SlotReference; + return expr instanceof EncodeString && expr.child(0) instanceof SlotReference; } private List collectEncodeAlias(LogicalProject project) { List encodeAlias = new ArrayList<>(); project.getProjects().forEach(e -> { - if (e instanceof Alias && e.child(0) instanceof EncodeStr) { + if (e instanceof Alias && e.child(0) instanceof EncodeString) { encodeAlias.add((Alias) e); } }); @@ -94,7 +94,7 @@ private List collectEncodeAlias(LogicalProject project) { private LogicalProject pushDownEncodeSlot(LogicalProject project) { List encodeAlias = collectEncodeAlias(project); LogicalProject result = (LogicalProject) - project.accept(EncodeSlotPushDownVisitor.visitor, encodeAlias); + project.accept(EncodeSlotPushDownVisitor.INSTANCE, encodeAlias); return result; } @@ -102,7 +102,24 @@ private LogicalProject pushDownEncodeSlot(LogicalProject> { - public static EncodeSlotPushDownVisitor visitor = new EncodeSlotPushDownVisitor(); + public static EncodeSlotPushDownVisitor INSTANCE = new EncodeSlotPushDownVisitor(); + + public LogicalProject replaceProjectsEncodeSlot(LogicalProject project, Map replaceMap) { + List newProjections = new ArrayList<>(); + boolean changed = false; + for (NamedExpression expr : project.getProjects()) { + if (replaceMap.containsKey(expr) && replaceMap.get(expr) instanceof NamedExpression) { + newProjections.add((NamedExpression) replaceMap.get(expr)); + changed = true; + } else { + newProjections.add(expr); + } + } + if (changed) { + return project.withProjects(newProjections); + } + return project; + } @Override public Plan visit(Plan plan, List encodeAlias) { @@ -111,11 +128,11 @@ public Plan visit(Plan plan, List encodeAlias) { // slot1 -> slot2 Map replaceMap = new HashMap<>(); List> byPassSlots = plan.children().stream() - .map(this::getByPassSlot) + .map(this::getPassThroughSlots) .collect(Collectors.toList()); Map> toBePushed = new HashMap<>(); for (Alias alias : encodeAlias) { - EncodeStr encode = (EncodeStr) alias.child(); + EncodeString encode = (EncodeString) alias.child(); Expression strExpr = encode.child(); if (strExpr instanceof SlotReference) { for (int i = 0; i < byPassSlots.size(); i++) { @@ -134,7 +151,9 @@ public Plan visit(Plan plan, List encodeAlias) { // 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); + if (plan instanceof LogicalProject) { + plan = replaceProjectsEncodeSlot((LogicalProject)plan, replaceMap); + } // rewrite children ImmutableList.Builder newChildren = ImmutableList.builderWithExpectedSize(plan.arity()); boolean hasNewChildren = false; @@ -170,8 +189,8 @@ public Plan visit(Plan plan, List encodeAlias) { return plan; } - private Set getByPassSlot(Plan plan) { - Set outputSlots = Sets.newHashSet(plan.getOutput()); + private Set getPassThroughSlots(Plan plan) { + Set outputSlots = Sets.newHashSet(plan.getOutputSet()); outputSlots.removeAll(plan.getInputSlots()); return outputSlots; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownFilterThroughProject.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownFilterThroughProject.java index ab5eb11e8d76d7f..3963c1b651ba285 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownFilterThroughProject.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownFilterThroughProject.java @@ -23,7 +23,7 @@ import org.apache.doris.nereids.trees.expressions.Expression; import org.apache.doris.nereids.trees.expressions.Slot; import org.apache.doris.nereids.trees.expressions.functions.scalar.DecodeAsVarchar; -import org.apache.doris.nereids.trees.expressions.functions.scalar.EncodeStr; +import org.apache.doris.nereids.trees.expressions.functions.scalar.EncodeString; import org.apache.doris.nereids.trees.plans.Plan; import org.apache.doris.nereids.trees.plans.logical.LogicalFilter; import org.apache.doris.nereids.trees.plans.logical.LogicalLimit; @@ -149,7 +149,7 @@ private static Set eliminateDecodeAndEncode(Set expressi } private static Expression eliminateDecodeAndEncode(Expression expression) { - if (expression instanceof DecodeAsVarchar && expression.child(0) instanceof EncodeStr) { + if (expression instanceof DecodeAsVarchar && expression.child(0) instanceof EncodeString) { return expression.child(0).child(0); } boolean hasNewChild = false; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeAsBigInt.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeAsBigInt.java index 50a7efcc0f4d74f..d3f267efa36d337 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeAsBigInt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeAsBigInt.java @@ -33,7 +33,7 @@ /** * ScalarFunction 'EncodeAsBigInt'. */ -public class EncodeAsBigInt extends EncodeStr +public class EncodeAsBigInt extends EncodeString implements ExplicitlyCastableSignature, PropagateNullable { public static final List SIGNATURES = ImmutableList.of( diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeAsInt.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeAsInt.java index 208bae89c3fabf2..661f05fe3be82de 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeAsInt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeAsInt.java @@ -33,7 +33,7 @@ /** * ScalarFunction 'EncodeAsInt'. */ -public class EncodeAsInt extends EncodeStr +public class EncodeAsInt extends EncodeString implements ExplicitlyCastableSignature, PropagateNullable { public static final List SIGNATURES = ImmutableList.of( diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeAsLargeInt.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeAsLargeInt.java index bd0c5b23520ec99..ee8d723d2b5143a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeAsLargeInt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeAsLargeInt.java @@ -33,7 +33,7 @@ /** * ScalarFunction 'EncodeAsLargeInt'. */ -public class EncodeAsLargeInt extends EncodeStr +public class EncodeAsLargeInt extends EncodeString implements ExplicitlyCastableSignature, PropagateNullable { public static final List SIGNATURES = ImmutableList.of( diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeAsSmallInt.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeAsSmallInt.java index b57be9a0f433780..d0e6a1fa23b1213 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeAsSmallInt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeAsSmallInt.java @@ -33,7 +33,7 @@ /** * ScalarFunction 'CompressAsSmallInt'. */ -public class EncodeAsSmallInt extends EncodeStr +public class EncodeAsSmallInt extends EncodeString implements ExplicitlyCastableSignature, PropagateNullable { public static final List SIGNATURES = ImmutableList.of( diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeStr.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeString.java similarity index 88% rename from fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeStr.java rename to fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeString.java index d97bea748326e47..778d76c34625087 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeStr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/EncodeString.java @@ -23,11 +23,11 @@ /** * Encode_as_XXXInt */ -public abstract class EncodeStr extends ScalarFunction implements UnaryExpression { +public abstract class EncodeString extends ScalarFunction implements UnaryExpression { /** * constructor with 1 argument. */ - public EncodeStr(String name, Expression arg0) { + public EncodeString(String name, Expression arg0) { super(name, arg0); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/Plan.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/Plan.java index 3c564a01e6fe8ba..26122e68a9783f5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/Plan.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/Plan.java @@ -233,8 +233,4 @@ default String getGroupIdAsString() { default String getGroupIdWithPrefix() { return "@" + getGroupIdAsString(); } - - default Plan replaceExpressions(Map replaceMap) { - return this; - } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalProject.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalProject.java index e668726a4971799..171a355d8452d82 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalProject.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalProject.java @@ -278,22 +278,4 @@ public void computeFd(DataTrait.Builder builder) { } } } - - @Override - public Plan replaceExpressions(Map replaceMap) { - List newProjections = new ArrayList<>(); - boolean changed = false; - for (NamedExpression expr : getProjects()) { - if (replaceMap.containsKey(expr) && replaceMap.get(expr) instanceof NamedExpression) { - newProjections.add((NamedExpression) replaceMap.get(expr)); - changed = true; - } else { - newProjections.add(expr); - } - } - if (changed) { - return this.withProjects(newProjections); - } - return this; - } }