feiniaofeiafei commented on code in PR #65245:
URL: https://github.com/apache/doris/pull/65245#discussion_r3534585142
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/CollectList.java:
##########
@@ -38,7 +38,7 @@
* AggregateFunction 'collect_list'. This class is generated by
GenerateFunction.
*/
public class CollectList extends NotNullableAggregateFunction
- implements UnaryExpression, ExplicitlyCastableSignature {
+ implements UnaryExpression, ExplicitlyCastableSignature,
SupportMultiDistinct {
Review Comment:
The capability check should take precedence over statistics and
`multi_distinct_strategy`. In
`DistinctAggStrategySelector.shouldUseMultiDistinct`, please include
unsupported distinct functions in `mustUseCte`, for example:
`containsCountDistinctMultiExpr(agg) ||
containsUnsupportedMultiDistinctFunction(agg)`. A forced or cost-selected
multi-distinct strategy must not choose a function that has no correct
multi-distinct implementation.
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/ArrayAgg.java:
##########
@@ -37,7 +37,7 @@
* AggregateFunction 'array_agg'.
*/
public class ArrayAgg extends NotNullableAggregateFunction
- implements UnaryExpression, ExplicitlyCastableSignature {
+ implements UnaryExpression, ExplicitlyCastableSignature,
SupportMultiDistinct {
Review Comment:
Instead of making every aggregate implement `SupportMultiDistinct`, could we
use the existing CTE split as the fallback for functions without a
multi-distinct implementation? Please consider extracting a shared predicate
such as `AggregateUtils.containsUnsupportedMultiDistinctFunction(agg)`, based
on distinct aggregate functions that do not implement `SupportMultiDistinct`.
This avoids adding a new FE/BE function solely to make this query shape
executable and preserves the existing `array_agg` NULL semantics.
##########
fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/CheckMultiDistinctTest.java:
##########
@@ -0,0 +1,133 @@
+// 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.CascadesContext;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.memo.Group;
+import org.apache.doris.nereids.memo.GroupExpression;
+import org.apache.doris.nereids.memo.GroupId;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator;
+import org.apache.doris.nereids.trees.expressions.functions.agg.ArrayAgg;
+import org.apache.doris.nereids.trees.expressions.functions.agg.Avg;
+import org.apache.doris.nereids.trees.expressions.functions.agg.CollectList;
+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.expressions.literal.IntegerLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.StringLiteral;
+import org.apache.doris.nereids.trees.plans.GroupPlan;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
+import org.apache.doris.nereids.util.PlanConstructor;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import java.util.List;
+import java.util.Optional;
+
+public class CheckMultiDistinctTest {
+ private final CascadesContext cascadesContext =
Mockito.mock(CascadesContext.class);
+
+ private final LogicalOlapScan scan = new LogicalOlapScan(
+ StatementScopeIdGenerator.newRelationId(),
PlanConstructor.student, ImmutableList.of(""));
+ private final Slot id = scan.getOutput().get(0);
+ private final Slot name = scan.getOutput().get(2);
+ private final Slot age = scan.getOutput().get(3);
+ private final GroupExpression scanGroupExpr = new GroupExpression(scan,
ImmutableList.of());
+ private final GroupPlan childGroup = new GroupPlan(
+ new Group(GroupId.createGenerator().getNextId(),
scanGroupExpr.getPlan().getLogicalProperties()));
+
+ private List<Plan> applyCheckMultiDistinct(Plan agg) {
+ return new CheckMultiDistinct().build().transform(agg,
cascadesContext);
+ }
+
+ private Plan buildAgg(List<NamedExpression> outputs) {
+ return new LogicalAggregate<>(Lists.newArrayList(), outputs, true,
Optional.empty(), childGroup);
+ }
+
+ @Test
+ public void testSupportedFunctionsAllowMultiDistinct() {
+ List<NamedExpression> outputs = Lists.newArrayList(
+ new Alias(new Count(true, id), "count_distinct_id"),
+ new Alias(new Sum(true, age), "sum_distinct_age"));
+ Assertions.assertDoesNotThrow(() ->
applyCheckMultiDistinct(buildAgg(outputs)));
+ }
+
+ @Test
+ public void testCollectListMultiDistinctSupported() {
+ List<NamedExpression> outputs = Lists.newArrayList(
+ new Alias(new CollectList(true, name),
"collect_distinct_name"),
+ new Alias(new CollectList(true, age), "collect_distinct_age"));
+ Assertions.assertDoesNotThrow(() ->
applyCheckMultiDistinct(buildAgg(outputs)));
Review Comment:
Changing the selector alone is insufficient because `CheckMultiDistinct`
currently rejects unsupported functions before `DistinctAggStrategySelector`
runs. Please relax or restructure that check for query shapes that
`SplitMultiDistinctStrategy` can decompose, so the CTE fallback is actually
reachable. The shared capability predicate should be reused by both places to
avoid divergent definitions.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]