github-actions[bot] commented on code in PR #60412:
URL: https://github.com/apache/doris/pull/60412#discussion_r3053019571


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/StringArithmetic.java:
##########
@@ -1123,6 +1123,74 @@ public static Expression soundex(StringLikeLiteral 
first) {
         return castStringLikeLiteral(first, result);
     }
 
+    /**
+     * Executable arithmetic functions levenshtein
+     */
+    @ExecFunction(name = "levenshtein")
+    public static Expression levenshtein(StringLikeLiteral first, 
StringLikeLiteral second) {
+        int[] left = first.getValue().codePoints().toArray();
+        int[] right = second.getValue().codePoints().toArray();
+
+        if (right.length > left.length) {
+            int[] tmp = left;
+            left = right;
+            right = tmp;
+        }
+
+        int m = left.length;
+        int n = right.length;
+        if (n == 0) {
+            return new IntegerLiteral(m);
+        }
+        if (m == 0) {
+            return new IntegerLiteral(n);
+        }
+
+        int[] prev = new int[n + 1];
+        int[] curr = new int[n + 1];
+        for (int j = 0; j <= n; j++) {
+            prev[j] = j;
+        }
+
+        for (int i = 1; i <= m; i++) {
+            curr[0] = i;
+            int leftChar = left[i - 1];
+            for (int j = 1; j <= n; j++) {
+                int cost = leftChar == right[j - 1] ? 0 : 1;
+                int insertCost = curr[j - 1] + 1;
+                int deleteCost = prev[j] + 1;
+                int replaceCost = prev[j - 1] + cost;
+                curr[j] = Math.min(insertCost, Math.min(deleteCost, 
replaceCost));
+            }
+            int[] tmp = prev;
+            prev = curr;
+            curr = tmp;
+        }
+
+        return new IntegerLiteral(prev[n]);
+    }
+
+    /**
+     * Executable arithmetic functions hamming_distance
+     */
+    @ExecFunction(name = "hamming_distance")

Review Comment:
   This adds the FE constant-folding implementation for `hamming_distance` (and 
the PR also adds the new Nereids scalar bindings), but the patch only updates 
`query_p0` regressions. I couldn't find any corresponding `nereids_*` 
regression changes in this PR, so the Nereids bind/fold path is still 
unvalidated. Please add a focused Nereids regression case for the new functions 
as well.



##########
regression-test/suites/query_p0/sql_functions/string_functions/test_string_all.groovy:
##########
@@ -753,6 +753,64 @@ suite("string_functions_all") {
     testFoldConst("SELECT soundex('R@b-e123rt'), soundex('Robert');")
     // SOUNDEX tests with non-ASCII characters - Skipped (not supported)
 
+    // LEVENSHTEIN tests
+    qt_levenshtein_331 "SELECT levenshtein('', ''), levenshtein('kitten', 
'sitting'), levenshtein('flaw', 'lawn'), levenshtein('你好', '你们'), 
levenshtein('数据库', '数据');"
+    testFoldConst("SELECT levenshtein('', ''), levenshtein('kitten', 
'sitting'), levenshtein('flaw', 'lawn'), levenshtein('你好', '你们'), 
levenshtein('数据库', '数据');")
+    qt_levenshtein_332 "SELECT levenshtein('abc', 'abc'), levenshtein('abc', 
''), levenshtein('', 'abc'), levenshtein(NULL, 'abc'), levenshtein('abc', 
NULL);"
+    testFoldConst("SELECT levenshtein('abc', 'abc'), levenshtein('abc', ''), 
levenshtein('', 'abc'), levenshtein(NULL, 'abc'), levenshtein('abc', NULL);")
+    qt_levenshtein_333 "SELECT levenshtein('abcd', 'abdc'), levenshtein('你好呀', 
'你好'), levenshtein('a你b', 'a们b');"
+    testFoldConst("SELECT levenshtein('abcd', 'abdc'), levenshtein('你好呀', 
'你好'), levenshtein('a你b', 'a们b');")
+    sql """DROP TABLE IF EXISTS string_distance_lv_test"""
+    sql """
+        CREATE TABLE IF NOT EXISTS string_distance_lv_test (
+            id int,
+            s1 VARCHAR,
+            s2 VARCHAR
+        )
+        DISTRIBUTED BY HASH(id) BUCKETS 1
+        PROPERTIES ("replication_num"="1")
+    """
+    sql """
+        insert into string_distance_lv_test values
+        (1, 'kitten', 'sitting'),
+        (2, 'abc', 'abc'),
+        (3, '数据库', '数据'),
+        (4, null, 'abc'),
+        (5, '你好呀', '你好'),
+        (6, 'abcd', 'abdc'),
+        (7, '', '数据库')
+    """
+    qt_levenshtein_tbl "SELECT id, levenshtein(s1, s2) FROM 
string_distance_lv_test ORDER BY id"
+
+    // HAMMING_DISTANCE tests
+    qt_hamming_distance_333 "SELECT hamming_distance('', ''), 
hamming_distance('abc', 'abc'), hamming_distance('abc', 'abd'), 
hamming_distance('你好', '你们');"

Review Comment:
   Both the BE implementation and the FE `@ExecFunction` add a special 
unequal-length error path for `hamming_distance`, but this new block only 
covers success/null cases. Per the regression-test rules in `AGENTS.md`, this 
should also have a `test { sql ...; exception ... }` case so that the error 
semantics do not regress silently.



-- 
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]

Reply via email to