Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

add string function RIGHT #938

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -409,6 +409,10 @@ public FunctionExpression strcmp(Expression... expressions) {
return function(BuiltinFunctionName.STRCMP, expressions);
}

public FunctionExpression right(Expression... expressions) {
return function(BuiltinFunctionName.RIGHT, expressions);
}

public FunctionExpression and(Expression... expressions) {
return function(BuiltinFunctionName.AND, expressions);
}
Expand Down
Expand Up @@ -131,6 +131,7 @@ public enum BuiltinFunctionName {
CONCAT_WS(FunctionName.of("concat_ws")),
LENGTH(FunctionName.of("length")),
STRCMP(FunctionName.of("strcmp")),
RIGHT(FunctionName.of("right")),

/**
* NULL Test.
Expand Down
Expand Up @@ -60,6 +60,7 @@ public void register(BuiltinFunctionRepository repository) {
repository.register(concat_ws());
repository.register(length());
repository.register(strcmp());
repository.register(right());
}

/**
Expand Down Expand Up @@ -194,6 +195,16 @@ private FunctionResolver strcmp() {
INTEGER, STRING, STRING));
}

/**
* Returns the rightmost len characters from the string str, or NULL if any argument is NULL.
* Supports following signatures:
* (STRING, INTEGER) -> STRING
*/
private FunctionResolver right() {
return define(BuiltinFunctionName.RIGHT.getName(),
impl(nullMissingHandling(TextFunction::exprRight), STRING, STRING, INTEGER));
}

private static ExprValue exprSubstrStart(ExprValue exprValue, ExprValue start) {
int startIdx = start.integerValue();
if (startIdx == 0) {
Expand Down Expand Up @@ -225,5 +236,10 @@ private static ExprValue exprSubStr(String str, int start, int len) {
}
return new ExprStringValue(str.substring(start, start + len));
}

private static ExprValue exprRight(ExprValue str, ExprValue len) {
return new ExprStringValue(str.stringValue().substring(
str.stringValue().length() - len.integerValue()));
}
}

Expand Up @@ -25,6 +25,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

import com.amazon.opendistroforelasticsearch.sql.data.model.ExprIntegerValue;
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprStringValue;
import com.amazon.opendistroforelasticsearch.sql.data.model.ExprValue;
import com.amazon.opendistroforelasticsearch.sql.expression.DSL;
Expand Down Expand Up @@ -296,6 +297,23 @@ void strcmp() {
assertEquals(missingValue(), eval(dsl.strcmp(missingRef, nullRef)));
}

@Test
void right() {
FunctionExpression expression = dsl.right(
DSL.literal(new ExprStringValue("foobarbar")),
DSL.literal(new ExprIntegerValue(4)));
assertEquals(STRING, expression.type());
assertEquals("rbar", eval(expression).stringValue());

when(nullRef.type()).thenReturn(STRING);
when(missingRef.type()).thenReturn(INTEGER);
assertEquals(missingValue(), eval(dsl.right(nullRef, missingRef)));
assertEquals(nullValue(), eval(dsl.right(nullRef, DSL.literal(new ExprIntegerValue(1)))));

when(nullRef.type()).thenReturn(INTEGER);
assertEquals(nullValue(), eval(dsl.right(DSL.literal(new ExprStringValue("value")), nullRef)));
}

void testConcatString(List<String> strings) {
String expected = null;
if (strings.stream().noneMatch(Objects::isNull)) {
Expand Down
23 changes: 23 additions & 0 deletions docs/experiment/ppl/functions/string.rst
Expand Up @@ -150,6 +150,29 @@ Example::
+---------------------+---------------------+


RIGHT
-----

Description
>>>>>>>>>>>

Usage: right(str, len) returns the rightmost len characters from the string str, or NULL if any argument is NULL.

Argument type: STRING, INTEGER

Return type: STRING

Example::

od> source=people | eval `RIGHT('helloworld', 5)` = RIGHT('helloworld', 5), `RIGHT('HELLOWORLD', 0)` = RIGHT('HELLOWORLD', 0) | fields `RIGHT('helloworld', 5)`, `RIGHT('HELLOWORLD', 0)`
fetched rows / total rows = 1/1
+--------------------------+--------------------------+
| RIGHT('helloworld', 5) | RIGHT('HELLOWORLD', 0) |
|--------------------------+--------------------------|
| world | |
+--------------------------+--------------------------+


RTRIM
-----

Expand Down
16 changes: 14 additions & 2 deletions docs/user/dql/functions.rst
Expand Up @@ -1760,9 +1760,21 @@ RIGHT
Description
>>>>>>>>>>>

Specifications:
Usage: right(str, len) returns the rightmost len characters from the string str, or NULL if any argument is NULL.

Argument type: STRING, INTEGER

Return type: STRING

1. RIGHT(STRING T, INTEGER) -> T
Example::

od> SELECT RIGHT('helloworld', 5), RIGHT('HELLOWORLD', 0)
fetched rows / total rows = 1/1
+--------------------------+--------------------------+
| RIGHT('helloworld', 5) | RIGHT('HELLOWORLD', 0) |
|--------------------------+--------------------------|
| world | |
+--------------------------+--------------------------+


RTRIM
Expand Down
Expand Up @@ -92,6 +92,11 @@ public void testTrim() throws IOException {
verifyQuery("trim", "", "", "hello", "world", "helloworld");
}

@Test
public void testRight() throws IOException {
verifyQuery("right", "", ", 3", "llo", "rld", "rld");
}

@Test
public void testRtrim() throws IOException {
verifyQuery("rtrim", "", "", "hello", "world", "helloworld");
Expand Down
Expand Up @@ -126,6 +126,11 @@ public void testStrcmp() throws IOException {
verifyQuery("strcmp('hello', 'hello')", "integer", 0);
}

@Test
public void testRight() throws IOException {
verifyQuery("right('variable', 4)", "keyword", "able");
}

protected JSONObject executeQuery(String query) throws IOException {
Request request = new Request("POST", QUERY_API_ENDPOINT);
request.setJsonEntity(String.format(Locale.ROOT, "{\n" + " \"query\": \"%s\"\n" + "}", query));
Expand Down
@@ -0,0 +1 @@
RIGHT('Hello World', 5) as column
1 change: 1 addition & 0 deletions ppl/src/main/antlr/OpenDistroPPLLexer.g4
Expand Up @@ -228,6 +228,7 @@ CONCAT: 'CONCAT';
CONCAT_WS: 'CONCAT_WS';
LENGTH: 'LENGTH';
STRCMP: 'STRCMP';
RIGHT: 'RIGHT';

// BOOL FUNCTIONS
LIKE: 'LIKE';
Expand Down
1 change: 1 addition & 0 deletions ppl/src/main/antlr/OpenDistroPPLParser.g4
Expand Up @@ -258,6 +258,7 @@ conditionFunctionBase

textFunctionBase
: SUBSTR | SUBSTRING | TRIM | LTRIM | RTRIM | LOWER | UPPER | CONCAT | CONCAT_WS | LENGTH | STRCMP
| RIGHT
;

/** operators */
Expand Down
2 changes: 1 addition & 1 deletion sql/src/main/antlr/OpenDistroSQLParser.g4
Expand Up @@ -345,7 +345,7 @@ dateTimeFunctionName

textFunctionName
: SUBSTR | SUBSTRING | TRIM | LTRIM | RTRIM | LOWER | UPPER
| CONCAT | CONCAT_WS | SUBSTR | LENGTH | STRCMP
| CONCAT | CONCAT_WS | SUBSTR | LENGTH | STRCMP | RIGHT
;

functionArgs
Expand Down