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

yiguolei pushed a commit to branch branch-4.2
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-4.2 by this push:
     new 4d0d376fab7 branch-4.2: [fix](fe) Preserve Unicode semantics in string 
constant folding (#71429)
4d0d376fab7 is described below

commit 4d0d376fab701edd60334f1e25afe02485ab2a70
Author: linrrarity <[email protected]>
AuthorDate: Sat Dec 5 10:37:13 2026 +0800

    branch-4.2: [fix](fe) Preserve Unicode semantics in string constant folding 
(#71429)
    
    pick: https://github.com/apache/doris/pull/71251
---
 .../functions/executable/StringArithmetic.java     | 86 ++++++++--------------
 .../functions/executable/StringArithmeticTest.java | 23 ++++++
 2 files changed, 55 insertions(+), 54 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/StringArithmetic.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/StringArithmetic.java
index 98a79583168..ba33c5caf34 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/StringArithmetic.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/StringArithmetic.java
@@ -45,10 +45,8 @@ import org.apache.doris.nereids.types.ArrayType;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 
-import java.io.ByteArrayOutputStream;
 import java.io.UnsupportedEncodingException;
 import java.math.BigInteger;
-import java.net.URLDecoder;
 import java.net.URLEncoder;
 import java.nio.ByteBuffer;
 import java.nio.charset.CharacterCodingException;
@@ -173,24 +171,23 @@ public class StringArithmetic {
     }
 
     private static String trimInImpl(String first, String second, boolean 
left, boolean right) {
-        StringBuilder result = new StringBuilder(first);
-
-        if (left) {
-            int start = 0;
-            while (start < result.length() && 
second.indexOf(result.charAt(start)) != -1) {
-                start++;
+        int start = 0;
+        int end = first.length();
+        while (left && start < end) {
+            int codePoint = first.codePointAt(start);
+            if (second.indexOf(codePoint) < 0) {
+                break;
             }
-            result.delete(0, start);
+            start += Character.charCount(codePoint);
         }
-        if (right) {
-            int end = result.length();
-            while (end > 0 && second.indexOf(result.charAt(end - 1)) != -1) {
-                end--;
+        while (right && start < end) {
+            int codePoint = first.codePointBefore(end);
+            if (second.indexOf(codePoint) < 0) {
+                break;
             }
-            result.delete(end, result.length());
+            end -= Character.charCount(codePoint);
         }
-
-        return result.toString();
+        return first.substring(start, end);
     }
 
     /**
@@ -1112,57 +1109,38 @@ public class StringArithmetic {
      */
     @ExecFunction(name = "url_decode")
     public static Expression urlDecode(StringLikeLiteral first) {
-        if (!isValidUtf8AfterUrlDecode(first.getValue())) {
-            // String literals cannot preserve an invalid UTF-8 byte sequence. 
Let BE evaluate it
-            // instead of folding the replacement characters produced by 
java.net.URLDecoder.
-            throw new IllegalArgumentException("URL-decoded value is not valid 
UTF-8");
-        }
-        try {
-            return castStringLikeLiteral(first, 
URLDecoder.decode(first.getValue(), StandardCharsets.UTF_8.name()));
-        } catch (UnsupportedEncodingException e) {
-            throw new RuntimeException(e);
-        }
-    }
-
-    private static boolean isValidUtf8AfterUrlDecode(String value) {
-        ByteArrayOutputStream decodedBytes = new 
ByteArrayOutputStream(value.length());
-        int index = 0;
-        while (index < value.length()) {
-            char current = value.charAt(index);
+        byte[] bytes = first.getValue().getBytes(StandardCharsets.UTF_8);
+        int decodedLength = 0;
+        for (int index = 0; index < bytes.length; index++) {
+            byte current = bytes[index];
             if (current == '%') {
-                if (index + 2 >= value.length()) {
-                    // Preserve URLDecoder's existing malformed-escape 
handling.
-                    return true;
+                if (bytes.length - index < 3) {
+                    throw new IllegalArgumentException("Incomplete URL 
escape");
                 }
-                int high = Character.digit(value.charAt(index + 1), 16);
-                int low = Character.digit(value.charAt(index + 2), 16);
+                // Non-ASCII UTF-8 bytes are negative, which Character.digit 
rejects.
+                int high = Character.digit(bytes[index + 1], 16);
+                int low = Character.digit(bytes[index + 2], 16);
                 if (high < 0 || low < 0) {
-                    // Preserve URLDecoder's existing malformed-escape 
handling.
-                    return true;
+                    throw new IllegalArgumentException("Invalid URL escape");
                 }
-                decodedBytes.write((high << 4) + low);
-                index += 3;
+                bytes[decodedLength++] = (byte) ((high << 4) | low);
+                index += 2;
             } else if (current == '+') {
-                decodedBytes.write(' ');
-                index++;
+                bytes[decodedLength++] = ' ';
             } else {
-                int start = index;
-                while (index < value.length() && value.charAt(index) != '%' && 
value.charAt(index) != '+') {
-                    index++;
-                }
-                byte[] originalBytes = value.substring(start, 
index).getBytes(StandardCharsets.UTF_8);
-                decodedBytes.write(originalBytes, 0, originalBytes.length);
+                bytes[decodedLength++] = current;
             }
         }
 
         try {
-            StandardCharsets.UTF_8.newDecoder()
+            String decoded = StandardCharsets.UTF_8.newDecoder()
                     .onMalformedInput(CodingErrorAction.REPORT)
                     .onUnmappableCharacter(CodingErrorAction.REPORT)
-                    .decode(ByteBuffer.wrap(decodedBytes.toByteArray()));
-            return true;
+                    .decode(ByteBuffer.wrap(bytes, 0, 
decodedLength)).toString();
+            return castStringLikeLiteral(first, decoded);
         } catch (CharacterCodingException e) {
-            return false;
+            // BE can return raw bytes that a FE string literal cannot 
preserve.
+            throw new IllegalArgumentException("URL-decoded value is not valid 
UTF-8", e);
         }
     }
 
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/functions/executable/StringArithmeticTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/functions/executable/StringArithmeticTest.java
index 22f0292f7c9..ea6efd65643 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/functions/executable/StringArithmeticTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/expressions/functions/executable/StringArithmeticTest.java
@@ -19,6 +19,9 @@ package 
org.apache.doris.nereids.trees.expressions.functions.executable;
 
 import org.apache.doris.nereids.trees.expressions.Expression;
 import org.apache.doris.nereids.trees.expressions.ExpressionEvaluator;
+import org.apache.doris.nereids.trees.expressions.functions.scalar.LtrimIn;
+import org.apache.doris.nereids.trees.expressions.functions.scalar.RtrimIn;
+import org.apache.doris.nereids.trees.expressions.functions.scalar.TrimIn;
 import org.apache.doris.nereids.trees.expressions.functions.scalar.UrlDecode;
 import org.apache.doris.nereids.trees.expressions.literal.DoubleLiteral;
 import org.apache.doris.nereids.trees.expressions.literal.FloatLiteral;
@@ -81,6 +84,25 @@ class StringArithmeticTest {
         Assertions.assertEquals(2, result.getValue());
     }
 
+    @Test
+    void testTrimInMatchesWholeCodePoints() {
+        ExpressionEvaluator evaluator = ExpressionEvaluator.INSTANCE;
+        Assertions.assertEquals(new StringLiteral("πŸ˜€x"),
+                evaluator.eval(new LtrimIn(new StringLiteral("πŸ˜πŸ˜€x"), new 
StringLiteral("😁"))));
+        Assertions.assertEquals(new StringLiteral("xπŸ¨€"),
+                evaluator.eval(new RtrimIn(new StringLiteral("xπŸ¨€πŸ˜€"), new 
StringLiteral("πŸ˜€"))));
+        Assertions.assertEquals(new StringLiteral("πŸ˜€"),
+                evaluator.eval(new TrimIn(new StringLiteral("πŸ˜πŸ˜€πˆ€"), new 
StringLiteral("πŸ˜πˆ€"))));
+    }
+
+    @Test
+    void testUrlDecodeDoesNotFoldInvalidPercentEscapes() {
+        for (String value : new String[] {"%οΌ”οΌ‘", "%+1", "%1g", "%"}) {
+            UrlDecode urlDecode = new UrlDecode(new StringLiteral(value));
+            Assertions.assertSame(urlDecode, 
ExpressionEvaluator.INSTANCE.eval(urlDecode), value);
+        }
+    }
+
     @Test
     void testUrlDecodeDoesNotFoldInvalidUtf8() {
         String[] invalidUtf8Values = {"%80", "%C0%AF", "%E0%80%80", 
"%ED%A0%80", "%FF"};
@@ -94,6 +116,7 @@ class StringArithmeticTest {
     void testUrlDecodeStillFoldsValidUtf8() {
         assertUrlDecodeValue("%E4%B8%AD+text", "δΈ­ text");
         assertUrlDecodeValue("%EF%BF%BD", "οΏ½");
+        assertUrlDecodeValue("δΈ­+%f0%9F%98%80%2B", "δΈ­ πŸ˜€+");
     }
 
     private void assertUrlDecodeValue(String encoded, String expected) {


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

Reply via email to