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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.doris.nereids.CascadesContext;
import org.apache.doris.nereids.StatementContext;
import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.jobs.JobContext;
import org.apache.doris.nereids.rules.rewrite.DistinctAggStrategySelector.DistinctSelectorContext;
import org.apache.doris.nereids.rules.rewrite.StatsDerive.DeriveContext;
Expand Down Expand Up @@ -117,9 +118,27 @@ public Plan visitLogicalAggregate(LogicalAggregate<? extends Plan> agg, Distinct
}

private boolean shouldUseMultiDistinct(LogicalAggregate<? extends Plan> agg) {
if (AggregateUtils.containsCountDistinctMultiExpr(agg)) {
boolean mustUseCte = AggregateUtils.containsCountDistinctMultiExpr(agg);
boolean mustUseMulti = agg.getSourceRepeat().isPresent();
if (mustUseCte && mustUseMulti) {
throw new AnalysisException(
"Unsupported query: GROUPING SETS/ROLLUP/CUBE cannot be used with a combination of "
+ "multi-column COUNT(DISTINCT) and other COUNT(DISTINCT) expressions.\n\n"
+ "Unsupported scenarios:\n"
+ "• COUNT(DISTINCT a, b) with COUNT(DISTINCT a) + GROUPING\n"
+ "• COUNT(DISTINCT a, b) with COUNT(DISTINCT a, c) + GROUPING\n\n"
+ "Supported scenarios:\n"
+ "• Single COUNT(DISTINCT a, b) + GROUPING\n"
+ "• Multiple COUNT(DISTINCT single_column) + "
+ "GROUPING (e.g., COUNT(DISTINCT a), COUNT(DISTINCT b))");
}
if (mustUseCte) {
return false;
}
// TODO with source repeat aggregate need to be supported cte split in future
if (mustUseMulti) {
return true;
}
ConnectContext ctx = ConnectContext.get();
if (ctx == null) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,6 @@ private static boolean isDistinctMultiColumns(AggregateFunction func) {

private static void collectDistinctAndNonDistinctFunctions(LogicalAggregate<? extends Plan> agg,
List<List<Alias>> aliases, List<Alias> otherAggFuncs) {
// TODO with source repeat aggregate need to be supported in future
// 这个可能也没有关系,可以先注释掉,之后加一下关于grouping的测试
// if (agg.getSourceRepeat().isPresent()) {
// return false;
// }
// boolean distinctMultiColumns = false;
Map<Set<Expression>, List<Alias>> distinctArgToAliasList = new LinkedHashMap<>();
for (NamedExpression namedExpression : agg.getOutputExpressions()) {
if (!(namedExpression instanceof Alias) || !(namedExpression.child(0) instanceof AggregateFunction)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,16 @@ suite("distinct_agg_strategy_selector") {
select count(distinct a_1) , count(distinct b_5) from t1000;"""
qt_no_stats_should_use_multi_distinct """explain shape plan
select count(distinct d_20) , count(distinct b_5) from t1000 group by a_1;"""

test {
sql "select count(distinct d_20,b_5) , count(distinct b_5) from t1000 group by grouping sets ((d_20, b_5),())"
exception "Unsupported query"
}
// multi_distinct_strategy = 2 means use cte, but it will be ignored because agg with source repeat should not use cte split
sql "set multi_distinct_strategy=2"
explain {
sql "logical plan select count(distinct d_20) , count(distinct b_5) from t1000 group by grouping sets ((d_20, b_5),())"
contains "multi_distinct_count"
}
sql "set multi_distinct_strategy=0 "
}
Loading