You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by "xzj7019 (via GitHub)" <gi...@apache.org> on 2024/02/02 13:02:10 UTC

[PR] [nereids] group by key elimination [doris]

xzj7019 opened a new pull request, #30774:
URL: https://github.com/apache/doris/pull/30774

   ## Proposed changes
   
   group by key elimination
   
   ## Further comments
   
   If this is a relatively large or complex change, kick off the discussion at [dev@doris.apache.org](mailto:dev@doris.apache.org) by explaining why you chose the solution you did and what alternatives you considered, etc...
   
   


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "XieJiann (via GitHub)" <gi...@apache.org>.
XieJiann commented on code in PR #30774:
URL: https://github.com/apache/doris/pull/30774#discussion_r1477309414


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/util/JoinUtils.java:
##########
@@ -330,15 +331,17 @@ public static boolean canEliminateByFk(LogicalJoin<?, ?> join, Plan primaryPlan,
     }
 
     /**
-     * can this join be eliminated by its left child
+     * if canEliminateByLeft
      */
-    public static boolean canEliminateByLeft(LogicalJoin<?, ?> join, FunctionalDependencies rightFuncDeps) {
+    public static boolean canEliminateByLeft(LogicalJoin<?, ?> join, ImmutableSet<FdItem> fdItems) {
         if (join.getJoinType().isLeftOuterJoin()) {
             Pair<Set<Slot>, Set<Slot>> njHashKeys = join.extractNullRejectHashKeys();
             if (!join.getOtherJoinConjuncts().isEmpty() || njHashKeys == null) {
                 return false;
             }
-            return rightFuncDeps.isUnique(njHashKeys.second);
+            return fdItems.stream().anyMatch(e -> e.getParentExprs().containsAll(njHashKeys.second)
+                    && e.isUnique()
+                    && !e.isCandidate());

Review Comment:
   as stated above, the inferring of uniqueness is incomplete



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "yiguolei (via GitHub)" <gi...@apache.org>.
yiguolei merged PR #30774:
URL: https://github.com/apache/doris/pull/30774


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "XieJiann (via GitHub)" <gi...@apache.org>.
XieJiann commented on code in PR #30774:
URL: https://github.com/apache/doris/pull/30774#discussion_r1477308360


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/properties/ExprFdItem.java:
##########
@@ -0,0 +1,41 @@
+// 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.properties;
+
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+
+import com.google.common.collect.ImmutableSet;
+
+/**
+ * ExprFdItem
+ */
+public class ExprFdItem extends FdItem {
+    private ImmutableSet<SlotReference> childExprs;
+
+    public ExprFdItem(ImmutableSet<SlotReference> parentExprs, boolean isUnique,
+            ImmutableSet<SlotReference> childExprs) {
+        super(parentExprs, isUnique, false);
+        this.childExprs = ImmutableSet.copyOf(childExprs);
+    }
+
+    @Override
+    public boolean checkExprInChild(SlotReference slot, LogicalPlan childPlan) {
+        return childExprs.contains(slot);
+    }
+}

Review Comment:
   The uniform information is lost?



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "XieJiann (via GitHub)" <gi...@apache.org>.
XieJiann commented on code in PR #30774:
URL: https://github.com/apache/doris/pull/30774#discussion_r1477310488


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/properties/FdItem.java:
##########
@@ -0,0 +1,64 @@
+// 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.properties;
+
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+
+import com.google.common.collect.ImmutableSet;
+
+/**
+ * FdItem
+ */
+public class FdItem {
+    private ImmutableSet<SlotReference> parentExprs;
+
+    private boolean isUnique;
+
+    private boolean isCandidate;

Review Comment:
   what's the meaning of the candidate? It seems to be not obvious



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "doris-robot (via GitHub)" <gi...@apache.org>.
doris-robot commented on PR #30774:
URL: https://github.com/apache/doris/pull/30774#issuecomment-1923759440

   Thank you for your contribution to Apache Doris.
   Don't know what should be done next? See [How to process your PR](https://cwiki.apache.org/confluence/display/DORIS/How+to+process+your+PR)


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "XieJiann (via GitHub)" <gi...@apache.org>.
XieJiann commented on code in PR #30774:
URL: https://github.com/apache/doris/pull/30774#discussion_r1477308879


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateGroupBy.java:
##########
@@ -50,8 +53,11 @@ public Rule build() {
                     Set<Slot> groupby = agg.getGroupByExpressions().stream().map(e -> (Slot) e)
                             .collect(Collectors.toSet());
                     Plan child = agg.child();
-                    boolean unique = child.getLogicalProperties().getFunctionalDependencies()
-                            .isUniqueAndNotNull(groupby);
+                    //boolean unique = child.getLogicalProperties().getFunctionalDependencies()
+                    //        .isUniqueAndNotNull(groupby);
+                    ImmutableSet<FdItem> fdItems = child.getLogicalProperties().getFdItems();
+                    boolean unique = !groupby.isEmpty() && fdItems.stream().anyMatch(e ->
+                            e.isUnique() && !e.isCandidate() && groupby.containsAll(e.getParentExprs()));

Review Comment:
   It's incomplete. such as :
   ```
   select * from (select a, b, sum(a + b) as c group by a, b) group by a b c
   ```
   `a c` are unique, and uniqueness of `a, b, c` also can be infered . That is the uniqueness can be inferred by uniqueness of any subset



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "XieJiann (via GitHub)" <gi...@apache.org>.
XieJiann commented on code in PR #30774:
URL: https://github.com/apache/doris/pull/30774#discussion_r1479207444


##########
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);
+                    if (isContain) {
+                        minParentExprCnt = parentExprs.size();
+                        minParentExprs = ImmutableSet.copyOf(parentExprs);
+                    }
+                }
+            }
+
+            Set<Integer> rootExprsSet = new HashSet<>();
+            List<SlotReference> rootExprs = new ArrayList<>();
+            Set<Integer> eliminateSet = new HashSet<>();
+            if (minParentExprs.size() > 0) {
+                // if any unique fd item found, find the expr which matching parentExprs
+                // from candiExprs directly
+                for (int i = 0; i < minParentExprs.size(); i++) {
+                    int index = findEqualExpr(candiExprs, minParentExprs.asList().get(i));
+                    if (index != -1) {
+                        rootExprsSet.add(new Integer(index));
+                    } else {
+                        return null;
+                    }
+                }
+            } else {
+                // no unique fd item found, try to find the smallest root exprs set
+                // from non-unique fd items.
+                for (int i = 0; i < nonUniqueFdItems.size() && eliminateSet.size() < candiExprs.size(); i++) {
+                    FdItem fdItem = nonUniqueFdItems.get(i);
+                    ImmutableSet<SlotReference> parentExprs = fdItem.getParentExprs();
+                    boolean isContains = isExprsContainFdParent(candiExprs, fdItem);
+                    if (isContains) {
+                        List<SlotReference> leftDomain = new ArrayList<>();
+                        List<SlotReference> rightDomain = new ArrayList<>();
+                        // generate new root exprs
+                        for (int j = 0; j < rootExprs.size(); j++) {
+                            leftDomain.add(rootExprs.get(j));
+                            boolean isInChild = fdItem.checkExprInChild(rootExprs.get(j), childPlan);
+                            if (!isInChild) {
+                                rightDomain.add(rootExprs.get(j));
+                            }
+                        }
+                        for (int j = 0; j < parentExprs.size(); j++) {
+                            int index = findEqualExpr(candiExprs, parentExprs.asList().get(j));
+                            if (index != -1) {
+                                rightDomain.add(candiExprs.get(index));
+                                if (!eliminateSet.contains(index)) {
+                                    leftDomain.add(candiExprs.get(index));
+                                }
+                            }
+                        }
+                        // check fd can eliminate new candi expr
+                        for (int j = 0; j < candiExprs.size(); j++) {
+                            if (!eliminateSet.contains(j)) {
+                                boolean isInChild = fdItem.checkExprInChild(candiExprs.get(j), childPlan);
+                                if (isInChild) {
+                                    eliminateSet.add(j);
+                                }
+                            }
+                        }
+                        // if fd eliminate new candi exprs or new root exprs is smaller than the older,
+                        // than use new root expr to replace old ones
+                        List<SlotReference> newRootExprs = leftDomain.size() <= rightDomain.size()
+                                ? leftDomain : rightDomain;
+                        rootExprs.clear();
+                        rootExprs.addAll(newRootExprs);
+                    }
+                }
+            }
+            // find the root expr, add into root exprs set, indicate the index in
+            // candiExprs list
+            for (int i = 0; i < rootExprs.size(); i++) {
+                int index = findEqualExpr(candiExprs, rootExprs.get(i));
+                if (index != -1) {
+                    rootExprsSet.add(new Integer(index));
+                } else {
+                    return null;
+                }
+            }

Review Comment:
   We don't need it after checking isContaining



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "XieJiann (via GitHub)" <gi...@apache.org>.
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 the smallest parent 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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "XieJiann (via GitHub)" <gi...@apache.org>.
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 the smallest parent 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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "doris-robot (via GitHub)" <gi...@apache.org>.
doris-robot commented on PR #30774:
URL: https://github.com/apache/doris/pull/30774#issuecomment-1927072041

   
   <details>
   <summary>TPC-H: <b>Total hot run time: 36567 ms</b></summary>
   
   ```
   machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
   scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
   Tpch sf100 test result on commit 3f6b13792e63f604b1ae2c3a8a9098940cab9da5, data reload: false
   
   ------ Round 1 ----------------------------------
   q1	17675	5031	4381	4381
   q2	2053	142	131	131
   q3	10672	906	924	906
   q4	4638	749	669	669
   q5	7697	2794	2820	2794
   q6	183	120	118	118
   q7	1148	716	721	716
   q8	9281	1987	2012	1987
   q9	7251	6353	6262	6262
   q10	8088	2395	2415	2395
   q11	418	214	194	194
   q12	792	257	279	257
   q13	18015	3288	3274	3274
   q14	275	253	237	237
   q15	521	490	492	490
   q16	485	423	417	417
   q17	913	487	508	487
   q18	6731	5998	5898	5898
   q19	1565	1392	1313	1313
   q20	563	351	312	312
   q21	7013	3058	3060	3058
   q22	806	312	271	271
   Total cold run time: 106783 ms
   Total hot run time: 36567 ms
   
   ----- Round 2, with runtime_filter_mode=off -----
   q1	4368	4424	4287	4287
   q2	337	227	234	227
   q3	2966	2834	2765	2765
   q4	1903	1636	1639	1636
   q5	5137	5266	5184	5184
   q6	188	113	112	112
   q7	2185	1807	1784	1784
   q8	3070	3279	3222	3222
   q9	8332	8314	8275	8275
   q10	5782	3573	3578	3573
   q11	560	458	475	458
   q12	734	564	558	558
   q13	14054	3015	3049	3015
   q14	289	246	266	246
   q15	536	487	498	487
   q16	511	476	487	476
   q17	1801	1703	1701	1701
   q18	7925	7805	7552	7552
   q19	7468	1537	1535	1535
   q20	2118	1929	1903	1903
   q21	4841	4576	4733	4576
   q22	554	481	475	475
   Total cold run time: 75659 ms
   Total hot run time: 54047 ms
   ```
   </details>
   


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "XieJiann (via GitHub)" <gi...@apache.org>.
XieJiann commented on code in PR #30774:
URL: https://github.com/apache/doris/pull/30774#discussion_r1477308509


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/properties/LogicalProperties.java:
##########
@@ -42,11 +42,12 @@ public class LogicalProperties {
     protected final Supplier<Set<Slot>> outputSetSupplier;
     protected final Supplier<Map<Slot, Slot>> outputMapSupplier;
     protected final Supplier<Set<ExprId>> outputExprIdSetSupplier;
-    protected final Supplier<FunctionalDependencies> fdSupplier;
+    protected final Supplier<ImmutableSet<FdItem>> fdItemsSupplier;
     private Integer hashCode = null;

Review Comment:
   why not put it in FunctionalDependencies and keep the same API with the origin 



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "XieJiann (via GitHub)" <gi...@apache.org>.
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #30774:
URL: https://github.com/apache/doris/pull/30774#issuecomment-1928622590

   PR approved by at least one committer and no changes requested.


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "xzj7019 (via GitHub)" <gi...@apache.org>.
xzj7019 commented on PR #30774:
URL: https://github.com/apache/doris/pull/30774#issuecomment-1926595704

   run buildall


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "XieJiann (via GitHub)" <gi...@apache.org>.
XieJiann commented on code in PR #30774:
URL: https://github.com/apache/doris/pull/30774#discussion_r1477308879


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateGroupBy.java:
##########
@@ -50,8 +53,11 @@ public Rule build() {
                     Set<Slot> groupby = agg.getGroupByExpressions().stream().map(e -> (Slot) e)
                             .collect(Collectors.toSet());
                     Plan child = agg.child();
-                    boolean unique = child.getLogicalProperties().getFunctionalDependencies()
-                            .isUniqueAndNotNull(groupby);
+                    //boolean unique = child.getLogicalProperties().getFunctionalDependencies()
+                    //        .isUniqueAndNotNull(groupby);
+                    ImmutableSet<FdItem> fdItems = child.getLogicalProperties().getFdItems();
+                    boolean unique = !groupby.isEmpty() && fdItems.stream().anyMatch(e ->
+                            e.isUnique() && !e.isCandidate() && groupby.containsAll(e.getParentExprs()));

Review Comment:
   It's incomplete. such as :
   ```
   select * from (select a, b, sum(a + b) as c group by a, b) group by a b c
   ```
   a c are unique, and unique a, b, c also can be infered . That is the uniqueness can be inferred by uniqueness of any subset



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "XieJiann (via GitHub)" <gi...@apache.org>.
XieJiann commented on code in PR #30774:
URL: https://github.com/apache/doris/pull/30774#discussion_r1479207677


##########
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);
+                    if (isContain) {
+                        minParentExprCnt = parentExprs.size();
+                        minParentExprs = ImmutableSet.copyOf(parentExprs);
+                    }
+                }
+            }
+
+            Set<Integer> rootExprsSet = new HashSet<>();
+            List<SlotReference> rootExprs = new ArrayList<>();
+            Set<Integer> eliminateSet = new HashSet<>();
+            if (minParentExprs.size() > 0) {
+                // if any unique fd item found, find the expr which matching parentExprs
+                // from candiExprs directly
+                for (int i = 0; i < minParentExprs.size(); i++) {
+                    int index = findEqualExpr(candiExprs, minParentExprs.asList().get(i));
+                    if (index != -1) {
+                        rootExprsSet.add(new Integer(index));
+                    } else {
+                        return null;
+                    }
+                }

Review Comment:
   We don't need it after checking` isContaining`:
   ```
   for (int i = 0; i < minParentExprs.size(); i++) {
                       int index = findEqualExpr(candiExprs, minParentExprs.asList().get(i));
                       if (index != -1) {
                           rootExprsSet.add(new Integer(index));
                       } else {
                           return null;
                       }
                   }
   ```



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "doris-robot (via GitHub)" <gi...@apache.org>.
doris-robot commented on PR #30774:
URL: https://github.com/apache/doris/pull/30774#issuecomment-1927145262

   
   <details>
   <summary>TPC-H: <b>Total hot run time: 37194 ms</b></summary>
   
   ```
   machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
   scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
   Tpch sf100 test result on commit 5655d42c1ac42b3330c4567ee82122df146c846a, data reload: false
   
   ------ Round 1 ----------------------------------
   q1	17743	4482	4422	4422
   q2	2045	145	133	133
   q3	10595	926	947	926
   q4	4644	715	738	715
   q5	7722	2883	2926	2883
   q6	184	119	119	119
   q7	1164	748	737	737
   q8	9275	2014	2028	2014
   q9	7355	6399	6434	6399
   q10	8196	2449	2432	2432
   q11	428	202	222	202
   q12	749	296	287	287
   q13	18022	3300	3268	3268
   q14	273	257	247	247
   q15	542	495	494	494
   q16	526	431	424	424
   q17	949	527	548	527
   q18	6811	6004	5927	5927
   q19	1586	1352	1408	1352
   q20	589	337	331	331
   q21	6914	3111	3059	3059
   q22	786	307	296	296
   Total cold run time: 107098 ms
   Total hot run time: 37194 ms
   
   ----- Round 2, with runtime_filter_mode=off -----
   q1	4592	4430	4319	4319
   q2	327	235	223	223
   q3	3011	2830	2844	2830
   q4	1876	1681	1634	1634
   q5	5258	5258	5236	5236
   q6	192	112	113	112
   q7	2176	1789	1770	1770
   q8	3133	3265	3246	3246
   q9	8430	8358	8295	8295
   q10	5791	3606	3596	3596
   q11	535	456	466	456
   q12	778	563	601	563
   q13	7260	3104	3108	3104
   q14	274	250	278	250
   q15	523	496	495	495
   q16	526	456	484	456
   q17	1884	1700	1693	1693
   q18	8072	7935	7621	7621
   q19	10164	1504	1477	1477
   q20	2113	1910	1910	1910
   q21	4833	4568	4668	4568
   q22	561	490	497	490
   Total cold run time: 72309 ms
   Total hot run time: 54344 ms
   ```
   </details>
   


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "xzj7019 (via GitHub)" <gi...@apache.org>.
xzj7019 commented on PR #30774:
URL: https://github.com/apache/doris/pull/30774#issuecomment-1926416447

   run buildall


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "xzj7019 (via GitHub)" <gi...@apache.org>.
xzj7019 commented on PR #30774:
URL: https://github.com/apache/doris/pull/30774#issuecomment-1927081886

   run buildall


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "doris-robot (via GitHub)" <gi...@apache.org>.
doris-robot commented on PR #30774:
URL: https://github.com/apache/doris/pull/30774#issuecomment-1927180311

   
   <details>
   <summary>ClickBench: <b>Total hot run time: 30.17 s</b></summary>
   
   ```
   machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
   scripts: https://github.com/apache/doris/tree/master/tools/clickbench-tools
   ClickBench test result on commit 5655d42c1ac42b3330c4567ee82122df146c846a, data reload: false
   
   query1	0.04	0.03	0.04
   query2	0.06	0.03	0.02
   query3	0.22	0.06	0.06
   query4	1.69	0.10	0.09
   query5	0.53	0.52	0.51
   query6	1.20	0.66	0.66
   query7	0.02	0.01	0.02
   query8	0.04	0.02	0.02
   query9	0.56	0.49	0.49
   query10	0.56	0.57	0.55
   query11	0.12	0.08	0.09
   query12	0.10	0.09	0.09
   query13	0.59	0.60	0.60
   query14	0.77	0.81	0.79
   query15	0.79	0.78	0.77
   query16	0.37	0.39	0.38
   query17	1.03	1.04	1.02
   query18	0.23	0.27	0.24
   query19	1.88	1.76	1.78
   query20	0.01	0.01	0.01
   query21	15.44	0.55	0.58
   query22	2.42	2.62	1.68
   query23	17.38	0.79	0.72
   query24	2.83	1.47	0.43
   query25	0.32	0.18	0.09
   query26	0.55	0.14	0.14
   query27	0.06	0.04	0.08
   query28	11.39	0.84	0.82
   query29	12.50	3.19	3.18
   query30	0.59	0.50	0.53
   query31	2.78	0.34	0.36
   query32	3.37	0.47	0.48
   query33	3.25	3.21	3.22
   query34	15.76	4.34	4.28
   query35	4.28	4.31	4.23
   query36	1.10	1.06	1.06
   query37	0.06	0.05	0.05
   query38	0.03	0.03	0.02
   query39	0.02	0.02	0.01
   query40	0.16	0.13	0.12
   query41	0.07	0.02	0.01
   query42	0.02	0.02	0.02
   query43	0.03	0.02	0.02
   Total cold run time: 105.22 s
   Total hot run time: 30.17 s
   ```
   </details>
   


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "doris-robot (via GitHub)" <gi...@apache.org>.
doris-robot commented on PR #30774:
URL: https://github.com/apache/doris/pull/30774#issuecomment-1927187839

   
   Load test result on machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
   ```
   Load test result on commit 5655d42c1ac42b3330c4567ee82122df146c846a with default session variables
   Stream load json:         18 seconds loaded 2358488459 Bytes, about 124 MB/s
   Stream load orc:          59 seconds loaded 1101869774 Bytes, about 17 MB/s
   Stream load parquet:      31 seconds loaded 861443392 Bytes, about 26 MB/s
   Insert into select:       13.9 seconds inserted 10000000 Rows, about 719K ops/s
   ```
   


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "xzj7019 (via GitHub)" <gi...@apache.org>.
xzj7019 commented on PR #30774:
URL: https://github.com/apache/doris/pull/30774#issuecomment-1927010751

   run buildall


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "doris-robot (via GitHub)" <gi...@apache.org>.
doris-robot commented on PR #30774:
URL: https://github.com/apache/doris/pull/30774#issuecomment-1927168761

   
   <details>
   <summary>TPC-DS: <b>Total hot run time: 173530 ms</b></summary>
   
   ```
   machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
   scripts: https://github.com/apache/doris/tree/master/tools/tpcds-tools
   TPC-DS sf100 test result on commit 5655d42c1ac42b3330c4567ee82122df146c846a, data reload: false
   
   query1	929	344	338	338
   query2	6531	1985	1993	1985
   query3	6705	212	212	212
   query4	31923	22021	21987	21987
   query5	4255	359	358	358
   query6	253	167	161	161
   query7	4591	283	274	274
   query8	244	173	181	173
   query9	9152	2309	2294	2294
   query10	414	213	202	202
   query11	18259	15405	15445	15405
   query12	132	75	75	75
   query13	1617	402	408	402
   query14	9010	6494	7148	6494
   query15	233	179	180	179
   query16	8142	262	244	244
   query17	1928	529	503	503
   query18	2102	271	266	266
   query19	366	140	138	138
   query20	79	76	73	73
   query21	198	126	124	124
   query22	5040	4581	4615	4581
   query23	31223	30081	29939	29939
   query24	10520	2745	2772	2745
   query25	536	345	339	339
   query26	708	144	157	144
   query27	2178	293	301	293
   query28	5868	1850	1833	1833
   query29	864	611	585	585
   query30	287	129	142	129
   query31	910	694	699	694
   query32	95	59	53	53
   query33	632	223	225	223
   query34	833	452	460	452
   query35	853	757	748	748
   query36	1004	920	953	920
   query37	92	56	60	56
   query38	3240	3136	3134	3134
   query39	1287	1247	1241	1241
   query40	183	92	91	91
   query41	38	34	34	34
   query42	98	89	87	87
   query43	515	489	500	489
   query44	1074	683	701	683
   query45	190	184	175	175
   query46	1032	660	655	655
   query47	1560	1488	1502	1488
   query48	423	355	350	350
   query49	1048	289	278	278
   query50	772	379	384	379
   query51	5302	5125	5221	5125
   query52	101	82	86	82
   query53	340	268	267	267
   query54	276	206	209	206
   query55	83	72	72	72
   query56	209	205	192	192
   query57	982	899	903	899
   query58	197	177	178	177
   query59	2412	2200	2225	2200
   query60	232	210	219	210
   query61	82	86	79	79
   query62	664	360	333	333
   query63	296	258	280	258
   query64	4786	3697	3444	3444
   query65	3253	3235	3218	3218
   query66	822	313	311	311
   query67	14525	14322	14165	14165
   query68	4100	527	529	527
   query69	454	341	332	332
   query70	1252	1244	1181	1181
   query71	330	244	257	244
   query72	6021	2809	2625	2625
   query73	717	321	313	313
   query74	6509	6247	6134	6134
   query75	3036	2331	2297	2297
   query76	2569	931	949	931
   query77	363	228	229	228
   query78	9096	8759	8467	8467
   query79	3314	499	499	499
   query80	1847	349	336	336
   query81	535	196	198	196
   query82	1033	84	79	79
   query83	244	125	126	125
   query84	285	86	74	74
   query85	2013	328	327	327
   query86	476	302	312	302
   query87	3393	3177	3222	3177
   query88	4197	2287	2314	2287
   query89	445	355	361	355
   query90	1945	162	163	162
   query91	148	126	138	126
   query92	52	44	44	44
   query93	5237	473	476	473
   query94	1244	179	170	170
   query95	8105	350	7822	350
   query96	604	271	277	271
   query97	4232	4072	4146	4072
   query98	224	205	191	191
   query99	1130	738	692	692
   Total cold run time: 289878 ms
   Total hot run time: 173530 ms
   ```
   </details>
   


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #30774:
URL: https://github.com/apache/doris/pull/30774#issuecomment-1928622636

   PR approved by anyone and no changes requested.


-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "XieJiann (via GitHub)" <gi...@apache.org>.
XieJiann commented on code in PR #30774:
URL: https://github.com/apache/doris/pull/30774#discussion_r1479207444


##########
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);
+                    if (isContain) {
+                        minParentExprCnt = parentExprs.size();
+                        minParentExprs = ImmutableSet.copyOf(parentExprs);
+                    }
+                }
+            }
+
+            Set<Integer> rootExprsSet = new HashSet<>();
+            List<SlotReference> rootExprs = new ArrayList<>();
+            Set<Integer> eliminateSet = new HashSet<>();
+            if (minParentExprs.size() > 0) {
+                // if any unique fd item found, find the expr which matching parentExprs
+                // from candiExprs directly
+                for (int i = 0; i < minParentExprs.size(); i++) {
+                    int index = findEqualExpr(candiExprs, minParentExprs.asList().get(i));
+                    if (index != -1) {
+                        rootExprsSet.add(new Integer(index));
+                    } else {
+                        return null;
+                    }
+                }
+            } else {
+                // no unique fd item found, try to find the smallest root exprs set
+                // from non-unique fd items.
+                for (int i = 0; i < nonUniqueFdItems.size() && eliminateSet.size() < candiExprs.size(); i++) {
+                    FdItem fdItem = nonUniqueFdItems.get(i);
+                    ImmutableSet<SlotReference> parentExprs = fdItem.getParentExprs();
+                    boolean isContains = isExprsContainFdParent(candiExprs, fdItem);
+                    if (isContains) {
+                        List<SlotReference> leftDomain = new ArrayList<>();
+                        List<SlotReference> rightDomain = new ArrayList<>();
+                        // generate new root exprs
+                        for (int j = 0; j < rootExprs.size(); j++) {
+                            leftDomain.add(rootExprs.get(j));
+                            boolean isInChild = fdItem.checkExprInChild(rootExprs.get(j), childPlan);
+                            if (!isInChild) {
+                                rightDomain.add(rootExprs.get(j));
+                            }
+                        }
+                        for (int j = 0; j < parentExprs.size(); j++) {
+                            int index = findEqualExpr(candiExprs, parentExprs.asList().get(j));
+                            if (index != -1) {
+                                rightDomain.add(candiExprs.get(index));
+                                if (!eliminateSet.contains(index)) {
+                                    leftDomain.add(candiExprs.get(index));
+                                }
+                            }
+                        }
+                        // check fd can eliminate new candi expr
+                        for (int j = 0; j < candiExprs.size(); j++) {
+                            if (!eliminateSet.contains(j)) {
+                                boolean isInChild = fdItem.checkExprInChild(candiExprs.get(j), childPlan);
+                                if (isInChild) {
+                                    eliminateSet.add(j);
+                                }
+                            }
+                        }
+                        // if fd eliminate new candi exprs or new root exprs is smaller than the older,
+                        // than use new root expr to replace old ones
+                        List<SlotReference> newRootExprs = leftDomain.size() <= rightDomain.size()
+                                ? leftDomain : rightDomain;
+                        rootExprs.clear();
+                        rootExprs.addAll(newRootExprs);
+                    }
+                }
+            }
+            // find the root expr, add into root exprs set, indicate the index in
+            // candiExprs list
+            for (int i = 0; i < rootExprs.size(); i++) {
+                int index = findEqualExpr(candiExprs, rootExprs.get(i));
+                if (index != -1) {
+                    rootExprsSet.add(new Integer(index));
+                } else {
+                    return null;
+                }
+            }

Review Comment:
   We don't need it after checking isContaining



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org


Re: [PR] [nereids] group by key elimination [doris]

Posted by "XieJiann (via GitHub)" <gi...@apache.org>.
XieJiann commented on code in PR #30774:
URL: https://github.com/apache/doris/pull/30774#discussion_r1479207677


##########
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);
+                    if (isContain) {
+                        minParentExprCnt = parentExprs.size();
+                        minParentExprs = ImmutableSet.copyOf(parentExprs);
+                    }
+                }
+            }
+
+            Set<Integer> rootExprsSet = new HashSet<>();
+            List<SlotReference> rootExprs = new ArrayList<>();
+            Set<Integer> eliminateSet = new HashSet<>();
+            if (minParentExprs.size() > 0) {
+                // if any unique fd item found, find the expr which matching parentExprs
+                // from candiExprs directly
+                for (int i = 0; i < minParentExprs.size(); i++) {
+                    int index = findEqualExpr(candiExprs, minParentExprs.asList().get(i));
+                    if (index != -1) {
+                        rootExprsSet.add(new Integer(index));
+                    } else {
+                        return null;
+                    }
+                }

Review Comment:
   We don't need it after checking isContaining



-- 
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-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org