[fix](grouping sets) grouping sets cause be core or return wrong results (#12313)

This commit is contained in:
ZenoYang
2022-09-08 14:55:50 +08:00
committed by GitHub
parent c3af60eff8
commit 9225dd16ca
2 changed files with 35 additions and 6 deletions

View File

@ -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();

View File

@ -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"
}
}