Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: chloe-zh <chloezh1102@gmail.com>
  • Loading branch information
chloe-zh committed Nov 13, 2021
1 parent 603d953 commit 0cf5c8a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand All @@ -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)));
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 0cf5c8a

Please sign in to comment.