-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multiple distinct aggregate functions that cannot be transformed into…
… multi_distinct will result in an error.
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/CheckMultiDistinct.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters