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 abea8643c94 branch-4.2: [fix](nereids) Honor cast mode for string 
literal arithmetic #70518 (#70617)
abea8643c94 is described below

commit abea8643c94e52ba2ca775fee0be0dd81dad83d0
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Thu Nov 19 15:28:49 2026 +0800

    branch-4.2: [fix](nereids) Honor cast mode for string literal arithmetic 
#70518 (#70617)
    
    Cherry-picked from #70518
    
    Co-authored-by: TengJianPing <[email protected]>
---
 .../doris/nereids/util/TypeCoercionUtils.java      | 15 +----
 .../doris/nereids/util/TypeCoercionUtilsTest.java  | 43 ++++++++++++++
 .../nereids_p0/test_string_literal_arithmetic.out  | 37 ++++++++++++
 .../test_string_literal_arithmetic.groovy          | 68 ++++++++++++++++++++++
 .../suites/nereids_syntax_p0/cast.groovy           | 12 +++-
 5 files changed, 159 insertions(+), 16 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/TypeCoercionUtils.java 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/TypeCoercionUtils.java
index 7f40f38b79e..1c9b346f879 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/util/TypeCoercionUtils.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/util/TypeCoercionUtils.java
@@ -69,7 +69,6 @@ import 
org.apache.doris.nereids.trees.expressions.literal.MapLiteral;
 import org.apache.doris.nereids.trees.expressions.literal.NullLiteral;
 import org.apache.doris.nereids.trees.expressions.literal.Result;
 import org.apache.doris.nereids.trees.expressions.literal.SmallIntLiteral;
-import org.apache.doris.nereids.trees.expressions.literal.StringLikeLiteral;
 import org.apache.doris.nereids.trees.expressions.literal.StringLiteral;
 import org.apache.doris.nereids.trees.expressions.literal.TimeStampNsLiteral;
 import org.apache.doris.nereids.trees.expressions.literal.TimeV2Literal;
@@ -1027,18 +1026,8 @@ public class TypeCoercionUtils {
         // but if string literal could be cast to small type, we could use 
smaller type than double.
         binaryArithmetic = 
TypeCoercionUtils.processCharacterLiteralInBinaryOperator(binaryArithmetic);
 
-        // check string literal can cast to double
-        for (Expression expr : binaryArithmetic.children()) {
-            if (expr instanceof StringLikeLiteral) {
-                try {
-                    new BigDecimal(((StringLikeLiteral) 
expr).getStringValue());
-                } catch (NumberFormatException e) {
-                    throw new IllegalStateException(String.format(
-                            "string literal %s cannot be cast to double", 
expr.toSql()));
-                }
-            }
-        }
-
+        // Keep other strings for the CAST expressions below. CAST evaluation 
must use enable_strict_cast.
+        // For example, 'abc' + 1 returns NULL in non-strict mode and reports 
an error in strict mode.
         Expression left = binaryArithmetic.left();
         Expression right = binaryArithmetic.right();
         // 1. choose default numeric type for left and right
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/util/TypeCoercionUtilsTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/util/TypeCoercionUtilsTest.java
index 0e4da57781b..5dfde17af69 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/util/TypeCoercionUtilsTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/util/TypeCoercionUtilsTest.java
@@ -1018,6 +1018,49 @@ public class TypeCoercionUtilsTest {
                 TypeCoercionUtils.castIfNotSameType(new 
StringLiteral("string"), VarcharType.createVarcharType(100)));
     }
 
+    @Test
+    public void testStringArithmeticDefersValueConversionToCast() {
+        ConnectContext previousContext = ConnectContext.get();
+        ConnectContext connectContext = new ConnectContext();
+        connectContext.setThreadLocalInfo();
+        try {
+            // Analysis must keep a CAST in both modes. CAST evaluation 
returns NULL or an error.
+            // The same rule applies to STRING, VARCHAR, and CHAR literals on 
either side.
+            for (boolean strict : new boolean[] {false, true}) {
+                connectContext.getSessionVariable().enableStrictCast = strict;
+                for (Expression string : ImmutableList.of(new 
StringLiteral("abc"),
+                        new VarcharLiteral("abc"), new CharLiteral("abc", 3))) 
{
+                    IntegerLiteral one = new IntegerLiteral(1);
+                    Expression left = 
TypeCoercionUtils.processBinaryArithmetic(new Add(string, one));
+                    Expression right = 
TypeCoercionUtils.processBinaryArithmetic(new Add(one, string));
+                    Assertions.assertEquals(new Cast(string, 
DoubleType.INSTANCE), left.child(0));
+                    Assertions.assertEquals(new Cast(string, 
DoubleType.INSTANCE), right.child(1));
+                    Assertions.assertEquals(DoubleType.INSTANCE, 
left.getDataType());
+                    Assertions.assertEquals(DoubleType.INSTANCE, 
right.getDataType());
+                }
+            }
+        } finally {
+            ConnectContext.remove();
+            if (previousContext != null) {
+                previousContext.setThreadLocalInfo();
+            }
+        }
+    }
+
+    @Test
+    public void testStringArithmeticKeepsNumericTypeSelection() {
+        // An integer string must still use integer arithmetic. It must not 
become DOUBLE.
+        Expression integer = TypeCoercionUtils.processBinaryArithmetic(
+                new Add(new StringLiteral("1"), new IntegerLiteral(1)));
+        Assertions.assertEquals(BigIntType.INSTANCE, integer.getDataType());
+
+        // DOUBLE accepts spaces around a number. BigDecimal rejects this 
input before CAST runs.
+        StringLiteral spaced = new StringLiteral(" 1 ");
+        Expression floating = TypeCoercionUtils.processBinaryArithmetic(new 
Add(spaced, new IntegerLiteral(1)));
+        Assertions.assertEquals(new Cast(spaced, DoubleType.INSTANCE), 
floating.child(0));
+        Assertions.assertEquals(DoubleType.INSTANCE, floating.getDataType());
+    }
+
     @Test
     public void testDecimalArithmetic() {
         Multiply multiply = new Multiply(new DecimalLiteral(new 
BigDecimal("987654.321")),
diff --git a/regression-test/data/nereids_p0/test_string_literal_arithmetic.out 
b/regression-test/data/nereids_p0/test_string_literal_arithmetic.out
new file mode 100644
index 00000000000..2c726aba922
--- /dev/null
+++ b/regression-test/data/nereids_p0/test_string_literal_arithmetic.out
@@ -0,0 +1,37 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !invalid_add --
+\N     \N
+
+-- !invalid_shared --
+\N     \N      \N      \N      \N      \N      \N      \N      \N      \N      
\N      \N
+
+-- !existing_paths --
+\N     \N      \N
+
+-- !string_pairs --
+\N     \N
+
+-- !valid_numbers --
+2      2.5     11      2
+
+-- !valid_numbers --
+2      2.5     11      2
+
+-- !invalid_add --
+\N     \N
+
+-- !invalid_shared --
+\N     \N      \N      \N      \N      \N      \N      \N      \N      \N      
\N      \N
+
+-- !existing_paths --
+\N     \N      \N
+
+-- !string_pairs --
+\N     \N
+
+-- !valid_numbers --
+2      2.5     11      2
+
+-- !valid_numbers --
+2      2.5     11      2
+
diff --git 
a/regression-test/suites/nereids_p0/test_string_literal_arithmetic.groovy 
b/regression-test/suites/nereids_p0/test_string_literal_arithmetic.groovy
new file mode 100644
index 00000000000..5cb9f8d4e37
--- /dev/null
+++ b/regression-test/suites/nereids_p0/test_string_literal_arithmetic.groovy
@@ -0,0 +1,68 @@
+// 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.
+
+suite("test_string_literal_arithmetic") {
+    // Use literals only. No table or stored row is required. The original 
failure is 'abc' + 1.
+    // Save the variables so this suite does not change the next suite's 
execution mode.
+    def originalSettings = ["enable_strict_cast", "enable_fold_constant_by_be",
+            "debug_skip_fold_constant", "enable_sql_cache"].collectEntries { 
name ->
+        [(name): sql("show variables like '${name}'")[0][1]]
+    }
+    try {
+        // Disable the cache so each query tests the selected CAST and 
execution modes.
+        sql "set enable_sql_cache=false"
+        sql "set enable_fold_constant_by_be=false"
+        for (skipFold in [false, true]) {
+            // false tests FE constant folding. true tests BE evaluation of 
the same literals.
+            sql "set debug_skip_fold_constant=${skipFold}"
+            sql "set enable_strict_cast=false"
+
+            // An invalid numeric string becomes NULL. Check both operand 
positions.
+            order_qt_invalid_add "select 'abc' + 1, 1 + 'abc'"
+            // These operators share the type conversion code used by addition.
+            order_qt_invalid_shared """select 'abc' - 1, 1 - 'abc', 'abc' * 1, 
1 * 'abc',
+                    'abc' % 1, 1 % 'abc', 'abc' & 1, 1 & 'abc',
+                    'abc' | 1, 1 | 'abc', 'abc' ^ 1, 1 ^ 'abc'"""
+            // Division and explicit CAST already use the required conversion 
rules.
+            order_qt_existing_paths "select 'abc' / 1, 'abc' div 1, cast('abc' 
as double) + 1"
+            // Two string operands and an empty string must also reach CAST 
evaluation.
+            order_qt_string_pairs "select 'abc' + '1', '' + 1"
+
+            for (strict in [false, true]) {
+                sql "set enable_strict_cast=${strict}"
+                // Keep integer type selection. Fractional and exponent forms 
use DOUBLE.
+                // Spaces are valid for DOUBLE, but the old BigDecimal check 
rejected them.
+                order_qt_valid_numbers "select '1' + 1, '1.5' + 1, '1e1' + 1, 
' 1 ' + 1"
+            }
+
+            // Strict mode must still fail. Match the input value because FE 
and BE use
+            // different CAST error messages. Test each operator and operand 
position alone.
+            for (op in ["+", "-", "*", "%", "&", "|", "^"]) {
+                test {
+                    sql "select 'abc' ${op} 1"
+                    exception "abc"
+                }
+                test {
+                    sql "select 1 ${op} 'abc'"
+                    exception "abc"
+                }
+            }
+        }
+    } finally {
+        originalSettings.each { name, value -> sql "set ${name}=${value}" }
+    }
+}
diff --git a/regression-test/suites/nereids_syntax_p0/cast.groovy 
b/regression-test/suites/nereids_syntax_p0/cast.groovy
index 30e57d58e60..428f58f5eaa 100644
--- a/regression-test/suites/nereids_syntax_p0/cast.groovy
+++ b/regression-test/suites/nereids_syntax_p0/cast.groovy
@@ -186,9 +186,15 @@ suite("cast") {
 
     qt_group31 "select count(*) from cast_test where (k11='2015-03-13 
12:36:38' or k11 = '2000-01-01 00:00:00') and k5 is not null group by k1%2, 
k2%2, k3%3, k4%3, k11%2 order by count(*)"
 
-    test {
-        sql "select true + 1 + 'x'"
-        exception "string literal 'x' cannot be cast to double"
+    def originalEnableStrictCast = sql("select @@enable_strict_cast")[0][0]
+    try {
+        sql "set enable_strict_cast = true"
+        test {
+            sql "select true + 1 + 'x'"
+            exception "can't cast to double in strict mode"
+        }
+    } finally {
+        sql "set enable_strict_cast = ${originalEnableStrictCast}"
     }
 
     qt_sql_test_DecimalV3_mode """select cast(1 as DECIMALV3(1, 0)) % 2.1;""";


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

Reply via email to