This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-4.2
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-4.2 by this push:
     new 9558bfc9aa5 branch-4.2: [improvement](nereids) Support CTE split for 
DISTINCT aggregate functions #65664 (#69838)
9558bfc9aa5 is described below

commit 9558bfc9aa537eb7452d3d20111f3ccfc16bf091
Author: feiniaofeiafei <[email protected]>
AuthorDate: Fri Oct 30 14:19:30 2026 +0800

    branch-4.2: [improvement](nereids) Support CTE split for DISTINCT aggregate 
functions #65664 (#69838)
    
    picked from #65664
---
 .../nereids/rules/analysis/NormalizeAggregate.java |   5 +-
 .../nereids/rules/analysis/NormalizeRepeat.java    |   3 +
 .../rules/rewrite/DistinctAggStrategySelector.java |  13 +-
 .../rules/rewrite/SplitMultiDistinctStrategy.java  |   6 +-
 .../expressions/functions/agg/CollectList.java     |  15 +
 .../functions/agg/ExponentialMovingAverage.java    |   6 +
 .../expressions/functions/agg/GroupBitmapXor.java  |  11 +-
 .../trees/expressions/functions/agg/Histogram.java |   5 +
 .../trees/expressions/functions/agg/MapAgg.java    |   2 +-
 .../trees/expressions/functions/agg/MapAggV2.java  |   2 +-
 .../expressions/functions/agg/Percentile.java      |   5 +
 .../functions/agg/PercentileApprox.java            |   5 +
 .../functions/agg/PercentileApproxWeighted.java    |   5 +
 .../expressions/functions/agg/PercentileArray.java |   5 +
 .../functions/agg/PercentileReservoir.java         |   5 +
 .../trees/expressions/functions/agg/TopN.java      |   5 +
 .../trees/expressions/functions/agg/TopNArray.java |   5 +
 .../expressions/functions/agg/TopNWeighted.java    |   5 +
 .../apache/doris/nereids/util/AggregateUtils.java  |  11 +
 .../uuid/test_uuid_aggregate_matrix.out            | 159 ++-------
 .../agg_function/agg_distinct_function.out         | 393 +++++++++++++++++++++
 .../uuid/test_uuid_aggregate_matrix.groovy         |   7 +-
 .../agg_function/agg_distinct_function.groovy      | 212 +++++++++++
 .../distinct_agg_strategy_selector.groovy          |  39 ++
 24 files changed, 784 insertions(+), 145 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/NormalizeAggregate.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/NormalizeAggregate.java
index 5c2b1b602c0..8e2a65b1811 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/NormalizeAggregate.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/NormalizeAggregate.java
@@ -41,7 +41,6 @@ import 
org.apache.doris.nereids.trees.expressions.WindowExpression;
 import 
org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction;
 import org.apache.doris.nereids.trees.expressions.functions.agg.AnyValue;
 import org.apache.doris.nereids.trees.expressions.functions.generator.Unnest;
-import org.apache.doris.nereids.trees.expressions.literal.Literal;
 import org.apache.doris.nereids.trees.expressions.literal.TinyIntLiteral;
 import org.apache.doris.nereids.trees.plans.Plan;
 import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
@@ -182,7 +181,7 @@ public class NormalizeAggregate implements 
RewriteRuleFactory, NormalizeToSlot {
                 for (Expression arg : aggFunc.children()) {
                     // should not push down literal under aggregate
                     // e.g. group_concat(distinct xxx, ','), the ',' literal 
show stay in aggregate
-                    if (arg instanceof Literal) {
+                    if (arg.isConstant()) {
                         continue;
                     }
                     if (arg.containsType(SubqueryExpr.class, 
WindowExpression.class, Unnest.class,
@@ -200,7 +199,7 @@ public class NormalizeAggregate implements 
RewriteRuleFactory, NormalizeToSlot {
                 for (Expression arg : aggFunc.children()) {
                     // should not push down literal under aggregate
                     // e.g. group_concat(distinct xxx, ','), the ',' literal 
show stay in aggregate
-                    if (arg instanceof Literal) {
+                    if (arg.isConstant()) {
                         continue;
                     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/NormalizeRepeat.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/NormalizeRepeat.java
index 78bdac77b58..b8381655d81 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/NormalizeRepeat.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/NormalizeRepeat.java
@@ -241,6 +241,9 @@ public class NormalizeRepeat extends OneAnalysisRuleFactory 
{
         ImmutableSet.Builder<Expression> argumentsOfAggregateFunctionBuilder = 
ImmutableSet.builder();
         for (AggregateFunction function : aggregateFunctions) {
             for (Expression arg : function.getArguments()) {
+                if (arg.isConstant()) {
+                    continue;
+                }
                 if (arg instanceof OrderExpression) {
                     argumentsOfAggregateFunctionBuilder.add(arg.child(0));
                 } else {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/DistinctAggStrategySelector.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/DistinctAggStrategySelector.java
index 817460a92fe..1ff8efc0341 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/DistinctAggStrategySelector.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/DistinctAggStrategySelector.java
@@ -119,19 +119,20 @@ public class DistinctAggStrategySelector extends 
DefaultPlanRewriter<DistinctSel
     }
 
     private boolean shouldUseMultiDistinct(LogicalAggregate<? extends Plan> 
agg) {
-        boolean mustUseCte = 
AggregateUtils.containsCountDistinctMultiExpr(agg);
+        boolean mustUseCte = 
AggregateUtils.containsNotSupportMultiDistinctFunction(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 query: GROUPING SETS/ROLLUP/CUBE cannot be 
used with multiple DISTINCT "
+                    + "argument groups when any DISTINCT aggregate does not 
support multi-distinct execution.\n\n"
                     + "Unsupported scenarios:\n"
+                    + "• ARRAY_AGG(DISTINCT a) with ARRAY_AGG(DISTINCT b) + 
GROUPING\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))");
+                    + "• A single DISTINCT argument group with GROUPING\n"
+                    + "• Multiple DISTINCT argument groups when all DISTINCT 
aggregates support "
+                    + "multi-distinct execution (e.g., COUNT(DISTINCT a), 
COUNT(DISTINCT b))");
         }
         if (mustUseCte) {
             return false;
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/SplitMultiDistinctStrategy.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/SplitMultiDistinctStrategy.java
index 20ad018fc15..4ae27a88300 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/SplitMultiDistinctStrategy.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/SplitMultiDistinctStrategy.java
@@ -43,6 +43,7 @@ import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 import java.util.stream.Collectors;
 
@@ -166,10 +167,11 @@ public class SplitMultiDistinctStrategy {
             List<List<Alias>> aliases, List<Alias> otherAggFuncs) {
         Map<Set<Expression>, List<Alias>> distinctArgToAliasList = new 
LinkedHashMap<>();
         for (NamedExpression namedExpression : agg.getOutputExpressions()) {
-            if (!(namedExpression instanceof Alias) || 
!(namedExpression.child(0) instanceof AggregateFunction)) {
+            if (!(namedExpression instanceof Alias) || 
!namedExpression.containsType(AggregateFunction.class)) {
                 continue;
             }
-            AggregateFunction aggFunc = (AggregateFunction) 
namedExpression.child(0);
+            Optional<Expression> op = namedExpression.collectFirst(e -> e 
instanceof AggregateFunction);
+            AggregateFunction aggFunc = (AggregateFunction) op.get();
             if (aggFunc.isDistinct()) {
                 
distinctArgToAliasList.computeIfAbsent(ImmutableSet.copyOf(aggFunc.getDistinctArguments()),
                         k -> new ArrayList<>()).add((Alias) namedExpression);
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/CollectList.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/CollectList.java
index 978ecb13236..6d2f715230f 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/CollectList.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/CollectList.java
@@ -18,6 +18,7 @@
 package org.apache.doris.nereids.trees.expressions.functions.agg;
 
 import org.apache.doris.catalog.FunctionSignature;
+import org.apache.doris.nereids.exceptions.AnalysisException;
 import org.apache.doris.nereids.trees.expressions.Expression;
 import 
org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
 import org.apache.doris.nereids.trees.expressions.literal.ArrayLiteral;
@@ -102,4 +103,18 @@ public class CollectList extends 
NotNullableAggregateFunction
     public Expression resultForEmptyInput() {
         return new ArrayLiteral(new ArrayList<>(), this.getDataType());
     }
+
+    @Override
+    public List<Expression> getDistinctArguments() {
+        return distinct ? ImmutableList.of(getArgument(0)) : 
ImmutableList.of();
+    }
+
+    @Override
+    public void checkLegalityBeforeTypeCoercion() {
+        if (arity() == 2 && !getArgument(1).isConstant()) {
+            throw new AnalysisException(
+                    "collect_list requires second parameter must be a 
constant: "
+                            + this.toSql());
+        }
+    }
 }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/ExponentialMovingAverage.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/ExponentialMovingAverage.java
index ba870c89a0c..03966416c36 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/ExponentialMovingAverage.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/ExponentialMovingAverage.java
@@ -119,4 +119,10 @@ public class ExponentialMovingAverage extends 
NullableAggregateFunction
     public List<FunctionSignature> getSignatures() {
         return SIGNATURES;
     }
+
+    @Override
+    public List<Expression> getDistinctArguments() {
+        return distinct ? ImmutableList.of(getArgument(1), getArgument(2)) : 
ImmutableList.of();
+    }
+
 }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/GroupBitmapXor.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/GroupBitmapXor.java
index 9cf9310df8c..bffa3a54c85 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/GroupBitmapXor.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/GroupBitmapXor.java
@@ -18,6 +18,7 @@
 package org.apache.doris.nereids.trees.expressions.functions.agg;
 
 import org.apache.doris.catalog.FunctionSignature;
+import org.apache.doris.nereids.exceptions.AnalysisException;
 import org.apache.doris.nereids.trees.expressions.Expression;
 import 
org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
 import org.apache.doris.nereids.trees.expressions.shape.UnaryExpression;
@@ -55,8 +56,7 @@ public class GroupBitmapXor extends NullableAggregateFunction
     }
 
     private GroupBitmapXor(boolean distinct, boolean alwaysNullable, 
Expression arg0) {
-        /* distinct is meaningless to bitmap */
-        super("group_bitmap_xor", false, alwaysNullable, arg0);
+        super("group_bitmap_xor", distinct, alwaysNullable, arg0);
     }
 
     /** constructor for withChildren and reuse signature */
@@ -92,4 +92,11 @@ public class GroupBitmapXor extends NullableAggregateFunction
     public List<FunctionSignature> getSignatures() {
         return SIGNATURES;
     }
+
+    @Override
+    public void checkLegalityBeforeTypeCoercion() {
+        if (distinct) {
+            throw new AnalysisException("group_bitmap_xor does not support 
DISTINCT");
+        }
+    }
 }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Histogram.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Histogram.java
index e3b7722459e..d803552098f 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Histogram.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Histogram.java
@@ -113,4 +113,9 @@ public class Histogram extends NotNullableAggregateFunction
     public Expression resultForEmptyInput() {
         return new VarcharLiteral("{\"num_buckets\":0,\"buckets\":[]}");
     }
+
+    @Override
+    public List<Expression> getDistinctArguments() {
+        return distinct ? ImmutableList.of(getArgument(0)) : 
ImmutableList.of();
+    }
 }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/MapAgg.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/MapAgg.java
index 8f8652395f7..393adea6d0e 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/MapAgg.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/MapAgg.java
@@ -53,7 +53,7 @@ public class MapAgg extends NotNullableAggregateFunction
     /**
      * constructor with 2 arguments.
      */
-    private MapAgg(boolean distinct, Expression arg0, Expression arg1) {
+    public MapAgg(boolean distinct, Expression arg0, Expression arg1) {
         super("map_agg_v1", distinct, arg0, arg1);
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/MapAggV2.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/MapAggV2.java
index 652f21f97ba..699f62c3684 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/MapAggV2.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/MapAggV2.java
@@ -53,7 +53,7 @@ public class MapAggV2 extends NotNullableAggregateFunction
     /**
      * constructor with 2 arguments.
      */
-    private MapAggV2(boolean distinct, Expression arg0, Expression arg1) {
+    public MapAggV2(boolean distinct, Expression arg0, Expression arg1) {
         super("map_agg_v2", distinct, arg0, arg1);
     }
 
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Percentile.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Percentile.java
index a134414d361..f1b7fc4d93c 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Percentile.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/Percentile.java
@@ -107,4 +107,9 @@ public class Percentile extends NullableAggregateFunction
     public List<FunctionSignature> getSignatures() {
         return SIGNATURES;
     }
+
+    @Override
+    public List<Expression> getDistinctArguments() {
+        return distinct ? ImmutableList.of(getArgument(0)) : 
ImmutableList.of();
+    }
 }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/PercentileApprox.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/PercentileApprox.java
index cd4c2347e50..a2234464f11 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/PercentileApprox.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/PercentileApprox.java
@@ -121,4 +121,9 @@ public class PercentileApprox extends 
NullableAggregateFunction
     public List<FunctionSignature> getSignatures() {
         return SIGNATURES;
     }
+
+    @Override
+    public List<Expression> getDistinctArguments() {
+        return distinct ? ImmutableList.of(getArgument(0)) : 
ImmutableList.of();
+    }
 }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/PercentileApproxWeighted.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/PercentileApproxWeighted.java
index 5d3154c908c..b0a58e84c96 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/PercentileApproxWeighted.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/PercentileApproxWeighted.java
@@ -125,4 +125,9 @@ public class PercentileApproxWeighted extends 
NullableAggregateFunction
     public List<FunctionSignature> getSignatures() {
         return SIGNATURES;
     }
+
+    @Override
+    public List<Expression> getDistinctArguments() {
+        return distinct ? ImmutableList.of(getArgument(0), getArgument(1)) : 
ImmutableList.of();
+    }
 }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/PercentileArray.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/PercentileArray.java
index 77e48767f10..df892547db0 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/PercentileArray.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/PercentileArray.java
@@ -111,4 +111,9 @@ public class PercentileArray extends 
NotNullableAggregateFunction
     public Expression resultForEmptyInput() {
         return new ArrayLiteral(new ArrayList<>(), this.getDataType());
     }
+
+    @Override
+    public List<Expression> getDistinctArguments() {
+        return distinct ? ImmutableList.of(getArgument(0)) : 
ImmutableList.of();
+    }
 }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/PercentileReservoir.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/PercentileReservoir.java
index 38978330ad7..3e67f57b605 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/PercentileReservoir.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/PercentileReservoir.java
@@ -107,4 +107,9 @@ public class PercentileReservoir extends 
NullableAggregateFunction
     public List<FunctionSignature> getSignatures() {
         return SIGNATURES;
     }
+
+    @Override
+    public List<Expression> getDistinctArguments() {
+        return distinct ? ImmutableList.of(getArgument(0)) : 
ImmutableList.of();
+    }
 }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/TopN.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/TopN.java
index cb5d94832ed..15b52e8f340 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/TopN.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/TopN.java
@@ -134,4 +134,9 @@ public class TopN extends NullableAggregateFunction
     public List<FunctionSignature> getSignatures() {
         return SIGNATURES;
     }
+
+    @Override
+    public List<Expression> getDistinctArguments() {
+        return distinct ? ImmutableList.of(getArgument(0)) : 
ImmutableList.of();
+    }
 }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/TopNArray.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/TopNArray.java
index bc0dae22a76..616c9775412 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/TopNArray.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/TopNArray.java
@@ -137,4 +137,9 @@ public class TopNArray extends NullableAggregateFunction
     public List<FunctionSignature> getSignatures() {
         return SIGNATURES;
     }
+
+    @Override
+    public List<Expression> getDistinctArguments() {
+        return distinct ? ImmutableList.of(getArgument(0)) : 
ImmutableList.of();
+    }
 }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/TopNWeighted.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/TopNWeighted.java
index 0f9b9575081..6bd5497c3e2 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/TopNWeighted.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/TopNWeighted.java
@@ -227,4 +227,9 @@ public class TopNWeighted extends NullableAggregateFunction
                             + this.toSql());
         }
     }
+
+    @Override
+    public List<Expression> getDistinctArguments() {
+        return distinct ? ImmutableList.of(getArgument(0), getArgument(1)) : 
ImmutableList.of();
+    }
 }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/AggregateUtils.java 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/AggregateUtils.java
index 506d1ae342a..6e5e0068408 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/AggregateUtils.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/AggregateUtils.java
@@ -135,6 +135,17 @@ public class AggregateUtils {
                 expr instanceof Count && ((Count) expr).isDistinct() && 
expr.arity() > 1);
     }
 
+    /** e.g. Aggregation with avg(distinct a)(not support multiDistinct) or 
count(distinct a,b) will return true*/
+    public static boolean 
containsNotSupportMultiDistinctFunction(LogicalAggregate<? extends Plan> 
aggregate) {
+        return ExpressionUtils.deapAnyMatch(aggregate.getOutputExpressions(), 
expr -> {
+            if (expr instanceof AggregateFunction && ((AggregateFunction) 
expr).isDistinct()) {
+                return !(expr instanceof SupportMultiDistinct)
+                        || expr instanceof Count && ((Count) 
expr).isDistinct() && expr.arity() > 1;
+            }
+            return false;
+        });
+    }
+
     /** count agg function distinct group, up to 2*/
     public static int distinctArgumentGroupCountUpToTwo(Aggregate<? extends 
Plan> aggregate) {
         Set<Expression> distinctArgumentGroup = null;
diff --git 
a/regression-test/data/datatype_p0/uuid/test_uuid_aggregate_matrix.out 
b/regression-test/data/datatype_p0/uuid/test_uuid_aggregate_matrix.out
index f50479907aa..8a88bcc3123 100644
--- a/regression-test/data/datatype_p0/uuid/test_uuid_aggregate_matrix.out
+++ b/regression-test/data/datatype_p0/uuid/test_uuid_aggregate_matrix.out
@@ -1619,201 +1619,102 @@ ffffffff-ffff-ffff-ffff-ffffffffffff  
00000000-0000-0000-0000-000000000000    9
 -- !state_union --
 \N     \N      0
 
--- !state_combine --
-\N     \N      0
-
 -- !state_union --
 00000000-0000-0000-0000-000000000000   00000000-0000-0000-0000-000000000000    
10
 
--- !state_combine --
-00000000-0000-0000-0000-000000000000   00000000-0000-0000-0000-000000000000    
10
-
 -- !state_union --
 00000000-0000-0000-0000-000000000001   00000000-0000-0000-0000-000000000001    
10
 
--- !state_combine --
-00000000-0000-0000-0000-000000000001   00000000-0000-0000-0000-000000000001    
10
-
 -- !state_union --
 550e8400-e29b-41d4-a716-446655440000   550e8400-e29b-41d4-a716-446655440000    
10
 
--- !state_combine --
-550e8400-e29b-41d4-a716-446655440000   550e8400-e29b-41d4-a716-446655440000    
10
-
 -- !state_union --
 018f0f59-1010-7abc-9234-001122334455   018f0f59-1010-7abc-9234-001122334455    
10
 
--- !state_combine --
-018f0f59-1010-7abc-9234-001122334455   018f0f59-1010-7abc-9234-001122334455    
10
-
 -- !state_union --
 7fffffff-ffff-ffff-ffff-ffffffffffff   7fffffff-ffff-ffff-ffff-ffffffffffff    
10
 
--- !state_combine --
-7fffffff-ffff-ffff-ffff-ffffffffffff   7fffffff-ffff-ffff-ffff-ffffffffffff    
10
-
 -- !state_union --
 80000000-0000-0000-0000-000000000000   80000000-0000-0000-0000-000000000000    
10
 
--- !state_combine --
-80000000-0000-0000-0000-000000000000   80000000-0000-0000-0000-000000000000    
10
-
 -- !state_union --
 ffffffff-ffff-ffff-ffff-ffffffffffff   ffffffff-ffff-ffff-ffff-ffffffffffff    
10
 
--- !state_combine --
-ffffffff-ffff-ffff-ffff-ffffffffffff   ffffffff-ffff-ffff-ffff-ffffffffffff    
10
-
 -- !state_union --
 00000000-0000-0000-ffff-ffffffffffff   00000000-0000-0000-ffff-ffffffffffff    
10
 
--- !state_combine --
-00000000-0000-0000-ffff-ffffffffffff   00000000-0000-0000-ffff-ffffffffffff    
10
-
 -- !state_union --
 00000000-0000-0001-0000-000000000000   00000000-0000-0001-0000-000000000000    
10
 
--- !state_combine --
-00000000-0000-0001-0000-000000000000   00000000-0000-0001-0000-000000000000    
10
-
 -- !state_union --
 ffffffff-ffff-ffff-ffff-ffffffffffff   00000000-0000-0000-0000-000000000000    
9
 
--- !state_combine --
-ffffffff-ffff-ffff-ffff-ffffffffffff   00000000-0000-0000-0000-000000000000    
9
-
 -- !state_union --
 \N     \N      0
 
--- !state_combine --
-\N     \N      0
-
 -- !state_union --
 00000000-0000-0000-0000-000000000000   00000000-0000-0000-0000-000000000000    
10
 
--- !state_combine --
-00000000-0000-0000-0000-000000000000   00000000-0000-0000-0000-000000000000    
10
-
 -- !state_union --
 00000000-0000-0000-0000-000000000001   00000000-0000-0000-0000-000000000001    
10
 
--- !state_combine --
-00000000-0000-0000-0000-000000000001   00000000-0000-0000-0000-000000000001    
10
-
 -- !state_union --
 550e8400-e29b-41d4-a716-446655440000   550e8400-e29b-41d4-a716-446655440000    
10
 
--- !state_combine --
-550e8400-e29b-41d4-a716-446655440000   550e8400-e29b-41d4-a716-446655440000    
10
-
 -- !state_union --
 018f0f59-1010-7abc-9234-001122334455   018f0f59-1010-7abc-9234-001122334455    
10
 
--- !state_combine --
-018f0f59-1010-7abc-9234-001122334455   018f0f59-1010-7abc-9234-001122334455    
10
-
 -- !state_union --
 7fffffff-ffff-ffff-ffff-ffffffffffff   7fffffff-ffff-ffff-ffff-ffffffffffff    
10
 
--- !state_combine --
-7fffffff-ffff-ffff-ffff-ffffffffffff   7fffffff-ffff-ffff-ffff-ffffffffffff    
10
-
 -- !state_union --
 80000000-0000-0000-0000-000000000000   80000000-0000-0000-0000-000000000000    
10
 
--- !state_combine --
-80000000-0000-0000-0000-000000000000   80000000-0000-0000-0000-000000000000    
10
-
 -- !state_union --
 ffffffff-ffff-ffff-ffff-ffffffffffff   ffffffff-ffff-ffff-ffff-ffffffffffff    
10
 
--- !state_combine --
-ffffffff-ffff-ffff-ffff-ffffffffffff   ffffffff-ffff-ffff-ffff-ffffffffffff    
10
-
 -- !state_union --
 00000000-0000-0000-ffff-ffffffffffff   00000000-0000-0000-ffff-ffffffffffff    
10
 
--- !state_combine --
-00000000-0000-0000-ffff-ffffffffffff   00000000-0000-0000-ffff-ffffffffffff    
10
-
 -- !state_union --
 00000000-0000-0001-0000-000000000000   00000000-0000-0001-0000-000000000000    
10
 
--- !state_combine --
-00000000-0000-0001-0000-000000000000   00000000-0000-0001-0000-000000000000    
10
-
 -- !state_union --
 ffffffff-ffff-ffff-ffff-ffffffffffff   00000000-0000-0000-0000-000000000000    
9
 
--- !state_combine --
-ffffffff-ffff-ffff-ffff-ffffffffffff   00000000-0000-0000-0000-000000000000    
9
-
 -- !state_union --
 \N     \N      0
 
--- !state_combine --
-\N     \N      0
-
 -- !state_union --
 00000000-0000-0000-0000-000000000000   00000000-0000-0000-0000-000000000000    
10
 
--- !state_combine --
-00000000-0000-0000-0000-000000000000   00000000-0000-0000-0000-000000000000    
10
-
 -- !state_union --
 00000000-0000-0000-0000-000000000001   00000000-0000-0000-0000-000000000001    
10
 
--- !state_combine --
-00000000-0000-0000-0000-000000000001   00000000-0000-0000-0000-000000000001    
10
-
 -- !state_union --
 550e8400-e29b-41d4-a716-446655440000   550e8400-e29b-41d4-a716-446655440000    
10
 
--- !state_combine --
-550e8400-e29b-41d4-a716-446655440000   550e8400-e29b-41d4-a716-446655440000    
10
-
 -- !state_union --
 018f0f59-1010-7abc-9234-001122334455   018f0f59-1010-7abc-9234-001122334455    
10
 
--- !state_combine --
-018f0f59-1010-7abc-9234-001122334455   018f0f59-1010-7abc-9234-001122334455    
10
-
 -- !state_union --
 7fffffff-ffff-ffff-ffff-ffffffffffff   7fffffff-ffff-ffff-ffff-ffffffffffff    
10
 
--- !state_combine --
-7fffffff-ffff-ffff-ffff-ffffffffffff   7fffffff-ffff-ffff-ffff-ffffffffffff    
10
-
 -- !state_union --
 80000000-0000-0000-0000-000000000000   80000000-0000-0000-0000-000000000000    
10
 
--- !state_combine --
-80000000-0000-0000-0000-000000000000   80000000-0000-0000-0000-000000000000    
10
-
 -- !state_union --
 ffffffff-ffff-ffff-ffff-ffffffffffff   ffffffff-ffff-ffff-ffff-ffffffffffff    
10
 
--- !state_combine --
-ffffffff-ffff-ffff-ffff-ffffffffffff   ffffffff-ffff-ffff-ffff-ffffffffffff    
10
-
 -- !state_union --
 00000000-0000-0000-ffff-ffffffffffff   00000000-0000-0000-ffff-ffffffffffff    
10
 
--- !state_combine --
-00000000-0000-0000-ffff-ffffffffffff   00000000-0000-0000-ffff-ffffffffffff    
10
-
 -- !state_union --
 00000000-0000-0001-0000-000000000000   00000000-0000-0001-0000-000000000000    
10
 
--- !state_combine --
-00000000-0000-0001-0000-000000000000   00000000-0000-0001-0000-000000000000    
10
-
 -- !state_union --
 ffffffff-ffff-ffff-ffff-ffffffffffff   00000000-0000-0000-0000-000000000000    
9
 
--- !state_combine --
-ffffffff-ffff-ffff-ffff-ffffffffffff   00000000-0000-0000-0000-000000000000    
9
-
 -- !histogram --
 
{"num_buckets":1,"buckets":[{"lower":"00000000-0000-0000-0000-000000000000","upper":"ffffffff-ffff-ffff-ffff-ffffffffffff","ndv":9,"count":9,"pre_sum":0}]}
    
{"num_buckets":1,"buckets":[{"lower":"00000000-0000-0000-0000-000000000000","upper":"ffffffff-ffff-ffff-ffff-ffffffffffff","ndv":9,"count":9,"pre_sum":0}]}
 
@@ -1836,16 +1737,16 @@ ffffffff-ffff-ffff-ffff-ffffffffffff    
00000000-0000-0000-0000-000000000000    9
 ffffffff-ffff-ffff-ffff-ffffffffffff   
["ffffffff-ffff-ffff-ffff-ffffffffffff"]        
["ffffffff-ffff-ffff-ffff-ffffffffffff"]        
["ffffffff-ffff-ffff-ffff-ffffffffffff"]        
["ffffffff-ffff-ffff-ffff-ffffffffffff"]
 
 -- !collect_column_limit --
-0      []      []
-1      ["00000000-0000-0000-0000-000000000000"]        
["00000000-0000-0000-0000-000000000000"]
-2      ["00000000-0000-0000-0000-000000000001"]        
["00000000-0000-0000-0000-000000000001"]
-3      []      []
-4      []      []
-5      ["7fffffff-ffff-ffff-ffff-ffffffffffff"]        
["7fffffff-ffff-ffff-ffff-ffffffffffff"]
-6      ["80000000-0000-0000-0000-000000000000"]        
["80000000-0000-0000-0000-000000000000"]
-7      []      []
-8      []      []
-9      ["00000000-0000-0001-0000-000000000000"]        
["00000000-0000-0001-0000-000000000000"]
+0      []
+1      ["00000000-0000-0000-0000-000000000000"]
+2      ["00000000-0000-0000-0000-000000000001"]
+3      []
+4      []
+5      ["7fffffff-ffff-ffff-ffff-ffffffffffff"]
+6      ["80000000-0000-0000-0000-000000000000"]
+7      []
+8      []
+9      ["00000000-0000-0001-0000-000000000000"]
 
 -- !histogram --
 
{"num_buckets":1,"buckets":[{"lower":"00000000-0000-0000-0000-000000000000","upper":"ffffffff-ffff-ffff-ffff-ffffffffffff","ndv":9,"count":9,"pre_sum":0}]}
    
{"num_buckets":1,"buckets":[{"lower":"00000000-0000-0000-0000-000000000000","upper":"ffffffff-ffff-ffff-ffff-ffffffffffff","ndv":9,"count":9,"pre_sum":0}]}
@@ -1869,16 +1770,16 @@ ffffffff-ffff-ffff-ffff-ffffffffffff    
["ffffffff-ffff-ffff-ffff-ffffffffffff"]        ["
 ffffffff-ffff-ffff-ffff-ffffffffffff   
["ffffffff-ffff-ffff-ffff-ffffffffffff"]        
["ffffffff-ffff-ffff-ffff-ffffffffffff"]        
["ffffffff-ffff-ffff-ffff-ffffffffffff"]        
["ffffffff-ffff-ffff-ffff-ffffffffffff"]
 
 -- !collect_column_limit --
-0      []      []
-1      ["00000000-0000-0000-0000-000000000000"]        
["00000000-0000-0000-0000-000000000000"]
-2      ["00000000-0000-0000-0000-000000000001"]        
["00000000-0000-0000-0000-000000000001"]
-3      []      []
-4      []      []
-5      ["7fffffff-ffff-ffff-ffff-ffffffffffff"]        
["7fffffff-ffff-ffff-ffff-ffffffffffff"]
-6      ["80000000-0000-0000-0000-000000000000"]        
["80000000-0000-0000-0000-000000000000"]
-7      []      []
-8      []      []
-9      ["00000000-0000-0001-0000-000000000000"]        
["00000000-0000-0001-0000-000000000000"]
+0      []
+1      ["00000000-0000-0000-0000-000000000000"]
+2      ["00000000-0000-0000-0000-000000000001"]
+3      []
+4      []
+5      ["7fffffff-ffff-ffff-ffff-ffffffffffff"]
+6      ["80000000-0000-0000-0000-000000000000"]
+7      []
+8      []
+9      ["00000000-0000-0001-0000-000000000000"]
 
 -- !histogram --
 
{"num_buckets":1,"buckets":[{"lower":"00000000-0000-0000-0000-000000000000","upper":"ffffffff-ffff-ffff-ffff-ffffffffffff","ndv":9,"count":9,"pre_sum":0}]}
    
{"num_buckets":1,"buckets":[{"lower":"00000000-0000-0000-0000-000000000000","upper":"ffffffff-ffff-ffff-ffff-ffffffffffff","ndv":9,"count":9,"pre_sum":0}]}
@@ -1902,14 +1803,14 @@ ffffffff-ffff-ffff-ffff-ffffffffffff    
["ffffffff-ffff-ffff-ffff-ffffffffffff"]        ["
 ffffffff-ffff-ffff-ffff-ffffffffffff   
["ffffffff-ffff-ffff-ffff-ffffffffffff"]        
["ffffffff-ffff-ffff-ffff-ffffffffffff"]        
["ffffffff-ffff-ffff-ffff-ffffffffffff"]        
["ffffffff-ffff-ffff-ffff-ffffffffffff"]
 
 -- !collect_column_limit --
-0      []      []
-1      ["00000000-0000-0000-0000-000000000000"]        
["00000000-0000-0000-0000-000000000000"]
-2      ["00000000-0000-0000-0000-000000000001"]        
["00000000-0000-0000-0000-000000000001"]
-3      []      []
-4      []      []
-5      ["7fffffff-ffff-ffff-ffff-ffffffffffff"]        
["7fffffff-ffff-ffff-ffff-ffffffffffff"]
-6      ["80000000-0000-0000-0000-000000000000"]        
["80000000-0000-0000-0000-000000000000"]
-7      []      []
-8      []      []
-9      ["00000000-0000-0001-0000-000000000000"]        
["00000000-0000-0001-0000-000000000000"]
+0      []
+1      ["00000000-0000-0000-0000-000000000000"]
+2      ["00000000-0000-0000-0000-000000000001"]
+3      []
+4      []
+5      ["7fffffff-ffff-ffff-ffff-ffffffffffff"]
+6      ["80000000-0000-0000-0000-000000000000"]
+7      []
+8      []
+9      ["00000000-0000-0001-0000-000000000000"]
 
diff --git 
a/regression-test/data/nereids_function_p0/agg_function/agg_distinct_function.out
 
b/regression-test/data/nereids_function_p0/agg_function/agg_distinct_function.out
new file mode 100644
index 00000000000..e3c944e1365
--- /dev/null
+++ 
b/regression-test/data/nereids_function_p0/agg_function/agg_distinct_function.out
@@ -0,0 +1,393 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !array_agg --
+[1, 2, 3, 4, 5]
+
+-- !array_agg_group --
+1      [1, 2, 3]
+2      [1, 4, 5]
+
+-- !avg --
+3.0
+
+-- !avg_group --
+1      2.0
+2      3.333333333333333
+
+-- !avg_weighted --
+3.666666666666667
+
+-- !avg_weighted_group --
+1      2.333333333333333
+2      4.2
+
+-- !collect_list --
+[1, 2, 3, 4, 5]
+
+-- !collect_list_group --
+1      [1, 2, 3]
+2      [1, 4, 5]
+
+-- !collect_set --
+[1, 2, 3, 4, 5]
+
+-- !collect_set_group --
+1      [1, 2, 3]
+2      [1, 4, 5]
+
+-- !corr_welford --
+1.0
+
+-- !corr_welford_group --
+1      1.0
+2      1.0
+
+-- !corr --
+1.0
+
+-- !corr_group --
+1      1.0
+2      1.0
+
+-- !covar --
+20.0
+
+-- !covar_group --
+1      6.666666666666664
+2      28.88888888888889
+
+-- !covar_samp --
+25.0
+
+-- !covar_samp_group --
+1      10.0
+2      43.33333333333334
+
+-- !group_bit_and --
+0
+
+-- !group_bit_and_group --
+1      0
+2      0
+
+-- !group_bit_or --
+7
+
+-- !group_bit_or_group --
+1      3
+2      5
+
+-- !group_bit_xor --
+1
+
+-- !group_bit_xor_group --
+1      0
+2      0
+
+-- !group_concat --
+a,b,c,d,e
+
+-- !group_concat_group --
+1      a,b,c
+2      a,d,e
+
+-- !histogram --
+{"num_buckets":5,"buckets":[{"lower":"1","upper":"1","ndv":1,"count":1,"pre_sum":0},{"lower":"2","upper":"2","ndv":1,"count":1,"pre_sum":1},{"lower":"3","upper":"3","ndv":1,"count":1,"pre_sum":2},{"lower":"4","upper":"4","ndv":1,"count":1,"pre_sum":3},{"lower":"5","upper":"5","ndv":1,"count":1,"pre_sum":4}]}
+
+-- !histogram_group --
+1      
{"num_buckets":3,"buckets":[{"lower":"1","upper":"1","ndv":1,"count":1,"pre_sum":0},{"lower":"2","upper":"2","ndv":1,"count":1,"pre_sum":1},{"lower":"3","upper":"3","ndv":1,"count":1,"pre_sum":2}]}
+2      
{"num_buckets":3,"buckets":[{"lower":"1","upper":"1","ndv":1,"count":1,"pre_sum":0},{"lower":"4","upper":"4","ndv":1,"count":1,"pre_sum":1},{"lower":"5","upper":"5","ndv":1,"count":1,"pre_sum":2}]}
+
+-- !hll_raw_agg --
+5
+
+-- !hll_raw_agg_group --
+1      3
+2      3
+
+-- !hll_union_agg --
+5
+
+-- !hll_union_agg_group --
+1      3
+2      3
+
+-- !kurt --
+-1.3
+
+-- !kurt_group --
+1      -1.5
+2      -1.499999999999995
+
+-- !map_agg_group --
+[1, 2, 3]      ["a", "b", "c"]
+[1, 4, 5]      ["a", "d", "e"]
+
+-- !map_agg --
+[1, 2, 3, 4, 5]        ["a", "b", "c", "d", "e"]
+
+-- !max_by --
+e
+
+-- !max_by_group --
+1      c
+2      e
+
+-- !min_by --
+a
+
+-- !min_by_group --
+1      a
+2      a
+
+-- !percentile --
+3.0
+
+-- !percentile_group --
+1      2.0
+2      4.0
+
+-- !percentile_approx --
+3.0
+
+-- !percentile_approx_group --
+1      2.0
+2      4.0
+
+-- !percentile_array --
+[2, 3, 4]
+
+-- !percentile_array_group --
+1      [1.5, 2, 2.5]
+2      [2.5, 4, 4.5]
+
+-- !percentile_approx_weighted --
+3.857142925262451
+
+-- !percentile_approx_weighted_group --
+1      2.400000095367432
+2      4.44444465637207
+
+-- !topn --
+{"e":1,"d":1,"c":1}
+
+-- !topn_group --
+1      {"c":1,"b":1,"a":1}
+2      {"e":1,"d":1,"a":1}
+
+-- !topn_array --
+["e", "d", "c"]
+
+-- !topn_array_group --
+1      ["c", "b", "a"]
+2      ["e", "d", "a"]
+
+-- !topn_weighted --
+["e", "d", "c"]
+
+-- !topn_weighted_group --
+1      ["c", "b", "a"]
+2      ["e", "d", "a"]
+
+-- !two_distinct_functions --
+[1, 2, 3, 4, 5]        [10, 20, 30, 40, 50]
+
+-- !two_distinct_functions_group --
+1      [1, 2, 3]       [10, 20, 30]
+2      [1, 4, 5]       [10, 40, 50]
+
+-- !two_avg --
+3.0    30.0
+
+-- !two_avg_group --
+1      2.0     20.0
+2      3.333333333333333       33.33333333333334
+
+-- !two_avg_weighted --
+3.666666666666667      36.66666666666666
+
+-- !two_avg_weighted_group --
+1      2.333333333333333       23.33333333333333
+2      4.2     42.0
+
+-- !two_collect_list --
+[1, 2, 3, 4, 5]        [10, 20, 30, 40, 50]
+
+-- !two_collect_list_group --
+1      [1, 2, 3]       [10, 20, 30]
+2      [1, 4, 5]       [10, 40, 50]
+
+-- !two_collect_set --
+[1, 2, 3, 4, 5]        [10, 20, 30, 40, 50]
+
+-- !two_collect_set_group --
+1      [1, 2, 3]       [10, 20, 30]
+2      [1, 4, 5]       [10, 40, 50]
+
+-- !two_corr_welford --
+0.9999999999999999     1.0
+
+-- !two_corr_welford_group --
+1      1.0     1.0
+2      1.0     1.0
+
+-- !two_corr --
+1.0    1.0
+
+-- !two_corr_group --
+1      1.0     1.0
+2      1.0     1.0
+
+-- !two_covar --
+20.0   2.0
+
+-- !two_covar_group --
+1      6.666666666666664       0.666666666666667
+2      28.88888888888889       2.888888888888889
+
+-- !two_covar_samp --
+25.0   2.5
+
+-- !two_covar_samp_group --
+1      10.0    1.0
+2      43.33333333333334       4.333333333333332
+
+-- !two_group_bit_and --
+0      0
+
+-- !two_group_bit_and_group --
+1      0       0
+2      0       0
+
+-- !two_group_bit_or --
+7      62
+
+-- !two_group_bit_or_group --
+1      3       30
+2      5       58
+
+-- !two_group_bit_xor --
+1      26
+
+-- !two_group_bit_xor_group --
+1      0       0
+2      0       16
+
+-- !two_group_concat --
+a,b,c,d,e      1,2,3,4,5
+
+-- !two_group_concat_group --
+1      a,b,c   1,2,3
+2      a,d,e   1,4,5
+
+-- !two_histogram --
+{"num_buckets":5,"buckets":[{"lower":"1","upper":"1","ndv":1,"count":1,"pre_sum":0},{"lower":"2","upper":"2","ndv":1,"count":1,"pre_sum":1},{"lower":"3","upper":"3","ndv":1,"count":1,"pre_sum":2},{"lower":"4","upper":"4","ndv":1,"count":1,"pre_sum":3},{"lower":"5","upper":"5","ndv":1,"count":1,"pre_sum":4}]}
  
{"num_buckets":5,"buckets":[{"lower":"10","upper":"10","ndv":1,"count":1,"pre_sum":0},{"lower":"20","upper":"20","ndv":1,"count":1,"pre_sum":1},{"lower":"30","upper":"30","ndv":1,"co
 [...]
+
+-- !two_histogram_group --
+1      
{"num_buckets":3,"buckets":[{"lower":"1","upper":"1","ndv":1,"count":1,"pre_sum":0},{"lower":"2","upper":"2","ndv":1,"count":1,"pre_sum":1},{"lower":"3","upper":"3","ndv":1,"count":1,"pre_sum":2}]}
   
{"num_buckets":3,"buckets":[{"lower":"10","upper":"10","ndv":1,"count":1,"pre_sum":0},{"lower":"20","upper":"20","ndv":1,"count":1,"pre_sum":1},{"lower":"30","upper":"30","ndv":1,"count":1,"pre_sum":2}]}
+2      
{"num_buckets":3,"buckets":[{"lower":"1","upper":"1","ndv":1,"count":1,"pre_sum":0},{"lower":"4","upper":"4","ndv":1,"count":1,"pre_sum":1},{"lower":"5","upper":"5","ndv":1,"count":1,"pre_sum":2}]}
   
{"num_buckets":3,"buckets":[{"lower":"10","upper":"10","ndv":1,"count":1,"pre_sum":0},{"lower":"40","upper":"40","ndv":1,"count":1,"pre_sum":1},{"lower":"50","upper":"50","ndv":1,"count":1,"pre_sum":2}]}
+
+-- !two_hll_raw_agg --
+5      5
+
+-- !two_hll_raw_agg_group --
+1      3       3
+2      3       3
+
+-- !two_hll_union_agg --
+5      5
+
+-- !two_hll_union_agg_group --
+1      3       3
+2      3       3
+
+-- !two_kurt --
+-1.3   -1.3
+
+-- !two_kurt_group --
+1      -1.5    -1.5
+2      -1.499999999999995      -1.499999999999998
+
+-- !two_map_agg --
+[1, 2, 3, 4, 5]        ["a", "b", "c", "d", "e"]       [10, 20, 30, 40, 50]    
["a", "b", "c", "d", "e"]
+
+-- !two_map_agg_group --
+1      [1, 2, 3]       ["a", "b", "c"] [10, 20, 30]    ["a", "b", "c"]
+2      [1, 4, 5]       ["a", "d", "e"] [10, 40, 50]    ["a", "d", "e"]
+
+-- !two_max_by --
+e      e
+
+-- !two_max_by_group --
+1      c       c
+2      e       e
+
+-- !two_min_by --
+a      a
+
+-- !two_min_by_group --
+1      a       a
+2      a       a
+
+-- !two_percentile --
+3.0    30.0
+
+-- !two_percentile_group --
+1      2.0     20.0
+2      4.0     40.0
+
+-- !two_percentile_approx --
+3.0    30.0
+
+-- !two_percentile_approx_group --
+1      2.0     20.0
+2      4.0     40.0
+
+-- !two_percentile_array --
+[2, 3, 4]      [20, 30, 40]
+
+-- !two_percentile_array_group --
+1      [1.5, 2, 2.5]   [15, 20, 25]
+2      [2.5, 4, 4.5]   [25, 40, 45]
+
+-- !two_percentile_approx_weighted --
+3.857142925262451      38.57143020629883
+
+-- !two_percentile_approx_weighted_group --
+1      2.400000095367432       24.0
+2      4.44444465637207        44.44444274902344
+
+-- !two_topn --
+{"e":1,"d":1,"c":1}    {"5":1,"4":1,"3":1}
+
+-- !two_topn_group --
+1      {"c":1,"b":1,"a":1}     {"3":1,"2":1,"1":1}
+2      {"e":1,"d":1,"a":1}     {"5":1,"4":1,"1":1}
+
+-- !two_topn_array --
+["e", "d", "c"]        ["5", "4", "3"]
+
+-- !two_topn_array_group --
+1      ["c", "b", "a"] ["3", "2", "1"]
+2      ["e", "d", "a"] ["5", "4", "1"]
+
+-- !two_topn_weighted --
+["e", "d", "c"]        ["5", "4", "3"]
+
+-- !two_topn_weighted_group --
+1      ["c", "b", "a"] ["3", "2", "1"]
+2      ["e", "d", "a"] ["5", "4", "1"]
+
+-- !percentile_res --
+2.0    40.0
+
+-- !percentile_res_group --
+1.5    25.0
+2.5    45.0
+
+-- !ema --
+2.501954556443252      0.8055193463643526
+
+-- !ema_group --
+1.500977039337158      0.4611636182575578
+2.501953125000455      0.7772022820853798
+
diff --git 
a/regression-test/suites/datatype_p0/uuid/test_uuid_aggregate_matrix.groovy 
b/regression-test/suites/datatype_p0/uuid/test_uuid_aggregate_matrix.groovy
index 0ee731a4f59..f86131872a1 100644
--- a/regression-test/suites/datatype_p0/uuid/test_uuid_aggregate_matrix.groovy
+++ b/regression-test/suites/datatype_p0/uuid/test_uuid_aggregate_matrix.groovy
@@ -119,7 +119,12 @@ suite("test_uuid_aggregate_matrix", "p0") {
         // Group per UUID: TOPN ties cannot make the expected array order 
arbitrary.
         qt_topn """SELECT 
u,TOPN_ARRAY(u,1),TOPN_ARRAY(u,2,4),COLLECT_LIST(u,2),COLLECT_SET(u,2)
                    FROM uuid_matrix_aggregate GROUP BY u ORDER BY u NULLS 
FIRST"""
-        qt_collect_column_limit """SELECT 
id,COLLECT_LIST(u,num),COLLECT_SET(u,num)
+        test {
+            sql """SELECT id,COLLECT_LIST(u,num)
+                   FROM uuid_matrix_aggregate GROUP BY id,num ORDER BY id"""
+            exception "collect_list requires second parameter must be a 
constant"
+        }
+        qt_collect_column_limit """SELECT id,COLLECT_SET(u,num)
                                   FROM uuid_matrix_aggregate GROUP BY id,num 
ORDER BY id"""
         for (String query : ['HISTOGRAM(u,num)', 'TOPN_ARRAY(u,num)']) {
             test {
diff --git 
a/regression-test/suites/nereids_function_p0/agg_function/agg_distinct_function.groovy
 
b/regression-test/suites/nereids_function_p0/agg_function/agg_distinct_function.groovy
new file mode 100644
index 00000000000..bd555aa6fac
--- /dev/null
+++ 
b/regression-test/suites/nereids_function_p0/agg_function/agg_distinct_function.groovy
@@ -0,0 +1,212 @@
+// 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.
+
+suite("agg_distinct_function") {
+    sql "drop table if exists agg_distinct_function"
+    sql """
+        create table agg_distinct_function (
+            k int,
+            a int,
+            b int,
+            s varchar(10),
+            w int
+        ) engine=olap
+        duplicate key(k)
+        distributed by hash(k) buckets 1
+        properties ("replication_num" = "1")
+    """
+
+    // The duplicate rows make DISTINCT observable. Every query is run both as 
a
+    // scalar aggregation and as a grouped aggregation.
+    sql """
+        insert into agg_distinct_function values
+            (1, 1, 10, 'a', 1), (1, 1, 10, 'a', 1),
+            (1, 2, 20, 'b', 2), (1, 3, 30, 'c', 3),
+            (2, 1, 10, 'a', 1), (2, 4, 40, 'd', 4),
+            (2, 4, 40, 'd', 4), (2, 5, 50, 'e', 5)
+    """
+
+    qt_array_agg "select array_sort(array_agg(distinct a)) from 
agg_distinct_function"
+    order_qt_array_agg_group "select k, array_sort(array_agg(distinct a)) from 
agg_distinct_function group by k order by k"
+    qt_avg "select avg(distinct a) from agg_distinct_function"
+    order_qt_avg_group "select k, avg(distinct a) from agg_distinct_function 
group by k order by k"
+    qt_avg_weighted "select avg_weighted(distinct a, w) from 
agg_distinct_function"
+    order_qt_avg_weighted_group "select k, avg_weighted(distinct a, w) from 
agg_distinct_function group by k order by k"
+    qt_collect_list "select array_sort(collect_list(distinct a)) from 
agg_distinct_function"
+    order_qt_collect_list_group "select k, array_sort(collect_list(distinct 
a)) from agg_distinct_function group by k order by k"
+    qt_collect_set "select array_sort(collect_set(distinct a)) from 
agg_distinct_function"
+    order_qt_collect_set_group "select k, array_sort(collect_set(distinct a)) 
from agg_distinct_function group by k order by k"
+    qt_corr_welford "select corr_welford(distinct a, b) from 
agg_distinct_function"
+    order_qt_corr_welford_group "select k, corr_welford(distinct a, b) from 
agg_distinct_function group by k order by k"
+    qt_corr "select corr(distinct a, b) from agg_distinct_function"
+    order_qt_corr_group "select k, corr(distinct a, b) from 
agg_distinct_function group by k order by k"
+    qt_covar "select covar(distinct a, b) from agg_distinct_function"
+    order_qt_covar_group "select k, covar(distinct a, b) from 
agg_distinct_function group by k order by k"
+    qt_covar_samp "select covar_samp(distinct a, b) from agg_distinct_function"
+    order_qt_covar_samp_group "select k, covar_samp(distinct a, b) from 
agg_distinct_function group by k order by k"
+    qt_group_bit_and "select group_bit_and(distinct a) from 
agg_distinct_function"
+    order_qt_group_bit_and_group "select k, group_bit_and(distinct a) from 
agg_distinct_function group by k order by k"
+    qt_group_bit_or "select group_bit_or(distinct a) from 
agg_distinct_function"
+    order_qt_group_bit_or_group "select k, group_bit_or(distinct a) from 
agg_distinct_function group by k order by k"
+    qt_group_bit_xor "select group_bit_xor(distinct a) from 
agg_distinct_function"
+    order_qt_group_bit_xor_group "select k, group_bit_xor(distinct a) from 
agg_distinct_function group by k order by k"
+//    qt_group_bitmap_xor "select bitmap_to_string(group_bitmap_xor(distinct 
bitmap_hash(a))) from agg_distinct_function"
+//    order_qt_group_bitmap_xor_group "select k, 
bitmap_to_string(group_bitmap_xor(distinct bitmap_hash(a))) from 
agg_distinct_function group by k order by k"
+    qt_group_concat "select group_concat(distinct s order by s) from 
agg_distinct_function"
+    order_qt_group_concat_group "select k, group_concat(distinct s order by s) 
from agg_distinct_function group by k order by k"
+    qt_histogram "select histogram(distinct a) from agg_distinct_function"
+    order_qt_histogram_group "select k, histogram(distinct a) from 
agg_distinct_function group by k order by k"
+    qt_hll_raw_agg "select hll_cardinality(hll_raw_agg(distinct hll_hash(a))) 
from agg_distinct_function"
+    order_qt_hll_raw_agg_group "select k, hll_cardinality(hll_raw_agg(distinct 
hll_hash(a))) from agg_distinct_function group by k order by k"
+    qt_hll_union_agg "select hll_union_agg(distinct hll_hash(a)) from 
agg_distinct_function"
+    order_qt_hll_union_agg_group "select k, hll_union_agg(distinct 
hll_hash(a)) from agg_distinct_function group by k order by k"
+    qt_kurt "select kurt(distinct a) from agg_distinct_function"
+    order_qt_kurt_group "select k, kurt(distinct a) from agg_distinct_function 
group by k order by k"
+    order_qt_map_agg_group """
+        SELECT
+            array_sort(map_keys(m)) AS sorted_keys,
+            array_sortby(map_values(m), map_keys(m)) AS values_by_sorted_key
+        FROM (
+            SELECT map_agg(DISTINCT a, s) AS m
+            FROM agg_distinct_function
+            group by k
+        ) t;
+    """
+    order_qt_map_agg """
+       SELECT
+            array_sort(map_keys(m)) AS sorted_keys,
+            array_sortby(map_values(m), map_keys(m)) AS values_by_sorted_key
+        FROM (
+            SELECT map_agg(DISTINCT a, s) AS m
+            FROM agg_distinct_function
+        ) t;
+    """
+    qt_max_by "select max_by(distinct s, a) from agg_distinct_function"
+    order_qt_max_by_group "select k, max_by(distinct s, a) from 
agg_distinct_function group by k order by k"
+    qt_min_by "select min_by(distinct s, a) from agg_distinct_function"
+    order_qt_min_by_group "select k, min_by(distinct s, a) from 
agg_distinct_function group by k order by k"
+    qt_percentile "select percentile(distinct a, 0.5) from 
agg_distinct_function"
+    order_qt_percentile_group "select k, percentile(distinct a, 0.5) from 
agg_distinct_function group by k order by k"
+    qt_percentile_approx "select percentile_approx(distinct a, 0.5) from 
agg_distinct_function"
+    order_qt_percentile_approx_group "select k, percentile_approx(distinct a, 
0.5) from agg_distinct_function group by k order by k"
+    qt_percentile_array "select percentile_array(distinct a, [0.25, 0.5, 
0.75]) from agg_distinct_function"
+    order_qt_percentile_array_group "select k, percentile_array(distinct a, 
[0.25, 0.5, 0.75]) from agg_distinct_function group by k order by k"
+    qt_percentile_approx_weighted "select percentile_approx_weighted(distinct 
a, w, 0.5) from agg_distinct_function"
+    order_qt_percentile_approx_weighted_group "select k, 
percentile_approx_weighted(distinct a, w, 0.5) from agg_distinct_function group 
by k order by k"
+    qt_topn "select topn(distinct s, 3) from agg_distinct_function"
+    order_qt_topn_group "select k, topn(distinct s, 3) from 
agg_distinct_function group by k order by k"
+    qt_topn_array "select topn_array(distinct s, 3) from agg_distinct_function"
+    order_qt_topn_array_group "select k, topn_array(distinct s, 3) from 
agg_distinct_function group by k order by k"
+    qt_topn_weighted "select topn_weighted(distinct s, w, 3) from 
agg_distinct_function"
+    order_qt_topn_weighted_group "select k, topn_weighted(distinct s, w, 3) 
from agg_distinct_function group by k order by k"
+    sql "select count_by_enum(distinct s) from agg_distinct_function"
+    sql "select k, count_by_enum(distinct s) from agg_distinct_function group 
by k order by k"
+
+    // Each pair uses different DISTINCT argument groups and exercises the CTE 
split path.
+    qt_two_distinct_functions "select array_sort(array_agg(distinct a)), 
array_sort(array_agg(distinct b)) from agg_distinct_function"
+    order_qt_two_distinct_functions_group "select k, 
array_sort(array_agg(distinct a)), array_sort(array_agg(distinct b)) from 
agg_distinct_function group by k order by k"
+    qt_two_avg "select avg(distinct a), avg(distinct b) from 
agg_distinct_function"
+    order_qt_two_avg_group "select k, avg(distinct a), avg(distinct b) from 
agg_distinct_function group by k order by k"
+    qt_two_avg_weighted "select avg_weighted(distinct a, w), 
avg_weighted(distinct b, w) from agg_distinct_function"
+    order_qt_two_avg_weighted_group "select k, avg_weighted(distinct a, w), 
avg_weighted(distinct b, w) from agg_distinct_function group by k order by k"
+    qt_two_collect_list "select array_sort(collect_list(distinct a)), 
array_sort(collect_list(distinct b)) from agg_distinct_function"
+    order_qt_two_collect_list_group "select k, 
array_sort(collect_list(distinct a)), array_sort(collect_list(distinct b)) from 
agg_distinct_function group by k order by k"
+    qt_two_collect_set "select array_sort(collect_set(distinct a)), 
array_sort(collect_set(distinct b)) from agg_distinct_function"
+    order_qt_two_collect_set_group "select k, array_sort(collect_set(distinct 
a)), array_sort(collect_set(distinct b)) from agg_distinct_function group by k 
order by k"
+    qt_two_corr_welford "select corr_welford(distinct a, b), 
corr_welford(distinct a, w) from agg_distinct_function"
+    order_qt_two_corr_welford_group "select k, corr_welford(distinct a, b), 
corr_welford(distinct a, w) from agg_distinct_function group by k order by k"
+    qt_two_corr "select corr(distinct a, b), corr(distinct a, w) from 
agg_distinct_function"
+    order_qt_two_corr_group "select k, corr(distinct a, b), corr(distinct a, 
w) from agg_distinct_function group by k order by k"
+    qt_two_covar "select covar(distinct a, b), covar(distinct a, w) from 
agg_distinct_function"
+    order_qt_two_covar_group "select k, covar(distinct a, b), covar(distinct 
a, w) from agg_distinct_function group by k order by k"
+    qt_two_covar_samp "select covar_samp(distinct a, b), covar_samp(distinct 
a, w) from agg_distinct_function"
+    order_qt_two_covar_samp_group "select k, covar_samp(distinct a, b), 
covar_samp(distinct a, w) from agg_distinct_function group by k order by k"
+    qt_two_group_bit_and "select group_bit_and(distinct a), 
group_bit_and(distinct b) from agg_distinct_function"
+    order_qt_two_group_bit_and_group "select k, group_bit_and(distinct a), 
group_bit_and(distinct b) from agg_distinct_function group by k order by k"
+    qt_two_group_bit_or "select group_bit_or(distinct a), 
group_bit_or(distinct b) from agg_distinct_function"
+    order_qt_two_group_bit_or_group "select k, group_bit_or(distinct a), 
group_bit_or(distinct b) from agg_distinct_function group by k order by k"
+    qt_two_group_bit_xor "select group_bit_xor(distinct a), 
group_bit_xor(distinct b) from agg_distinct_function"
+    order_qt_two_group_bit_xor_group "select k, group_bit_xor(distinct a), 
group_bit_xor(distinct b) from agg_distinct_function group by k order by k"
+//    qt_two_group_bitmap_xor "select 
bitmap_to_string(group_bitmap_xor(distinct bitmap_hash(a))), 
bitmap_to_string(group_bitmap_xor(distinct bitmap_hash(b))) from 
agg_distinct_function"
+//    order_qt_two_group_bitmap_xor_group "select k, 
bitmap_to_string(group_bitmap_xor(distinct bitmap_hash(a))), 
bitmap_to_string(group_bitmap_xor(distinct bitmap_hash(b))) from 
agg_distinct_function group by k order by k"
+    qt_two_group_concat "select group_concat(distinct s order by s), 
group_concat(distinct cast(a as string) order by cast(a as string)) from 
agg_distinct_function"
+    order_qt_two_group_concat_group "select k, group_concat(distinct s order 
by s), group_concat(distinct cast(a as string) order by cast(a as string)) from 
agg_distinct_function group by k order by k"
+    qt_two_histogram "select histogram(distinct a), histogram(distinct b) from 
agg_distinct_function"
+    order_qt_two_histogram_group "select k, histogram(distinct a), 
histogram(distinct b) from agg_distinct_function group by k order by k"
+    qt_two_hll_raw_agg "select hll_cardinality(hll_raw_agg(distinct 
hll_hash(a))), hll_cardinality(hll_raw_agg(distinct hll_hash(b))) from 
agg_distinct_function"
+    order_qt_two_hll_raw_agg_group "select k, 
hll_cardinality(hll_raw_agg(distinct hll_hash(a))), 
hll_cardinality(hll_raw_agg(distinct hll_hash(b))) from agg_distinct_function 
group by k order by k"
+    qt_two_hll_union_agg "select hll_union_agg(distinct hll_hash(a)), 
hll_union_agg(distinct hll_hash(b)) from agg_distinct_function"
+    order_qt_two_hll_union_agg_group "select k, hll_union_agg(distinct 
hll_hash(a)), hll_union_agg(distinct hll_hash(b)) from agg_distinct_function 
group by k order by k"
+    qt_two_kurt "select kurt(distinct a), kurt(distinct b) from 
agg_distinct_function"
+    order_qt_two_kurt_group "select k, kurt(distinct a), kurt(distinct b) from 
agg_distinct_function group by k order by k"
+    qt_two_map_agg """
+        select array_sort(map_keys(a_map)), array_sortby(map_values(a_map), 
map_keys(a_map)),
+                array_sort(map_keys(b_map)), array_sortby(map_values(b_map), 
map_keys(b_map))
+        from (
+            select map_agg(distinct a, s) as a_map, map_agg(distinct b, s) as 
b_map
+            from agg_distinct_function
+        ) t
+    """
+    order_qt_two_map_agg_group """
+        select k, array_sort(map_keys(a_map)), array_sortby(map_values(a_map), 
map_keys(a_map)),
+                array_sort(map_keys(b_map)), array_sortby(map_values(b_map), 
map_keys(b_map))
+        from (
+            select k, map_agg(distinct a, s) as a_map, map_agg(distinct b, s) 
as b_map
+            from agg_distinct_function
+            group by k
+        ) t
+        order by k
+    """
+    qt_two_max_by "select max_by(distinct s, a), max_by(distinct s, b) from 
agg_distinct_function"
+    order_qt_two_max_by_group "select k, max_by(distinct s, a), 
max_by(distinct s, b) from agg_distinct_function group by k order by k"
+    qt_two_min_by "select min_by(distinct s, a), min_by(distinct s, b) from 
agg_distinct_function"
+    order_qt_two_min_by_group "select k, min_by(distinct s, a), 
min_by(distinct s, b) from agg_distinct_function group by k order by k"
+    qt_two_percentile "select percentile(distinct a, 0.5), percentile(distinct 
b, 0.5) from agg_distinct_function"
+    order_qt_two_percentile_group "select k, percentile(distinct a, 0.5), 
percentile(distinct b, 0.5) from agg_distinct_function group by k order by k"
+    qt_two_percentile_approx "select percentile_approx(distinct a, 0.5), 
percentile_approx(distinct b, 0.5) from agg_distinct_function"
+    order_qt_two_percentile_approx_group "select k, percentile_approx(distinct 
a, 0.5), percentile_approx(distinct b, 0.5) from agg_distinct_function group by 
k order by k"
+    qt_two_percentile_array "select percentile_array(distinct a, [0.25, 0.5, 
0.75]), percentile_array(distinct b, [0.25, 0.5, 0.75]) from 
agg_distinct_function"
+    order_qt_two_percentile_array_group "select k, percentile_array(distinct 
a, [0.25, 0.5, 0.75]), percentile_array(distinct b, [0.25, 0.5, 0.75]) from 
agg_distinct_function group by k order by k"
+    qt_two_percentile_approx_weighted "select 
percentile_approx_weighted(distinct a, w, 0.5), 
percentile_approx_weighted(distinct b, w, 0.5) from agg_distinct_function"
+    order_qt_two_percentile_approx_weighted_group "select k, 
percentile_approx_weighted(distinct a, w, 0.5), 
percentile_approx_weighted(distinct b, w, 0.5) from agg_distinct_function group 
by k order by k"
+    qt_two_topn "select topn(distinct s, 3), topn(distinct cast(a as string), 
3) from agg_distinct_function"
+    order_qt_two_topn_group "select k, topn(distinct s, 3), topn(distinct 
cast(a as string), 3) from agg_distinct_function group by k order by k"
+    qt_two_topn_array "select topn_array(distinct s, 3), topn_array(distinct 
cast(a as string), 3) from agg_distinct_function"
+    order_qt_two_topn_array_group "select k, topn_array(distinct s, 3), 
topn_array(distinct cast(a as string), 3) from agg_distinct_function group by k 
order by k"
+    qt_two_topn_weighted "select topn_weighted(distinct s, w, 3), 
topn_weighted(distinct cast(a as string), w, 3) from agg_distinct_function"
+    order_qt_two_topn_weighted_group "select k, topn_weighted(distinct s, w, 
3), topn_weighted(distinct cast(a as string), w, 3) from agg_distinct_function 
group by k order by k"
+    sql "select count_by_enum(distinct s), count_by_enum(distinct cast(a as 
string)) from agg_distinct_function"
+    sql "select k, count_by_enum(distinct s), count_by_enum(distinct cast(a as 
string)) from agg_distinct_function group by k order by k"
+    order_qt_percentile_res """select percentile_reservoir(distinct a, 0.25), 
percentile_reservoir(distinct b, 0.75) from agg_distinct_function"""
+    order_qt_percentile_res_group """select percentile_reservoir(distinct a, 
0.25), percentile_reservoir(distinct b, 0.75) from agg_distinct_function group 
by k"""
+    order_qt_ema """select exponential_moving_average(distinct 1.0, w, 
b),exponential_moving_average(distinct 5.0, a, b) from agg_distinct_function"""
+    order_qt_ema_group """select exponential_moving_average(distinct 1.0, w, 
b),exponential_moving_average(distinct 5.0, a, b) from agg_distinct_function 
group by k"""
+
+    sql """
+        select percentile_reservoir(distinct a, 0.25), 
percentile_reservoir(distinct a, 0.75)
+        from agg_distinct_function group by rollup(k)
+    """
+    sql """
+        select exponential_moving_average(distinct 1.0, a, 
b),exponential_moving_average(distinct 5.0, a, b)
+        from agg_distinct_function group by rollup(k)
+    """
+
+    test {
+        sql "select bitmap_to_string(group_bitmap_xor(distinct 
bitmap_hash(a))) from agg_distinct_function"
+        exception "group_bitmap_xor does not support DISTINCT"
+    }
+}
diff --git 
a/regression-test/suites/nereids_rules_p0/agg_strategy/distinct_agg_strategy_selector.groovy
 
b/regression-test/suites/nereids_rules_p0/agg_strategy/distinct_agg_strategy_selector.groovy
index e43340bcdff..abb14b40d4d 100644
--- 
a/regression-test/suites/nereids_rules_p0/agg_strategy/distinct_agg_strategy_selector.groovy
+++ 
b/regression-test/suites/nereids_rules_p0/agg_strategy/distinct_agg_strategy_selector.groovy
@@ -60,4 +60,43 @@ suite("distinct_agg_strategy_selector") {
     // test
     order_qt_count_distinct_group "select count(distinct a_1,b_5), 
count(distinct b_5,a_1) from t1000;"
     order_qt_count_distinct_group_with_gby "select count(distinct a_1,b_5), 
count(distinct b_5,a_1) from t1000 group by c_10;"
+
+    multi_sql """
+    SET enable_nereids_planner = true;
+
+    DROP VIEW IF EXISTS v_distinct_guard_repro;
+    DROP TABLE IF EXISTS t_distinct_guard_repro;
+
+    CREATE TABLE t_distinct_guard_repro (
+        id INT,
+        a DECIMAL(20, 2),
+        b INT
+    )
+    ENGINE = OLAP
+    DUPLICATE KEY(id)
+    DISTRIBUTED BY HASH(id) BUCKETS 1
+    PROPERTIES (
+        "replication_num" = "1"
+    );
+
+    INSERT INTO t_distinct_guard_repro VALUES
+        (1, 1.00, 10),
+        (2, 1.00, 10),
+        (3, 2.00, 20),
+        (4, 3.00, 30);
+    """
+    sql "SET enable_decimal256 = false;"
+
+    sql """CREATE VIEW v_distinct_guard_repro AS
+    SELECT
+        SUM(DISTINCT a) AS distinct_sum_a,
+        ARRAY_AGG(DISTINCT b) AS distinct_b_values
+    FROM t_distinct_guard_repro;"""
+
+    sql "SET enable_decimal256 = true;"
+
+    sql """SELECT
+        distinct_sum_a,
+        array_sort(distinct_b_values)
+    FROM v_distinct_guard_repro;"""
 }
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to