Skip to content

Commit

Permalink
[enhancement](nereids)make error message more readable when bind logi…
Browse files Browse the repository at this point in the history
…calRepeat node (apache#26744)
  • Loading branch information
starocean999 authored Nov 13, 2023
1 parent db29850 commit a78e0f8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -74,6 +75,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;

Expand Down Expand Up @@ -386,6 +388,19 @@ protected boolean condition(Rule rule, Plan plan) {
.collect(ImmutableList.toImmutableList());
List<NamedExpression> newOutput = adjustNullableForRepeat(groupingSets, output);
groupingSets.forEach(list -> checkIfOutputAliasNameDuplicatedForGroupBy(list, newOutput));

// check all GroupingScalarFunction inputSlots must be from groupingExprs
Set<Slot> groupingExprs = groupingSets.stream()
.flatMap(Collection::stream).map(expr -> expr.getInputSlots())
.flatMap(Collection::stream).collect(Collectors.toSet());
Set<GroupingScalarFunction> 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);
})
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) """

Expand Down Expand Up @@ -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"""
Expand All @@ -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(
Expand Down Expand Up @@ -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."
}
}

0 comments on commit a78e0f8

Please sign in to comment.