XieJiann commented on code in PR #30774: URL: https://github.com/apache/doris/pull/30774#discussion_r1479203582
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateGroupByKey.java: ########## @@ -0,0 +1,217 @@ +// 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.properties.FdItem; +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate; +import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; +import org.apache.doris.qe.ConnectContext; + +import com.google.common.collect.ImmutableSet; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * Eliminate group by key based on fd item information. + */ +public class EliminateGroupByKey extends OneRewriteRuleFactory { + @Override + public Rule build() { + return logicalAggregate(logicalProject()).then(agg -> { + Set<Integer> enableNereidsRules = ConnectContext.get().getSessionVariable().getEnableNereidsRules(); + if (!enableNereidsRules.contains(RuleType.ELIMINATE_GROUP_BY_KEY.type())) { + return null; + } + LogicalPlan childPlan = agg.child(); + List<FdItem> uniqueFdItems = new ArrayList<>(); + List<FdItem> nonUniqueFdItems = new ArrayList<>(); + if (agg.getGroupByExpressions().isEmpty() + || agg.getGroupByExpressions().equals(agg.getOutputExpressions()) + || !agg.getGroupByExpressions().stream().allMatch(e -> e instanceof SlotReference) + || agg.getGroupByExpressions().stream().allMatch(e -> + ((SlotReference) e).getColumn().isPresent() && ((SlotReference) e).getTable().isPresent())) { + return null; + } + ImmutableSet<FdItem> fdItems = childPlan.getLogicalProperties().getFunctionalDependencies().getFdItems(); + if (fdItems.isEmpty()) { + return null; + } + List<SlotReference> candiExprs = agg.getGroupByExpressions().stream() + .map(SlotReference.class::cast).collect(Collectors.toList()); + + fdItems.stream().filter(e -> !e.isCandidate()).forEach(e -> { + if (e.isUnique()) { + uniqueFdItems.add(e); + } else { + nonUniqueFdItems.add(e); + } + } + ); + + int minParentExprCnt = -1; + ImmutableSet<SlotReference> minParentExprs = ImmutableSet.of(); + // if unique fd items exists, try to find the one which has the + // smallest parent exprs + for (int i = 0; i < uniqueFdItems.size(); i++) { + FdItem fdItem = uniqueFdItems.get(i); + ImmutableSet<SlotReference> parentExprs = fdItem.getParentExprs(); + if (minParentExprCnt == -1 || parentExprs.size() < minParentExprCnt) { + boolean isContain = isExprsContainFdParent(candiExprs, fdItem); Review Comment: Is it redundant? If there are unique slots, we can always use one unique slot to replace all group by keys -- 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: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org