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

yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 9225dd16ca [fix](grouping sets) grouping sets cause be core or return 
wrong results (#12313)
9225dd16ca is described below

commit 9225dd16ca9cbad308dc11fd6bc85100cc620241
Author: ZenoYang <cookie...@qq.com>
AuthorDate: Thu Sep 8 14:55:50 2022 +0800

    [fix](grouping sets) grouping sets cause be core or return wrong results 
(#12313)
---
 .../java/org/apache/doris/analysis/SelectStmt.java | 25 ++++++++++++++++------
 .../grouping_sets/test_grouping_sets.groovy        | 16 ++++++++++++++
 2 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java
index f6230865fd..e29a68d375 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java
@@ -444,13 +444,16 @@ public class SelectStmt extends QueryStmt {
             }
         }
         if (groupByClause != null && groupByClause.isGroupByExtension()) {
+            ArrayList<Expr> aggFnExprList = new ArrayList<>();
             for (SelectListItem item : selectList.getItems()) {
-                if (item.getExpr() instanceof FunctionCallExpr && 
item.getExpr().fn instanceof AggregateFunction) {
+                aggFnExprList.clear();
+                getAggregateFnExpr(item.getExpr(), aggFnExprList);
+                for (Expr aggFnExpr : aggFnExprList) {
                     for (Expr expr : groupByClause.getGroupingExprs()) {
-                        if (item.getExpr().contains(expr)) {
-                            throw new AnalysisException("column: " + 
expr.toSql() + " cannot both in select list and "
-                                    + "aggregate functions when using GROUPING 
SETS/CUBE/ROLLUP, please use union"
-                                    + " instead.");
+                        if (aggFnExpr.contains(expr)) {
+                            throw new AnalysisException("column: " + 
expr.toSql() + " cannot both in select "
+                                    + "list and aggregate functions when using 
GROUPING SETS/CUBE/ROLLUP, "
+                                    + "please use union instead.");
                         }
                     }
                 }
@@ -1956,7 +1959,7 @@ public class SelectStmt extends QueryStmt {
     private boolean checkGroupingFn(Expr expr) {
         if (expr instanceof GroupingFunctionCallExpr) {
             return true;
-        } else if (expr.getChildren() != null && expr.getChildren().size() > 
0) {
+        } else if (expr.getChildren() != null) {
             for (Expr child : expr.getChildren()) {
                 if (checkGroupingFn(child)) {
                     return true;
@@ -1966,6 +1969,16 @@ public class SelectStmt extends QueryStmt {
         return false;
     }
 
+    private void getAggregateFnExpr(Expr expr, ArrayList<Expr> aggFnExprList) {
+        if (expr instanceof FunctionCallExpr && expr.fn instanceof 
AggregateFunction) {
+            aggFnExprList.add(expr);
+        } else if (expr.getChildren() != null) {
+            for (Expr child : expr.getChildren()) {
+                getAggregateFnExpr(child, aggFnExprList);
+            }
+        }
+    }
+
     @Override
     public int hashCode() {
         return id.hashCode();
diff --git 
a/regression-test/suites/query_p0/grouping_sets/test_grouping_sets.groovy 
b/regression-test/suites/query_p0/grouping_sets/test_grouping_sets.groovy
index b67ac59cea..3e70440782 100644
--- a/regression-test/suites/query_p0/grouping_sets/test_grouping_sets.groovy
+++ b/regression-test/suites/query_p0/grouping_sets/test_grouping_sets.groovy
@@ -42,4 +42,20 @@ suite("test_grouping_sets") {
                  select if(k0 = 1, 2, k0) k_if, k1, sum(k2) k2_sum from 
test_query_db.baseall where k0 is null or k2 = 1991
                  group by grouping sets((k_if, k1),()) order by k_if, k1, 
k2_sum
                """
+
+    test {
+        sql """
+              SELECT k1, k2, SUM(k3) FROM test_query_db.test
+              GROUP BY GROUPING SETS ((k1, k2), (k1), (k2), ( ), (k3) ) order 
by k1, k2
+            """
+        exception "errCode = 2, detailMessage = column: `k3` cannot both in 
select list and aggregate functions"
+    }
+
+    test {
+        sql """
+              SELECT k1, k2, SUM(k3)/(SUM(k3)+1) FROM test_query_db.test
+              GROUP BY GROUPING SETS ((k1, k2), (k1), (k2), ( ), (k3) ) order 
by k1, k2
+            """
+        exception "errCode = 2, detailMessage = column: `k3` cannot both in 
select list and aggregate functions"
+    }
 }


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

Reply via email to