This is an automated email from the ASF dual-hosted git repository.

airborne pushed a commit to branch branch-3.0
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-3.0 by this push:
     new e7cbcd2519f [Fix](count on index) fix count on index opt when count 
project expr #41772 (#42228)
e7cbcd2519f is described below

commit e7cbcd2519fec73c71fedf5d45338425f60bede6
Author: airborne12 <airborn...@gmail.com>
AuthorDate: Tue Oct 22 12:09:36 2024 +0800

    [Fix](count on index) fix count on index opt when count project expr #41772 
(#42228)
    
    cherry pick from #41772
---
 .../rules/implementation/AggregateStrategies.java  | 57 ++++++++++++++++------
 .../data/inverted_index_p0/test_count_on_index.out |  3 ++
 .../inverted_index_p0/test_count_on_index.groovy   | 24 +++++++++
 3 files changed, 69 insertions(+), 15 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/implementation/AggregateStrategies.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/implementation/AggregateStrategies.java
index dbddc2e14bb..0f6230a5fb4 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/implementation/AggregateStrategies.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/implementation/AggregateStrategies.java
@@ -541,26 +541,53 @@ public class AggregateStrategies implements 
ImplementationRuleFactory {
             LogicalFilter<? extends Plan> filter,
             LogicalOlapScan olapScan,
             CascadesContext cascadesContext) {
-        PhysicalOlapScan physicalOlapScan
-                = (PhysicalOlapScan) new LogicalOlapScanToPhysicalOlapScan()
+
+        PhysicalOlapScan physicalOlapScan = (PhysicalOlapScan) new 
LogicalOlapScanToPhysicalOlapScan()
                 .build()
                 .transform(olapScan, cascadesContext)
                 .get(0);
+
+        List<Expression> argumentsOfAggregateFunction = 
normalizeArguments(agg.getAggregateFunctions(), project);
+
+        if (!onlyContainsSlot(argumentsOfAggregateFunction)) {
+            return agg;
+        }
+
+        return agg.withChildren(ImmutableList.of(
+                project != null
+                        ? project.withChildren(ImmutableList.of(
+                        filter.withChildren(ImmutableList.of(
+                                new PhysicalStorageLayerAggregate(
+                                        physicalOlapScan, 
PushDownAggOp.COUNT_ON_MATCH)))))
+                        : filter.withChildren(ImmutableList.of(
+                                new PhysicalStorageLayerAggregate(
+                                        physicalOlapScan, 
PushDownAggOp.COUNT_ON_MATCH)))
+        ));
+    }
+
+    private List<Expression> normalizeArguments(Set<AggregateFunction> 
aggregateFunctions,
+            @Nullable LogicalProject<? extends Plan> project) {
+        List<Expression> arguments = aggregateFunctions.stream()
+                .flatMap(aggregateFunction -> 
aggregateFunction.getArguments().stream())
+                .collect(ImmutableList.toImmutableList());
+
         if (project != null) {
-            return agg.withChildren(ImmutableList.of(
-                    project.withChildren(ImmutableList.of(
-                            filter.withChildren(ImmutableList.of(
-                                    new PhysicalStorageLayerAggregate(
-                                            physicalOlapScan,
-                                            PushDownAggOp.COUNT_ON_MATCH)))))
-            ));
-        } else {
-            return agg.withChildren(ImmutableList.of(
-                            filter.withChildren(ImmutableList.of(
-                                    new PhysicalStorageLayerAggregate(
-                                            physicalOlapScan,
-                                            PushDownAggOp.COUNT_ON_MATCH)))));
+            arguments = Project.findProject(arguments, project.getProjects())
+                    .stream()
+                    .map(p -> p instanceof Alias ? p.child(0) : p)
+                    .collect(ImmutableList.toImmutableList());
         }
+
+        return arguments;
+    }
+
+    private boolean onlyContainsSlot(List<Expression> arguments) {
+        return arguments.stream().allMatch(argument -> {
+            if (argument instanceof SlotReference) {
+                return true;
+            }
+            return false;
+        });
     }
 
     //select /*+SET_VAR(enable_pushdown_minmax_on_unique=true) */min(user_id) 
from table_unique;
diff --git a/regression-test/data/inverted_index_p0/test_count_on_index.out 
b/regression-test/data/inverted_index_p0/test_count_on_index.out
index 3c0f47e7f8b..f74f3dc927a 100644
--- a/regression-test/data/inverted_index_p0/test_count_on_index.out
+++ b/regression-test/data/inverted_index_p0/test_count_on_index.out
@@ -77,3 +77,6 @@
 -- !sql_bad --
 0      1
 
+-- !sql_bad2 --
+0      1
+
diff --git 
a/regression-test/suites/inverted_index_p0/test_count_on_index.groovy 
b/regression-test/suites/inverted_index_p0/test_count_on_index.groovy
index 320fc65ff76..ec2c556d835 100644
--- a/regression-test/suites/inverted_index_p0/test_count_on_index.groovy
+++ b/regression-test/suites/inverted_index_p0/test_count_on_index.groovy
@@ -313,6 +313,30 @@ suite("test_count_on_index_httplogs", "p0") {
                 contains "pushAggOp=NONE"
         }
         qt_sql_bad "${bad_sql}"
+        def bad_sql2 = """
+        SELECT
+            COUNT(cond1) AS num1,
+            COUNT(cond2) AS num2
+        FROM (
+            SELECT
+                CASE
+                    WHEN c IN ('c1', 'c2', 'c3') AND d = 'd1' THEN b
+                END AS cond1,
+                CASE
+                    WHEN e = 'e1' AND c IN ('c1', 'c2', 'c3') THEN b
+                END AS cond2
+            FROM
+                ${tableName5}
+            WHERE
+                a = '2024-07-26'
+                AND e = 'e1'
+        ) AS project;
+        """
+        explain {
+            sql("${bad_sql2}")
+                contains "pushAggOp=NONE"
+        }
+        qt_sql_bad2 "${bad_sql2}"
     } finally {
         //try_sql("DROP TABLE IF EXISTS ${testTable}")
     }


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

Reply via email to