From 13c174df4b6d0fcc0395f68467456cdf0d28b1cb Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 19 Mar 2025 09:56:13 +0800 Subject: [PATCH] branch-2.1: [fix](Nereids) fix substring with only one parameter #48957 (#49030) Cherry-picked from #48957 Co-authored-by: LiBinfeng --- .../expressions/functions/executable/StringArithmetic.java | 7 ++++--- .../fold_constant/fold_constant_string_arithmatic.groovy | 6 ++++++ 2 files changed, 10 insertions(+), 3 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 bc3317ea8d..00b674ace6 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 @@ -84,7 +84,7 @@ public class StringArithmetic { if (stringLength == 0) { return ""; } - int leftIndex = 0; + long leftIndex = 0; if (second < (- stringLength)) { return ""; } else if (second < 0) { @@ -94,7 +94,7 @@ public class StringArithmetic { } else { return ""; } - int rightIndex = 0; + long rightIndex = 0; if (third <= 0) { return ""; } else if ((third + leftIndex) > stringLength) { @@ -102,7 +102,8 @@ public class StringArithmetic { } else { rightIndex = third + leftIndex; } - return first.substring(leftIndex, rightIndex); + // left index and right index are in integer range because of definition, so we can safely cast it to int + return first.substring((int) leftIndex, (int) rightIndex); } /** diff --git a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy index 734094eab8..dce9000606 100644 --- a/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy +++ b/regression-test/suites/nereids_p0/expression/fold_constant/fold_constant_string_arithmatic.groovy @@ -653,6 +653,9 @@ suite("fold_constant_string_arithmatic") { testFoldConst("select substr('abcdef',3,-1)") testFoldConst("select substr('',3,-1)") testFoldConst("select substr('abcdef',3,10)") + testFoldConst("select substr('abcdef',-3)") + testFoldConst("select substr('abcdef',3)") + testFoldConst("select substr('',3)") // substring testFoldConst("select substring('1', 1, 1)") @@ -679,6 +682,9 @@ suite("fold_constant_string_arithmatic") { testFoldConst("select substring('Hello World', 1, 5)") testFoldConst("select substring('', 1, 5)") testFoldConst("select substring('Hello World', 1, 50)") + testFoldConst("select substring('abcdef',-3)") + testFoldConst("select substring('abcdef',3)") + testFoldConst("select substring('',3)") // substring_index testFoldConst("select substring_index('a,b,c', ',', 2)")