Skip to content

Commit

Permalink
Multiple distinct aggregate functions that cannot be transformed into…
Browse files Browse the repository at this point in the history
… multi_distinct will result in an error.
  • Loading branch information
keanji-x committed Jul 7, 2023
1 parent 2d445bb commit 768764c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.doris.nereids.rules.rewrite.CheckAndStandardizeWindowFunctionAndFrame;
import org.apache.doris.nereids.rules.rewrite.CheckDataTypes;
import org.apache.doris.nereids.rules.rewrite.CheckMatchExpression;
import org.apache.doris.nereids.rules.rewrite.CheckMultiDistinct;
import org.apache.doris.nereids.rules.rewrite.CollectFilterAboveConsumer;
import org.apache.doris.nereids.rules.rewrite.CollectProjectAboveConsumer;
import org.apache.doris.nereids.rules.rewrite.ColumnPruning;
Expand Down Expand Up @@ -292,6 +293,7 @@ public class Rewriter extends AbstractBatchJobExecutor {
custom(RuleType.ADJUST_NULLABLE, AdjustNullable::new),
bottomUp(
new ExpressionRewrite(CheckLegalityAfterRewrite.INSTANCE),
new CheckMultiDistinct(),
new CheckAfterRewrite()
)),
topic("MATERIALIZED CTE", topDown(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.rules.rewrite;

import org.apache.doris.nereids.exceptions.AnalysisException;
import org.apache.doris.nereids.rules.Rule;
import org.apache.doris.nereids.rules.RuleType;
import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction;
import org.apache.doris.nereids.trees.expressions.functions.agg.Avg;
import org.apache.doris.nereids.trees.expressions.functions.agg.Count;
import org.apache.doris.nereids.trees.expressions.functions.agg.GroupConcat;
import org.apache.doris.nereids.trees.expressions.functions.agg.Sum;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;

import com.google.common.collect.ImmutableSet;

import java.util.List;
import java.util.stream.Collectors;

/**
* If there are multiple distinct aggregate functions that cannot
* be transformed into multi_distinct, an error is reported.
* The following functions can be transformed into multi_distinct:
* - count -> MULTI_DISTINCT_COUNT
* - sum -> MULTI_DISTINCT_SUM
* - avg -> MULTI_DISTINCT_AVG
* - group_concat -> MULTI_DISTINCT_GROUP_CONCAT
*/
public class CheckMultiDistinct extends OneRewriteRuleFactory {
private final ImmutableSet<Class<? extends AggregateFunction>> supportedFunctions =
ImmutableSet.of(Count.class, Sum.class, Avg.class, GroupConcat.class);

@Override
public Rule build() {
return logicalAggregate().then(agg -> checkDistinct(agg)).toRule(RuleType.CHECK_ANALYSIS);
}

private LogicalAggregate checkDistinct(LogicalAggregate<? extends Plan> aggregate) {
List<AggregateFunction> distinctFuncs = aggregate.getAggregateFunctions().stream()
.filter(AggregateFunction::isDistinct)
.collect(Collectors.toList());
if (aggregate.getDistinctArguments().size() > 1) {
for (AggregateFunction aggfunc : distinctFuncs) {
if (!supportedFunctions.contains(aggfunc.getClass())) {
throw new AnalysisException(aggfunc.toString() + " can't support multi distinct.");
}
}
}

return aggregate;
}
}
5 changes: 5 additions & 0 deletions regression-test/suites/nereids_syntax_p0/analyze_agg.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ suite("analyze_agg") {
tt2.d,
tt2.c;
"""

test {
sql "select count(distinct t2.id), max(distinct t2.c) from t2"
exception "max(DISTINCT c#2) can't support multi distinct."
}
}

0 comments on commit 768764c

Please sign in to comment.