morrySnow commented on code in PR #39252:
URL: https://github.com/apache/doris/pull/39252#discussion_r1723083782


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/MaxMinFilterPushDown.java:
##########
@@ -0,0 +1,133 @@
+// 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.annotation.DependsRules;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.expression.ExpressionRewrite;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.GreaterThan;
+import org.apache.doris.nereids.trees.expressions.GreaterThanEqual;
+import org.apache.doris.nereids.trees.expressions.LessThan;
+import org.apache.doris.nereids.trees.expressions.LessThanEqual;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import 
org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction;
+import org.apache.doris.nereids.trees.expressions.functions.agg.Max;
+import org.apache.doris.nereids.trees.expressions.functions.agg.Min;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
+import org.apache.doris.nereids.util.ExpressionUtils;
+import org.apache.doris.nereids.util.PlanUtils;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+
+/**
+ * select id, max(a) from t group by id having max(a)>10;
+ * ->
+ * select id, max(a) from t where a>10 group by id;
+ * select id, min(a) from t group by id having min(a)<10;
+ * ->
+ * select id, min(a) from t where a<10 group by id;
+ */
+@DependsRules({
+        ExpressionRewrite.class
+})
+public class MaxMinFilterPushDown extends OneRewriteRuleFactory {
+    @Override
+    public Rule build() {
+        return logicalFilter(logicalAggregate())
+                .then(this::pushDownMaxMinFilter)
+                .toRule(RuleType.MAX_MIN_FILTER_PUSH_DOWN);
+    }
+
+    private Plan pushDownMaxMinFilter(LogicalFilter<LogicalAggregate<Plan>> 
filter) {
+        Set<Expression> conjuncts = filter.getConjuncts();
+        LogicalAggregate<Plan> agg = filter.child();
+        Plan aggChild = agg.child();
+        List<NamedExpression> aggOutputExpressions = 
agg.getOutputExpressions();
+        Set<Expression> aggFuncs = 
ExpressionUtils.collect(aggOutputExpressions,
+                expr -> expr instanceof AggregateFunction);
+        Set<Expression> maxMinFunc = ExpressionUtils.collect(aggFuncs,
+                expr -> expr instanceof Max || expr instanceof Min);

Review Comment:
   if group by key is empty, we should keep the filter upper aggregate



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/MaxMinFilterPushDown.java:
##########
@@ -0,0 +1,133 @@
+// 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.annotation.DependsRules;
+import org.apache.doris.nereids.rules.Rule;
+import org.apache.doris.nereids.rules.RuleType;
+import org.apache.doris.nereids.rules.expression.ExpressionRewrite;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.ExprId;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.GreaterThan;
+import org.apache.doris.nereids.trees.expressions.GreaterThanEqual;
+import org.apache.doris.nereids.trees.expressions.LessThan;
+import org.apache.doris.nereids.trees.expressions.LessThanEqual;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import 
org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction;
+import org.apache.doris.nereids.trees.expressions.functions.agg.Max;
+import org.apache.doris.nereids.trees.expressions.functions.agg.Min;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
+import org.apache.doris.nereids.util.ExpressionUtils;
+import org.apache.doris.nereids.util.PlanUtils;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+
+/**
+ * select id, max(a) from t group by id having max(a)>10;
+ * ->
+ * select id, max(a) from t where a>10 group by id;
+ * select id, min(a) from t group by id having min(a)<10;
+ * ->
+ * select id, min(a) from t where a<10 group by id;
+ */
+@DependsRules({
+        ExpressionRewrite.class
+})
+public class MaxMinFilterPushDown extends OneRewriteRuleFactory {
+    @Override
+    public Rule build() {
+        return logicalFilter(logicalAggregate())
+                .then(this::pushDownMaxMinFilter)
+                .toRule(RuleType.MAX_MIN_FILTER_PUSH_DOWN);
+    }
+
+    private Plan pushDownMaxMinFilter(LogicalFilter<LogicalAggregate<Plan>> 
filter) {
+        Set<Expression> conjuncts = filter.getConjuncts();
+        LogicalAggregate<Plan> agg = filter.child();
+        Plan aggChild = agg.child();
+        List<NamedExpression> aggOutputExpressions = 
agg.getOutputExpressions();
+        Set<Expression> aggFuncs = 
ExpressionUtils.collect(aggOutputExpressions,
+                expr -> expr instanceof AggregateFunction);
+        Set<Expression> maxMinFunc = ExpressionUtils.collect(aggFuncs,
+                expr -> expr instanceof Max || expr instanceof Min);
+        // LogicalAggregate only outputs one aggregate function, which is max 
or min
+        if (aggFuncs.size() != 1 || maxMinFunc.size() != 1) {
+            return null;
+        }
+        ExprId exprId = null;
+        Expression func = maxMinFunc.iterator().next();
+        for (NamedExpression expr : aggOutputExpressions) {
+            if (expr instanceof Alias && ((Alias) expr).child().equals(func)) {
+                Alias alias = (Alias) expr;
+                exprId = alias.getExprId();
+            }
+        }
+        // try to find min(a)<10 or max(a)>10
+        Expression originConjunct = findMatchingConjunct(conjuncts, func 
instanceof Max, exprId).orElse(null);
+        if (null == originConjunct) {
+            return null;
+        }
+        Set<Expression> newUpperConjuncts = new HashSet<>(conjuncts);
+        newUpperConjuncts.remove(originConjunct);
+        Expression newPredicate = null;
+        if (func instanceof Max) {
+            if (originConjunct instanceof GreaterThan) {
+                newPredicate = new GreaterThan(func.child(0), 
originConjunct.child(1));
+            } else if (originConjunct instanceof GreaterThanEqual) {
+                newPredicate = new GreaterThanEqual(func.child(0), 
originConjunct.child(1));
+            }
+        } else {
+            if (originConjunct instanceof LessThan) {
+                newPredicate = new LessThan(func.child(0), 
originConjunct.child(1));
+            } else if (originConjunct instanceof LessThanEqual) {
+                newPredicate = new LessThanEqual(func.child(0), 
originConjunct.child(1));
+            }
+        }
+        Preconditions.checkState(newPredicate != null);

Review Comment:
   add error msg



##########
fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/MaxMinFilterPushDownTest.java:
##########
@@ -0,0 +1,87 @@
+// 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.util.MemoPatternMatchSupported;
+import org.apache.doris.nereids.util.PlanChecker;
+import org.apache.doris.utframe.TestWithFeService;
+
+import org.junit.jupiter.api.Test;
+
+public class MaxMinFilterPushDownTest extends TestWithFeService implements 
MemoPatternMatchSupported {
+    @Override
+    protected void runBeforeAll() throws Exception {
+        createDatabase("test");
+        connectContext.setDatabase("test");
+        createTable("CREATE TABLE IF NOT EXISTS max_t(\n"
+                + "`id` int(32),\n"
+                + "`score` int(64) NULL,\n"
+                + "`name` varchar(64) NULL\n"
+                + ") properties('replication_num'='1');");
+        
connectContext.getSessionVariable().setDisableNereidsRules("PRUNE_EMPTY_PARTITION");
+    }
+
+    @Test
+    public void testMaxRewrite() {
+        String sql = "select id, max(score) from max_t group by id having 
max(score)>10";
+        PlanChecker.from(connectContext).analyze(sql).rewrite()
+                .matches(logicalFilter(logicalOlapScan()).when(filter -> 
filter.getConjuncts().size() == 1));
+    }
+
+    @Test
+    public void testMinRewrite() {
+        String sql = "select id, min(score) from max_t group by id having 
min(score)<10";
+        PlanChecker.from(connectContext).analyze(sql).rewrite()
+                .matches(logicalFilter(logicalOlapScan()).when(filter -> 
filter.getConjuncts().size() == 1));
+    }
+
+    @Test
+    public void testMaxNotRewrite0() {
+        String sql = "select id, max(score) from max_t group by id having 
max(score)<10";
+        PlanChecker.from(connectContext).analyze(sql).rewrite()
+                .nonMatch(logicalFilter(logicalOlapScan()));
+    }
+
+    @Test
+    public void testMinNotRewrite1() {
+        String sql = "select id, min(score) from max_t group by id having 
min(score)>10";
+        PlanChecker.from(connectContext).analyze(sql).rewrite()
+                .nonMatch(logicalFilter(logicalOlapScan()));
+    }
+
+    @Test
+    public void testMinNotRewrite2() {
+        String sql = "select id, min(score), max(score) from max_t group by id 
having min(score)>10";

Review Comment:
   test min(a), max(b) and min(a), min(b)?



##########
fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/MaxMinFilterPushDownTest.java:
##########
@@ -0,0 +1,87 @@
+// 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.util.MemoPatternMatchSupported;
+import org.apache.doris.nereids.util.PlanChecker;
+import org.apache.doris.utframe.TestWithFeService;
+
+import org.junit.jupiter.api.Test;
+
+public class MaxMinFilterPushDownTest extends TestWithFeService implements 
MemoPatternMatchSupported {
+    @Override
+    protected void runBeforeAll() throws Exception {
+        createDatabase("test");
+        connectContext.setDatabase("test");
+        createTable("CREATE TABLE IF NOT EXISTS max_t(\n"
+                + "`id` int(32),\n"
+                + "`score` int(64) NULL,\n"
+                + "`name` varchar(64) NULL\n"
+                + ") properties('replication_num'='1');");
+        
connectContext.getSessionVariable().setDisableNereidsRules("PRUNE_EMPTY_PARTITION");
+    }
+
+    @Test
+    public void testMaxRewrite() {
+        String sql = "select id, max(score) from max_t group by id having 
max(score)>10";
+        PlanChecker.from(connectContext).analyze(sql).rewrite()
+                .matches(logicalFilter(logicalOlapScan()).when(filter -> 
filter.getConjuncts().size() == 1));
+    }
+
+    @Test
+    public void testMinRewrite() {
+        String sql = "select id, min(score) from max_t group by id having 
min(score)<10";
+        PlanChecker.from(connectContext).analyze(sql).rewrite()
+                .matches(logicalFilter(logicalOlapScan()).when(filter -> 
filter.getConjuncts().size() == 1));
+    }
+
+    @Test
+    public void testMaxNotRewrite0() {

Review Comment:
   could u give it a more meaningful name?



-- 
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]

Reply via email to