yujun777 commented on code in PR #69456: URL: https://github.com/apache/doris/pull/69456#discussion_r4357355513
########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/TopKFunction.java: ########## @@ -0,0 +1,148 @@ +// 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.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.CustomSignature; +import org.apache.doris.nereids.trees.expressions.literal.ArrayLiteral; +import org.apache.doris.nereids.trees.expressions.literal.Literal; +import org.apache.doris.nereids.trees.expressions.literal.NullLiteral; +import org.apache.doris.nereids.trees.expressions.literal.StructLiteral; +import org.apache.doris.nereids.types.ArrayType; +import org.apache.doris.nereids.types.BigIntType; +import org.apache.doris.nereids.types.DataType; +import org.apache.doris.nereids.types.DecimalV2Type; +import org.apache.doris.nereids.types.DecimalV3Type; +import org.apache.doris.nereids.types.StructField; +import org.apache.doris.nereids.types.StructType; + +import com.google.common.collect.ImmutableList; + +import java.util.ArrayList; +import java.util.List; + +/** + * Shared implementation of {@code topk_min_count(x, k)} and {@code topk_max_count(x, k)}. + * + * <p>Both aggregates return {@code STRUCT<values ARRAY<STRUCT<value T, count BIGINT>>, bound T>}. + * {@code values} holds at most k distinct non-NULL values ordered by value (ascending for MIN, + * descending for MAX) with the number of rows equal to each value. {@code bound} is NULL when the + * group was not truncated; otherwise it is the k-th value and {@code bound IS NOT NULL} is the + * truncation flag. With {@code k = 0} no list is built and {@code bound} is the extreme value seen + * on this side (NULL when no non-NULL input was seen). + */ +public abstract class TopKCountFunction extends NotNullableAggregateFunction + implements CustomSignature, NullIgnoringAggregateFunction { + + /** Upper bound of k, shared with the BE implementation. */ + public static final long MAX_TOPK_K = 65536; + + protected TopKCountFunction(String name, boolean distinct, List<Expression> children) { + super(name, distinct, false, children); + } + + /** constructor for withChildren and reuse signature */ + protected TopKCountFunction(AggregateFunctionParams functionParams) { + super(functionParams); + } + + @Override + public void checkLegalityBeforeTypeCoercion() { + if (distinct) { + throw new AnalysisException(getName() + " does not support DISTINCT"); + } + if (!getArgument(1).isConstant()) { + throw new AnalysisException( + getName() + " requires second parameter must be a constant: " + toSql()); + } + if (!isSupportedValueType(getArgumentType(0))) { + throw new AnalysisException(getName() + " does not support type " + + getArgumentType(0).toSql()); + } + } + + @Override + public void checkLegalityAfterRewrite() { + checkLegalityBeforeTypeCoercion(); + Expression count = getArgument(1); + // A constant FE cannot fold is left to BE, which reads the value from the count column. + if (!(count instanceof Literal)) { Review Comment: Fixed in 1c73d49d491: `TopKFunction` no longer implements `NullIgnoringAggregateFunction`. The marker's contract (an input consisting only of rows whose argument is NULL behaves like an empty input) does not hold while a backend-evaluated count is validated per input row, so `InferAggNotNull` must not filter those rows away for these aggregates. Without the marker the global aggregate keeps its rows, `add` validates the count before skipping a NULL value, and an all-NULL input with a non-literal NULL count now reports `the count of min_topk and max_topk must be a non-null constant` instead of returning an empty array. NULL values of x are still ignored by `add` itself, so the aggregate's result is unchanged for valid counts. Covered in `query_p0/aggregate/min_max_topk`: the all-NULL global input with `bitmap_min(bitmap_from_string(''))` fails for both aggregates, and the same shape with the valid non-literal count `bitmap_min(bitmap_from_string('1'))` still returns the empty array. -- 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]
