|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.nereids.rules.rewrite; |
| 19 | + |
| 20 | +import org.apache.doris.nereids.properties.OrderKey; |
| 21 | +import org.apache.doris.nereids.rules.Rule; |
| 22 | +import org.apache.doris.nereids.rules.RuleType; |
| 23 | +import org.apache.doris.nereids.trees.expressions.Alias; |
| 24 | +import org.apache.doris.nereids.trees.expressions.Expression; |
| 25 | +import org.apache.doris.nereids.trees.expressions.NamedExpression; |
| 26 | +import org.apache.doris.nereids.trees.expressions.Slot; |
| 27 | +import org.apache.doris.nereids.trees.expressions.SlotReference; |
| 28 | +import org.apache.doris.nereids.trees.plans.Plan; |
| 29 | +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; |
| 30 | +import org.apache.doris.nereids.trees.plans.logical.LogicalTopN; |
| 31 | +import org.apache.doris.qe.ConnectContext; |
| 32 | + |
| 33 | +import org.apache.logging.log4j.LogManager; |
| 34 | +import org.apache.logging.log4j.Logger; |
| 35 | + |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.HashMap; |
| 38 | +import java.util.List; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.Set; |
| 41 | + |
| 42 | +/** |
| 43 | + * |
| 44 | + * try to reduce shuffle cost of topN operator, used to optimize plan after applying Compress_materialize |
| 45 | + * |
| 46 | + * topn(orderKey=[a]) |
| 47 | + * --> project(a+1 as x, a+2 as y, a) |
| 48 | + * --> any(output(a)) |
| 49 | + * => |
| 50 | + * project(a+1 as x, a+2 as y, a) |
| 51 | + * --> topn(orderKey=[a]) |
| 52 | + * --> any(output(a)) |
| 53 | + * |
| 54 | + */ |
| 55 | +public class PullUpProjectBetweenTopNAndAgg extends OneRewriteRuleFactory { |
| 56 | + public static final Logger LOG = LogManager.getLogger(PullUpProjectBetweenTopNAndAgg.class); |
| 57 | + |
| 58 | + @Override |
| 59 | + public Rule build() { |
| 60 | + return logicalTopN(logicalProject(logicalAggregate())) |
| 61 | + .when(topN -> ConnectContext.get() != null |
| 62 | + && ConnectContext.get().getSessionVariable().enableCompressMaterialize) |
| 63 | + .then(topN -> adjust(topN)).toRule(RuleType.ADJUST_TOPN_PROJECT); |
| 64 | + } |
| 65 | + |
| 66 | + private Plan adjust(LogicalTopN<? extends Plan> topN) { |
| 67 | + LogicalProject<Plan> project = (LogicalProject<Plan>) topN.child(); |
| 68 | + Set<Slot> projectInputSlots = project.getInputSlots(); |
| 69 | + Map<SlotReference, SlotReference> keyAsKey = new HashMap<>(); |
| 70 | + for (NamedExpression proj : project.getProjects()) { |
| 71 | + if (proj instanceof Alias && ((Alias) proj).child(0) instanceof SlotReference) { |
| 72 | + keyAsKey.put((SlotReference) ((Alias) proj).toSlot(), (SlotReference) ((Alias) proj).child()); |
| 73 | + } |
| 74 | + } |
| 75 | + boolean match = true; |
| 76 | + List<OrderKey> newOrderKeys = new ArrayList<>(); |
| 77 | + for (OrderKey orderKey : topN.getOrderKeys()) { |
| 78 | + Expression orderExpr = orderKey.getExpr(); |
| 79 | + if (orderExpr instanceof SlotReference) { |
| 80 | + if (projectInputSlots.contains(orderExpr)) { |
| 81 | + newOrderKeys.add(orderKey); |
| 82 | + } else if (keyAsKey.containsKey(orderExpr)) { |
| 83 | + newOrderKeys.add(orderKey.withExpression(keyAsKey.get(orderExpr))); |
| 84 | + } else { |
| 85 | + match = false; |
| 86 | + break; |
| 87 | + } |
| 88 | + } else { |
| 89 | + match = false; |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + if (match) { |
| 94 | + if (project.getProjects().size() >= project.getInputSlots().size()) { |
| 95 | + LOG.info("$$$$ before: project.getProjects() = " + project.getProjects()); |
| 96 | + LOG.info("$$$$ before: project.getInputSlots() = " + project.getInputSlots()); |
| 97 | + LOG.info("$$$$ before: " + topN.treeString()); |
| 98 | + topN = topN.withChildren(project.children()).withOrderKeys(newOrderKeys); |
| 99 | + project = (LogicalProject<Plan>) project.withChildren(topN); |
| 100 | + LOG.info("$$$$ after:" + project.treeString()); |
| 101 | + return project; |
| 102 | + } |
| 103 | + } |
| 104 | + return topN; |
| 105 | + } |
| 106 | +} |
0 commit comments