morrySnow commented on code in PR #44748: URL: https://github.com/apache/doris/pull/44748#discussion_r1862926785
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownEncodeSlot.java: ########## @@ -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 org.apache.doris.nereids.util.PlanUtils; + +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 = + PlanUtils.mergeProjections(project.getProjects(), toBePushed.get(child)); + 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()); Review Comment: could get outputSet directly by `getOutputSet` to avoid do uniq again ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownEncodeSlot.java: ########## @@ -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 org.apache.doris.nereids.util.PlanUtils; + +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)) Review Comment: why only skip LogicalCatalogRelation? what about other leaf node? ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownEncodeSlot.java: ########## @@ -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 org.apache.doris.nereids.util.PlanUtils; + +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)) Review Comment: ```suggestion .whenNot(project -> project.child() instanceof LogicalCatalogRelation) ``` ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownEncodeSlot.java: ########## @@ -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 org.apache.doris.nereids.util.PlanUtils; + +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 = + PlanUtils.mergeProjections(project.getProjects(), toBePushed.get(child)); + 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) { Review Comment: this function's name is diff with its action. it remove all bypass slot indeed. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownEncodeSlot.java: ########## @@ -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 org.apache.doris.nereids.util.PlanUtils; + +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(); Review Comment: ```suggestion public static EncodeSlotPushDownVisitor INSTANCE = new EncodeSlotPushDownVisitor(); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
