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 f6230865fd441f..e29a68d37574e7 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 void analyze(Analyzer analyzer) throws UserException { } } if (groupByClause != null && groupByClause.isGroupByExtension()) { + ArrayList 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 void collectTableRefs(List tblRefs) { 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 @@ private boolean checkGroupingFn(Expr expr) { return false; } + private void getAggregateFnExpr(Expr expr, ArrayList 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 b67ac59ceaddd4..3e704407823bd9 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" + } }