diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java index ab0d42962ace409..e9c5c54eb39c618 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java @@ -48,6 +48,7 @@ import org.apache.doris.nereids.trees.expressions.functions.FunctionBuilder; import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction; import org.apache.doris.nereids.trees.expressions.functions.generator.TableGeneratingFunction; +import org.apache.doris.nereids.trees.expressions.functions.scalar.GroupingScalarFunction; import org.apache.doris.nereids.trees.expressions.functions.scalar.Lambda; import org.apache.doris.nereids.trees.expressions.functions.table.TableValuedFunction; import org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionRewriter; @@ -76,6 +77,7 @@ import org.apache.doris.nereids.trees.plans.logical.LogicalTVFRelation; import org.apache.doris.nereids.trees.plans.logical.UsingJoin; import org.apache.doris.nereids.trees.plans.visitor.InferPlanOutputAlias; +import org.apache.doris.nereids.util.ExpressionUtils; import org.apache.doris.nereids.util.TypeCoercionUtils; import org.apache.doris.qe.ConnectContext; @@ -388,6 +390,19 @@ protected boolean condition(Rule rule, Plan plan) { .collect(ImmutableList.toImmutableList()); List newOutput = adjustNullableForRepeat(groupingSets, output); groupingSets.forEach(list -> checkIfOutputAliasNameDuplicatedForGroupBy(list, newOutput)); + + // check all GroupingScalarFunction inputSlots must be from groupingExprs + Set groupingExprs = groupingSets.stream() + .flatMap(Collection::stream).map(expr -> expr.getInputSlots()) + .flatMap(Collection::stream).collect(Collectors.toSet()); + Set groupingScalarFunctions = ExpressionUtils + .collect(newOutput, GroupingScalarFunction.class::isInstance); + for (GroupingScalarFunction function : groupingScalarFunctions) { + if (!groupingExprs.containsAll(function.getInputSlots())) { + throw new AnalysisException("Column in " + function.getName() + + " does not exist in GROUP BY clause."); + } + } return repeat.withGroupSetsAndOutput(groupingSets, newOutput); }) ), 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 e27b067f9abf8cf..6564bca3509f94d 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 @@ -44,21 +44,44 @@ suite("test_grouping_sets", "p0") { group by grouping sets((k_if, k1),()) order by k_if, k1, k2_sum """ + test { + sql """ + SELECT /*+ SET_VAR(enable_nereids_planner=false) */ 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" + } + + sql """set enable_nereids_planner=true;""" + sql """set enable_fallback_to_original_planner=false;""" 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" + } + sql """set enable_nereids_planner=false;""" + sql """set enable_fallback_to_original_planner=true;""" + test { + sql """ + SELECT /*+ SET_VAR(enable_nereids_planner=false) */ 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" } + sql """set enable_nereids_planner=true;""" + sql """set enable_fallback_to_original_planner=false;""" 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" + exception "errCode = 2, detailMessage = column: k3 cannot both in select list and aggregate functions" } + sql """set enable_nereids_planner=false;""" + sql """set enable_fallback_to_original_planner=true;""" qt_select7 """ select k1,k2,sum(k3) from test_query_db.test where 1 = 2 group by grouping sets((k1), (k1,k2)) """ @@ -164,9 +187,18 @@ suite("test_grouping_sets", "p0") { qt_select19 """SELECT k1 ,GROUPING(k1) FROM test_query_db.test GROUP BY ROLLUP (k1) ORDER BY k1""" qt_select20 """SELECT k1 ,GROUPING(k1) FROM test_query_db.test GROUP BY CUBE (k1) ORDER BY k1""" test { - sql "SELECT k1 ,GROUPING(k2) FROM test_query_db.test GROUP BY CUBE (k1) ORDER BY k1" + sql "SELECT /*+ SET_VAR(enable_nereids_planner=false) */ k1 ,GROUPING(k2) FROM test_query_db.test GROUP BY CUBE (k1) ORDER BY k1" exception "Column `k2` in GROUP_ID() does not exist in GROUP BY clause" } + sql """set enable_nereids_planner=true;""" + sql """set enable_fallback_to_original_planner=false;""" + test { + sql "SELECT k1 ,GROUPING(k2) FROM test_query_db.test GROUP BY CUBE (k1) ORDER BY k1" + exception "Column in Grouping does not exist in GROUP BY clause" + } + sql """set enable_nereids_planner=false;""" + sql """set enable_fallback_to_original_planner=true;""" + // test grouping sets id contain null data sql """drop table if exists test_query_db.test_grouping_sets_id_null""" sql """create table if not exists test_query_db.test_grouping_sets_id_null like test_query_db.test""" @@ -192,8 +224,6 @@ suite("test_grouping_sets", "p0") { """ sql """drop table if exists test_query_db.test_grouping_sets_id_null""" // test grouping sets shoot rollup - // because nereids cannot support rollup correctly forbid it temporary - sql """set enable_nereids_planner=false""" sql "drop table if exists test_query_db.test_grouping_sets_rollup" sql """ create table if not exists test_query_db.test_grouping_sets_rollup( @@ -232,8 +262,16 @@ suite("test_grouping_sets", "p0") { sql "drop table if exists test_query_db.test_grouping_sets_rollup" // test_grouping_select test { - sql "select k1, if(grouping(k1)=1, count(k1), 0) from test_query_db.test group by grouping sets((k1))" + sql "select /*+ SET_VAR(enable_nereids_planner=false) */ k1, if(grouping(k1)=1, count(k1), 0) from test_query_db.test group by grouping sets((k1))" exception "`k1` cannot both in select list and aggregate functions " + "when using GROUPING SETS/CUBE/ROLLUP, please use union instead." } + sql """set enable_nereids_planner=true;""" + sql """set enable_fallback_to_original_planner=false;""" + + test { + sql "select k1, if(grouping(k1)=1, count(k1), 0) from test_query_db.test group by grouping sets((k1))" + exception "k1 cannot both in select list and aggregate functions " + + "when using GROUPING SETS/CUBE/ROLLUP, please use union instead." + } }