This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-4.1
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-4.1 by this push:
new b5d41a19f6a branch-4.1: [Fix](topn) Reject non-positive topn count
argument #63350 (#63408)
b5d41a19f6a is described below
commit b5d41a19f6aeb5ab484ca730002493a984d74cf9
Author: github-actions[bot]
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Wed May 20 18:41:39 2026 +0800
branch-4.1: [Fix](topn) Reject non-positive topn count argument #63350
(#63408)
Cherry-picked from #63350
Co-authored-by: linrrarity <[email protected]>
---
.../trees/expressions/functions/agg/TopN.java | 14 ++++++++++
.../trees/expressions/functions/agg/TopNArray.java | 14 ++++++++++
.../expressions/functions/agg/TopNWeighted.java | 14 ++++++++++
.../agg_function/topn/topn.groovy | 32 ++++++++++++++++++++++
4 files changed, 74 insertions(+)
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 5033b96e809..b223b130cc0 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
@@ -21,6 +21,7 @@ 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.Literal;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.IntegerType;
import org.apache.doris.nereids.types.VarcharType;
@@ -97,6 +98,19 @@ public class TopN extends NullableAggregateFunction
}
}
+ @Override
+ public void checkLegalityAfterRewrite() {
+ Expression topNCount = getArgument(1);
+ if (topNCount.isNullLiteral()) {
+ return;
+ }
+ if (!(topNCount instanceof Literal) || ((Literal)
topNCount).getDouble() <= 0) {
+ throw new AnalysisException(
+ "topn requires second parameter must be a constant
positive integer: "
+ + this.toSql());
+ }
+ }
+
/**
* withDistinctAndChildren.
*/
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 7a6f82db99c..bc370be9619 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
@@ -21,6 +21,7 @@ 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.Literal;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.ArrayType;
import org.apache.doris.nereids.types.IntegerType;
@@ -100,6 +101,19 @@ public class TopNArray extends NullableAggregateFunction
}
}
+ @Override
+ public void checkLegalityAfterRewrite() {
+ Expression topNCount = getArgument(1);
+ if (topNCount.isNullLiteral()) {
+ return;
+ }
+ if (!(topNCount instanceof Literal) || ((Literal)
topNCount).getDouble() <= 0) {
+ throw new AnalysisException(
+ "topn_array requires second parameter must be a constant
positive integer: "
+ + this.toSql());
+ }
+ }
+
/**
* withDistinctAndChildren.
*/
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 1ed1586f99f..9297e7f1ae0 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
@@ -21,6 +21,7 @@ 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.Literal;
import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
import org.apache.doris.nereids.types.ArrayType;
import org.apache.doris.nereids.types.BigIntType;
@@ -205,4 +206,17 @@ public class TopNWeighted extends NullableAggregateFunction
+ this.toSql());
}
}
+
+ @Override
+ public void checkLegalityAfterRewrite() {
+ Expression topNCount = getArgument(2);
+ if (topNCount.isNullLiteral()) {
+ return;
+ }
+ if (!(topNCount instanceof Literal) || ((Literal)
topNCount).getDouble() <= 0) {
+ throw new AnalysisException(
+ "topn_weighted requires third parameter must be a constant
positive integer: "
+ + this.toSql());
+ }
+ }
}
diff --git
a/regression-test/suites/nereids_function_p0/agg_function/topn/topn.groovy
b/regression-test/suites/nereids_function_p0/agg_function/topn/topn.groovy
index e29cafa29bd..004c758ff36 100644
--- a/regression-test/suites/nereids_function_p0/agg_function/topn/topn.groovy
+++ b/regression-test/suites/nereids_function_p0/agg_function/topn/topn.groovy
@@ -51,21 +51,53 @@ suite("topn") {
exception "errCode = 2"
}
+ test {
+ sql """select topn(id,-1) from test_topn;"""
+ exception "topn requires second parameter must be a constant positive
integer"
+ }
+
+ test {
+ sql """select topn(id,0) from test_topn;"""
+ exception "topn requires second parameter must be a constant positive
integer"
+ }
+
test {
sql """select topn_array(id,id) from test_topn;"""
exception "errCode = 2"
}
+
test {
sql """select topn_array(id,1,id) from test_topn;"""
exception "errCode = 2"
}
+ test {
+ sql """select topn_array(id,-1) from test_topn;"""
+ exception "topn_array requires second parameter must be a constant
positive integer"
+ }
+
+ test {
+ sql """select topn_array(id,0) from test_topn;"""
+ exception "topn_array requires second parameter must be a constant
positive integer"
+ }
+
test {
sql """select topn_weighted(id,id,id) from test_topn;"""
exception "errCode = 2"
}
+
test {
sql """select topn_weighted(id,id,1,id) from test_topn;"""
exception "errCode = 2"
}
+
+ test {
+ sql """select topn_weighted(id,id,-1) from test_topn;"""
+ exception "topn_weighted requires third parameter must be a constant
positive integer"
+ }
+
+ test {
+ sql """select topn_weighted(id,id,0) from test_topn;"""
+ exception "topn_weighted requires third parameter must be a constant
positive integer"
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]