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

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


The following commit(s) were added to refs/heads/branch-3.1 by this push:
     new ae7acc2da91 branch-3.1: [fix](column label) generate label for string 
literal as MySQL #53457 (#53547)
ae7acc2da91 is described below

commit ae7acc2da9189652411c2da00b13938708e77db6
Author: morrySnow <[email protected]>
AuthorDate: Fri Jul 18 20:22:48 2025 +0800

    branch-3.1: [fix](column label) generate label for string literal as MySQL 
#53457 (#53547)
    
    cherry picked from #53457
---
 .../doris/nereids/parser/LogicalPlanBuilder.java   |  6 ++-
 .../parser/LogicalPlanBuilderAssistant.java        | 41 ++++++++--------
 .../parser/LogicalPlanBuilderAssistantTest.java    | 56 ++++++++++++++++++++++
 3 files changed, 81 insertions(+), 22 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
index 9713077d019..d847130b922 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
@@ -1631,14 +1631,16 @@ public class LogicalPlanBuilder extends 
DorisParserBaseVisitor<Object> {
             if (ctx.identifierOrText() == null) {
                 if (expression instanceof NamedExpression) {
                     return (NamedExpression) expression;
-                } else if (expression instanceof Literal) {
-                    return new Alias(expression);
                 } else {
                     int start = ctx.expression().start.getStartIndex();
                     int stop = ctx.expression().stop.getStopIndex();
                     String alias = ctx.start.getInputStream()
                             .getText(new 
org.antlr.v4.runtime.misc.Interval(start, stop));
                     if (expression instanceof Literal) {
+                        if (expression instanceof StringLikeLiteral) {
+                            alias = 
LogicalPlanBuilderAssistant.getStringLiteralAlias((
+                                    (StringLikeLiteral) 
expression).getStringValue());
+                        }
                         return new Alias(expression, alias, true);
                     } else {
                         return new UnboundAlias(expression, alias, true);
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilderAssistant.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilderAssistant.java
index 5b68ed63e45..7806201a75d 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilderAssistant.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilderAssistant.java
@@ -17,16 +17,12 @@
 
 package org.apache.doris.nereids.parser;
 
-import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral;
-import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral;
-import org.apache.doris.nereids.trees.expressions.literal.LargeIntLiteral;
-import org.apache.doris.nereids.trees.expressions.literal.Literal;
-import org.apache.doris.nereids.trees.expressions.literal.SmallIntLiteral;
-import org.apache.doris.nereids.trees.expressions.literal.TinyIntLiteral;
 import org.apache.doris.nereids.trees.plans.logical.LogicalCheckPolicy;
 import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
 
-import java.math.BigInteger;
+import com.google.common.collect.ImmutableSet;
+
+import java.util.Set;
 
 /**
  * Logical plan builder assistant for buildIn dialect and other dialect.
@@ -35,6 +31,10 @@ import java.math.BigInteger;
  */
 public class LogicalPlanBuilderAssistant {
 
+    // acording to MySQL's dev doc: 
https://dev.mysql.com/doc/refman/8.4/en/string-literals.html
+    private static final Set<Character> NEED_TRIM_CHARS_FOR_ALIAS
+            = ImmutableSet.of('\0', '\b', '\n', '\r', '\t', '\032', ' ');
+
     private LogicalPlanBuilderAssistant() {
     }
 
@@ -84,21 +84,22 @@ public class LogicalPlanBuilderAssistant {
     }
 
     /**
-     * Handle Integer literal by BigInteger.
+     * MySQL will trim any inviable char and blank at the beginning of the 
string,
+     * and stop at the first \0 if it exists. we follow the behavior.
+     *
+     * @param value the StringLikeLiteral value
+     * @return the alias generated from the value
      */
-    public static Literal handleIntegerLiteral(String value) {
-        BigInteger bigInt = new BigInteger(value);
-        if (BigInteger.valueOf(bigInt.byteValue()).equals(bigInt)) {
-            return new TinyIntLiteral(bigInt.byteValue());
-        } else if (BigInteger.valueOf(bigInt.shortValue()).equals(bigInt)) {
-            return new SmallIntLiteral(bigInt.shortValue());
-        } else if (BigInteger.valueOf(bigInt.intValue()).equals(bigInt)) {
-            return new IntegerLiteral(bigInt.intValue());
-        } else if (BigInteger.valueOf(bigInt.longValue()).equals(bigInt)) {
-            return new BigIntLiteral(bigInt.longValueExact());
-        } else {
-            return new LargeIntLiteral(bigInt);
+    public static String getStringLiteralAlias(String value) {
+        int i = 0;
+        while (i < value.length() && 
NEED_TRIM_CHARS_FOR_ALIAS.contains(value.charAt(i))) {
+            i++;
+        }
+        int j = i;
+        while (j < value.length() && value.charAt(j) != '\0') {
+            j++;
         }
+        return value.substring(i, j);
     }
 
     /**
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/LogicalPlanBuilderAssistantTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/LogicalPlanBuilderAssistantTest.java
new file mode 100644
index 00000000000..6dbcf0fa935
--- /dev/null
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/parser/LogicalPlanBuilderAssistantTest.java
@@ -0,0 +1,56 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.nereids.parser;
+
+import com.google.common.collect.Lists;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+public class LogicalPlanBuilderAssistantTest {
+
+    @Test
+    public void testGetStringLiteralAlias() {
+        String expected = "A";
+
+        List<String> values = Lists.newArrayList(
+                "\tA",
+                "\nA",
+                "\bA",
+                "\rA",
+                "\0A",
+                "\032A",
+                " A",
+                "\t\r\b\n\0\032    A",
+                "     \t\r\b\n\0\032    A",
+                "     \t\r\b\n\0\032    A\0BCDEF"
+        );
+
+        for (String value : values) {
+            Assertions.assertEquals(expected, 
LogicalPlanBuilderAssistant.getStringLiteralAlias(value));
+        }
+
+        Assertions.assertEquals("A\t\n\b\n\032    A",
+                
LogicalPlanBuilderAssistant.getStringLiteralAlias("A\t\n\b\n\032    A"));
+        Assertions.assertEquals("A\t\n\b",
+                
LogicalPlanBuilderAssistant.getStringLiteralAlias("A\t\n\b\0\n\032    A"));
+        Assertions.assertEquals("A\t\n\b\n\032    A",
+                
LogicalPlanBuilderAssistant.getStringLiteralAlias("A\t\n\b\n\032    A\0BCDEF"));
+    }
+}


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

Reply via email to