Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java
Original file line number Diff line number Diff line change
Expand Up @@ -444,13 +444,16 @@ public void analyze(Analyzer analyzer) throws UserException {
}
}
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.");
}
}
}
Expand Down Expand Up @@ -1956,7 +1959,7 @@ public void collectTableRefs(List<TableRef> 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;
Expand All @@ -1966,6 +1969,16 @@ private boolean checkGroupingFn(Expr expr) {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}