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 cd8a068482e branch-4.1: [fix](search) Validate ngram search gram 
number #67749 (#68418)
cd8a068482e is described below

commit cd8a068482e132621fbdc081afd2430ae034a0d1
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Thu Sep 24 09:35:57 2026 +0800

    branch-4.1: [fix](search) Validate ngram search gram number #67749 (#68418)
    
    Cherry-picked from #67749
    
    Co-authored-by: Mryange <[email protected]>
---
 .../expressions/functions/scalar/NgramSearch.java  | 24 +++++++++++++++-------
 .../string_functions/test_string_function.groovy   |  8 ++++++++
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/NgramSearch.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/NgramSearch.java
index f1a0524ef5c..396244657bc 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/NgramSearch.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/NgramSearch.java
@@ -22,6 +22,7 @@ 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.functions.PropagateNullable;
+import org.apache.doris.nereids.trees.expressions.literal.IntegerLikeLiteral;
 import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
 import org.apache.doris.nereids.types.DoubleType;
 import org.apache.doris.nereids.types.IntegerType;
@@ -47,19 +48,28 @@ public class NgramSearch extends ScalarFunction
      */
     public NgramSearch(Expression arg0, Expression arg1, Expression arg2) {
         super("ngram_search", arg0, arg1, arg2);
-        if (!(arg1.isConstant())) {
+    }
+
+    /** constructor for withChildren and reuse signature */
+    private NgramSearch(ScalarFunctionParams functionParams) {
+        super(functionParams);
+    }
+
+    @Override
+    public void checkLegalityBeforeTypeCoercion() {
+        if (!child(1).isConstant()) {
             throw new AnalysisException(
                     "ngram_search(text,pattern,gram_num): pattern support 
const value only.");
         }
-        if (!(arg2.isConstant())) {
+        Expression gramNum = child(2);
+        if (!(gramNum instanceof IntegerLikeLiteral)) {
             throw new AnalysisException(
                     "ngram_search(text,pattern,gram_num): gram_num support 
const value only.");
         }
-    }
-
-    /** constructor for withChildren and reuse signature */
-    private NgramSearch(ScalarFunctionParams functionParams) {
-        super(functionParams);
+        if (((IntegerLikeLiteral) gramNum).getIntValue() <= 0) {
+            throw new AnalysisException(
+                    "ngram_search(text,pattern,gram_num): gram_num must be a 
positive constant.");
+        }
     }
 
     /**
diff --git 
a/regression-test/suites/query_p0/sql_functions/string_functions/test_string_function.groovy
 
b/regression-test/suites/query_p0/sql_functions/string_functions/test_string_function.groovy
index 734d802695c..7fbeb8c7df6 100644
--- 
a/regression-test/suites/query_p0/sql_functions/string_functions/test_string_function.groovy
+++ 
b/regression-test/suites/query_p0/sql_functions/string_functions/test_string_function.groovy
@@ -443,6 +443,14 @@ suite("test_string_function", "arrow_flight_sql") {
     qt_ngram_search2 """select ngram_search('abc','abc1313131',3); """
     qt_ngram_search3 """select ngram_search('abc1313131','abc1313131',3); """
     qt_ngram_search3 """select ngram_search('1313131','abc1313131',3); """
+    test {
+        sql "select ngram_search('abc', 'abc', 0);"
+        exception "gram_num must be a positive constant"
+    }
+    test {
+        sql "select ngram_search('abc', 'abc', -1);"
+        exception "gram_num must be a positive constant"
+    }
     
 
     sql "drop table if exists test_function_ngram_search;";


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

Reply via email to