HappenLee commented on code in PR #66942:
URL: https://github.com/apache/doris/pull/66942#discussion_r3841802691


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/AggCombinerFunctionBuilder.java:
##########
@@ -139,6 +146,12 @@ public Pair<BoundFunction, AggregateFunction> build(String 
name, List<?> argumen
                 arguments = arguments.subList(1, arguments.size());
             }
             return Pair.of(new StateCombinator((List<Expression>) arguments, 
nestedFunction), nestedFunction);
+        } else if (combinatorSuffix.equalsIgnoreCase(COMBINE)) {
+            AggregateFunction nestedFunction = buildState(nestedName, 
arguments);
+            if (!arguments.isEmpty() && arguments.get(0) instanceof Boolean && 
(Boolean) arguments.get(0)) {
+                throw new IllegalStateException(name + " doesn't support 
DISTINCT");
+            }
+            return Pair.of(new CombineCombinator((List<Expression>) arguments, 
nestedFunction), nestedFunction);

Review Comment:
   Fixed for this PR by explicitly disabling `ai_agg_state` and 
`ai_agg_combine` at function resolution. This avoids constructing an AggState 
from the non-canonical two-argument child list while `AIAgg` expands it to 
three arguments internally. Canonical AI AggState support is deferred to a 
follow-up.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/combinator/CombineCombinator.java:
##########
@@ -0,0 +1,158 @@
+// 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.trees.expressions.functions.combinator;
+
+import org.apache.doris.catalog.BuiltinAggregateFunctions;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.FunctionRegistry;
+import org.apache.doris.catalog.FunctionSignature;
+import org.apache.doris.common.Pair;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.OrderExpression;
+import 
org.apache.doris.nereids.trees.expressions.functions.AggCombinerFunctionBuilder;
+import org.apache.doris.nereids.trees.expressions.functions.AlwaysNotNullable;
+import org.apache.doris.nereids.trees.expressions.functions.BoundFunction;
+import 
org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
+import org.apache.doris.nereids.trees.expressions.functions.ExpressionTrait;
+import org.apache.doris.nereids.trees.expressions.functions.Function;
+import org.apache.doris.nereids.trees.expressions.functions.FunctionBuilder;
+import 
org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction;
+import 
org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunctionParams;
+import org.apache.doris.nereids.trees.expressions.functions.agg.RollUpTrait;
+import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
+import org.apache.doris.nereids.types.AggStateType;
+import org.apache.doris.nereids.types.DataType;
+
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Aggregate inputs into the nested function's serialized state.
+ */
+public class CombineCombinator extends AggregateFunction
+        implements ExplicitlyCastableSignature, AlwaysNotNullable, Combinator, 
RollUpTrait {

Review Comment:
   Acknowledged and deferred to a follow-up PR, as discussed in 
https://github.com/apache/doris/pull/66942#issuecomment-5392378919. This is 
shared by `_union` and `_combine`: correlated scalar-subquery rewriting needs a 
typed empty AggState expression and a common empty-input result contract. We 
will fix both combinators uniformly rather than special-casing `_combine` in 
this PR.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/combinator/CombineCombinator.java:
##########
@@ -0,0 +1,158 @@
+// 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.trees.expressions.functions.combinator;
+
+import org.apache.doris.catalog.BuiltinAggregateFunctions;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.FunctionRegistry;
+import org.apache.doris.catalog.FunctionSignature;
+import org.apache.doris.common.Pair;
+import org.apache.doris.nereids.exceptions.AnalysisException;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.OrderExpression;
+import 
org.apache.doris.nereids.trees.expressions.functions.AggCombinerFunctionBuilder;
+import org.apache.doris.nereids.trees.expressions.functions.AlwaysNotNullable;
+import org.apache.doris.nereids.trees.expressions.functions.BoundFunction;
+import 
org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
+import org.apache.doris.nereids.trees.expressions.functions.ExpressionTrait;
+import org.apache.doris.nereids.trees.expressions.functions.Function;
+import org.apache.doris.nereids.trees.expressions.functions.FunctionBuilder;
+import 
org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction;
+import 
org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunctionParams;
+import org.apache.doris.nereids.trees.expressions.functions.agg.RollUpTrait;
+import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
+import org.apache.doris.nereids.types.AggStateType;
+import org.apache.doris.nereids.types.DataType;
+
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * Aggregate inputs into the nested function's serialized state.
+ */
+public class CombineCombinator extends AggregateFunction
+        implements ExplicitlyCastableSignature, AlwaysNotNullable, Combinator, 
RollUpTrait {
+
+    private final AggregateFunction nested;
+    private final AggStateType returnType;
+
+    /** Constructor of CombineCombinator. */
+    public CombineCombinator(List<Expression> arguments, AggregateFunction 
nested) {
+        super(nested.getName() + AggCombinerFunctionBuilder.COMBINE_SUFFIX, 
arguments);
+        checkArguments(arguments, nested);
+        this.nested = Objects.requireNonNull(nested, "nested can not be null");
+        this.returnType = createReturnType(arguments, nested);
+    }
+
+    private CombineCombinator(AggregateFunctionParams functionParams, 
AggregateFunction nested) {
+        super(functionParams);
+        checkArguments(functionParams.arguments, nested);
+        this.nested = Objects.requireNonNull(nested, "nested can not be null");
+        this.returnType = createReturnType(functionParams.arguments, nested);
+    }
+
+    private static void checkArguments(List<Expression> arguments, 
AggregateFunction nested) {
+        if (arguments.isEmpty()) {
+            throw new AnalysisException(String.format(
+                    "%s_combine requires at least one argument", 
nested.getName()));
+        }
+        for (Expression argument : arguments) {
+            if (argument instanceof OrderExpression) {
+                throw new AnalysisException(String.format(
+                        "%s_combine doesn't support order by expression", 
nested.getName()));
+            }
+        }
+    }
+
+    private static AggStateType createReturnType(List<Expression> arguments, 
AggregateFunction nested) {
+        return new AggStateType(nested.getName(),
+                arguments.stream().map(ExpressionTrait::getDataType)
+                        .collect(ImmutableList.toImmutableList()),
+                arguments.stream().map(ExpressionTrait::nullable)
+                        .collect(ImmutableList.toImmutableList()),
+                
BuiltinAggregateFunctions.INSTANCE.aggFuncNameNullableMap.get(nested.getName()));
+    }
+
+    @Override
+    public CombineCombinator withChildren(List<Expression> children) {
+        return new CombineCombinator(getFunctionParams(children), nested);
+    }
+
+    @Override
+    public AggregateFunction withDistinctAndChildren(boolean distinct, 
List<Expression> children) {
+        if (distinct) {
+            throw new AnalysisException(getName() + " doesn't support 
DISTINCT");
+        }
+        return new CombineCombinator(getFunctionParams(false, children), 
nested);
+    }
+
+    @Override
+    public List<FunctionSignature> getSignatures() {
+        return nested.getSignatures().stream()
+                .map(signature -> signature.withReturnType(returnType))
+                .collect(ImmutableList.toImmutableList());
+    }
+
+    @Override
+    public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
+        return visitor.visitCombineCombinator(this, context);
+    }
+
+    @Override
+    public DataType getDataType() {
+        return returnType;
+    }
+
+    @Override
+    public AggregateFunction getNestedFunction() {
+        return nested;
+    }
+
+    @Override
+    protected List<DataType> intermediateTypes() {

Review Comment:
   Fixed. `CombineCombinator.supportAggregatePhase` now delegates to the nested 
aggregate function, so aggregates that disallow a phase remain disallowed after 
wrapping. The orthogonal bitmap functions are additionally rejected from 
AggState combinators, and the delegation behavior is covered by unit test.



-- 
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]

Reply via email to