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 bc3317ea8d7ab7..00b674ace6a30a 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 @@ private static String substringImpl(String first, int second, int third) { if (stringLength == 0) { return ""; } - int leftIndex = 0; + long leftIndex = 0; if (second < (- stringLength)) { return ""; } else if (second < 0) { @@ -94,7 +94,7 @@ private static String substringImpl(String first, int second, int third) { } else { return ""; } - int rightIndex = 0; + long rightIndex = 0; if (third <= 0) { return ""; } else if ((third + leftIndex) > stringLength) { @@ -102,7 +102,8 @@ private static String substringImpl(String first, int second, int third) { } 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 734094eab86aac..dce90006065fa3 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)")