Skip to content

Commit 1c73d49

Browse files
committed
[fix](function) Stop InferAggNotNull from hiding an invalid top-K count
min_topk/max_topk implemented NullIgnoringAggregateFunction, whose contract says that an input consisting only of rows with a NULL argument must behave like an empty input. That is false when the count is a constant expression the FE cannot fold: BE validates it per input row, so an all-NULL value column still has to reach the aggregate. InferAggNotNull inserted an x IS NOT NULL filter below the global aggregate instead, folded the input to empty, and the invalid count silently returned an empty array. Key changes: - Drop the marker from TopKFunction; NULL x values are still skipped by add, which runs after the count validation - Cover the all-NULL global input with a non-literal NULL count (now fails) and with a valid non-literal count (still returns an empty array) Unit Test: - regression-test/suites/query_p0/aggregate/min_max_topk.groovy
1 parent 51bd4e1 commit 1c73d49

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

‎fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/agg/TopKFunction.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,15 @@
4545
* equal to each value. There is no truncation flag; the caller infers it from the length (a list of
4646
* exactly k entries is conservatively treated as possibly truncated). With {@code k = 0} the array
4747
* holds at most the extreme value and its row count.
48+
*
49+
* <p>NULL values of x are skipped by the implementation, but this class deliberately does not
50+
* implement {@code NullIgnoringAggregateFunction}: a count expression the FE cannot fold is
51+
* validated by BE on every input row, so an all-NULL input must still reach the aggregate to
52+
* report a NULL count. Filtering those rows away (as {@code InferAggNotNull} would) turns that
53+
* error into an empty result, which breaks the null-ignoring result-invariance contract.
4854
*/
4955
public abstract class TopKFunction extends NotNullableAggregateFunction
50-
implements CustomSignature, NullIgnoringAggregateFunction {
56+
implements CustomSignature {
5157

5258
/** Upper bound of k, shared with the BE implementation. */
5359
public static final long MAX_TOPK_K = 65536;

‎regression-test/data/query_p0/aggregate/min_max_topk.out‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,6 @@ e 0 0 [{"value":0, "count":5}] [{"value":0, "count":5}]
115115
-- !max_k_accepted --
116116
[{"value":1, "count":1}] [{"value":1, "count":1}]
117117

118+
-- !nonliteral_count_all_null --
119+
[]
120+

‎regression-test/suites/query_p0/aggregate/min_max_topk.groovy‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,28 @@ suite("min_max_topk") {
195195
exception "min_topk requires a non-null constant count"
196196
}
197197

198+
// A count the FE cannot fold is validated by BE on every input row. Over a global aggregate
199+
// the optimizer must therefore keep the rows even when x is NULL everywhere; filtering them
200+
// away would silently return an empty array instead of reporting the NULL count.
201+
test {
202+
sql """SELECT min_topk(v, bitmap_min(bitmap_from_string('')))
203+
FROM (SELECT CAST(NULL AS INT) AS v UNION ALL SELECT CAST(NULL AS INT) AS v) t"""
204+
exception "the count of min_topk and max_topk must be a non-null constant"
205+
}
206+
207+
test {
208+
sql """SELECT max_topk(v, bitmap_min(bitmap_from_string('')))
209+
FROM (SELECT CAST(NULL AS INT) AS v UNION ALL SELECT CAST(NULL AS INT) AS v) t"""
210+
exception "the count of min_topk and max_topk must be a non-null constant"
211+
}
212+
213+
// The same non-literal path with a valid count keeps returning the empty array for all-NULL
214+
// input, so the row visibility change only affects the invalid-count error.
215+
order_qt_nonliteral_count_all_null """
216+
SELECT min_topk(v, bitmap_min(bitmap_from_string('1')))
217+
FROM (SELECT CAST(NULL AS INT) AS v UNION ALL SELECT CAST(NULL AS INT) AS v) t
218+
"""
219+
198220
test {
199221
sql "SELECT min_topk(DISTINCT i, 2) FROM min_max_topk_t"
200222
exception "min_topk does not support DISTINCT"

0 commit comments

Comments
 (0)