This is an automated email from the ASF dual-hosted git repository.

Mryange pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new e0b98f679ad [fix](search) Validate ngram search gram number (#67749)
e0b98f679ad is described below

commit e0b98f679ad1594ca3756f2b4afe05bcf6aab19c
Author: Mryange <[email protected]>
AuthorDate: Mon Sep 14 13:08:26 2026 +0800

    [fix](search) Validate ngram search gram number (#67749)
    
    `ngram_search` accepted zero or negative `gram_num` values and deferred
    the failure to execution. The FE now requires `pattern` and `gram_num`
    to be constants and validates that `gram_num` is a positive integer,
    with regression coverage for invalid values.
    
    ### Release note
    
    None
    
    ### Check List (For Author)
    
    - Test <!-- At least one of them must be included. -->
        - [ ] Regression test
        - [ ] Unit Test
        - [ ] Manual test (add detailed scripts or steps below)
        - [ ] No need to test or manual test. Explain why:
    - [ ] This is a refactor/code format and no logic has been changed.
            - [ ] Previous test can cover this change.
            - [ ] No code files have been changed.
            - [ ] Other reason <!-- Add your reason?  -->
    
    - Behavior changed:
        - [ ] No.
        - [ ] Yes. <!-- Explain the behavior change -->
    
    - Does this need documentation?
        - [ ] No.
    - [ ] Yes. <!-- Add document PR link here. eg:
    https://github.com/apache/doris-website/pull/1214 -->
    
    ### Check List (For Reviewer who merge this PR)
    
    - [ ] Confirm the release note
    - [ ] Confirm test cases
    - [ ] Confirm document
    - [ ] Add branch pick label <!-- Add branch pick label that this PR
    should merge into -->
---
 .../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 9db9777b1c8..30aa358015b 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
@@ -536,6 +536,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