diff --git a/core/src/main/java/org/opensearch/sql/expression/text/TextFunction.java b/core/src/main/java/org/opensearch/sql/expression/text/TextFunction.java index 57521acf59..372540b4e9 100644 --- a/core/src/main/java/org/opensearch/sql/expression/text/TextFunction.java +++ b/core/src/main/java/org/opensearch/sql/expression/text/TextFunction.java @@ -286,12 +286,18 @@ private static ExprValue exprSubStr(String str, int start, int len) { } private static ExprValue exprRight(ExprValue str, ExprValue len) { - return new ExprStringValue(str.stringValue().substring( - str.stringValue().length() - len.integerValue())); + if (len.integerValue() <= 0) { + return new ExprStringValue(""); + } + String stringValue = str.stringValue(); + int left = Math.max(stringValue.length() - len.integerValue(), 0); + return new ExprStringValue(str.stringValue().substring(left)); } private static ExprValue exprLeft(ExprValue expr, ExprValue length) { - return new ExprStringValue(expr.stringValue().substring(0, length.integerValue())); + String stringValue = expr.stringValue(); + int right = length.integerValue(); + return new ExprStringValue(stringValue.substring(0, Math.min(right, stringValue.length()))); } private static ExprValue exprAscii(ExprValue expr) { diff --git a/core/src/test/java/org/opensearch/sql/expression/text/TextFunctionTest.java b/core/src/test/java/org/opensearch/sql/expression/text/TextFunctionTest.java index 13f4d43980..f7f7a7749b 100644 --- a/core/src/test/java/org/opensearch/sql/expression/text/TextFunctionTest.java +++ b/core/src/test/java/org/opensearch/sql/expression/text/TextFunctionTest.java @@ -291,6 +291,18 @@ void right() { assertEquals(STRING, expression.type()); assertEquals("rbar", eval(expression).stringValue()); + expression = dsl.right(DSL.literal("foo"), DSL.literal(10)); + assertEquals(STRING, expression.type()); + assertEquals("foo", eval(expression).value()); + + expression = dsl.right(DSL.literal("foo"), DSL.literal(0)); + assertEquals(STRING, expression.type()); + assertEquals("", eval(expression).value()); + + expression = dsl.right(DSL.literal(""), DSL.literal(10)); + assertEquals(STRING, expression.type()); + assertEquals("", eval(expression).value()); + when(nullRef.type()).thenReturn(STRING); when(missingRef.type()).thenReturn(INTEGER); assertEquals(missingValue(), eval(dsl.right(nullRef, missingRef))); @@ -308,6 +320,18 @@ void left() { assertEquals(STRING, expression.type()); assertEquals("hello", eval(expression).stringValue()); + expression = dsl.left(DSL.literal("hello"), DSL.literal(10)); + assertEquals(STRING, expression.type()); + assertEquals("hello", eval(expression).value()); + + expression = dsl.left(DSL.literal("hello"), DSL.literal(0)); + assertEquals(STRING, expression.type()); + assertEquals("", eval(expression).value()); + + expression = dsl.left(DSL.literal(""), DSL.literal(10)); + assertEquals(STRING, expression.type()); + assertEquals("", eval(expression).value()); + when(nullRef.type()).thenReturn(STRING); when(missingRef.type()).thenReturn(INTEGER); assertEquals(missingValue(), eval(dsl.left(nullRef, missingRef))); diff --git a/integ-test/src/test/resources/correctness/expressions/text_functions.txt b/integ-test/src/test/resources/correctness/expressions/text_functions.txt index 28186aceed..c2fd57c330 100644 --- a/integ-test/src/test/resources/correctness/expressions/text_functions.txt +++ b/integ-test/src/test/resources/correctness/expressions/text_functions.txt @@ -1,5 +1,11 @@ RIGHT('Hello World', 5) as column +RIGHT('Hello World', 20) as column +RIGHT('Hello World', 0) as column +RIGHT('', 5) as column LEFT('Hello World', 5) as column +LEFT('Hello World', 20) as column +LEFT('Hello World', 0) as column +LEFT('', 5) as column ASCII('hello') as column LOCATE('world', 'helloworld') as column LOCATE('world', 'hello') as column