Skip to content

Commit

Permalink
Added SIGNUM function to V2 engine (#1429)
Browse files Browse the repository at this point in the history
* Added SIGNUM function to V2 engine (#241)

* Added SIGNUM function to V2 engine, updated documentation, added unit/IT tests

Signed-off-by: Matthew Wells <[email protected]>

* fixed merge mistake

Signed-off-by: Matthew Wells <[email protected]>

* fixed merging error

Signed-off-by: Matthew Wells <[email protected]>

---------

Signed-off-by: Matthew Wells <[email protected]>
  • Loading branch information
matthewryanwells authored Mar 15, 2023
1 parent 4f9f5ca commit 6496692
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 6 deletions.
5 changes: 5 additions & 0 deletions core/src/main/java/org/opensearch/sql/expression/DSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,13 @@ public static FunctionExpression sign(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.SIGN, expressions);
}

public static FunctionExpression signum(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.SIGNUM, expressions);
}

public static FunctionExpression sinh(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.SINH, expressions);

}

public static FunctionExpression sqrt(Expression... expressions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public enum BuiltinFunctionName {
RINT(FunctionName.of("rint")),
ROUND(FunctionName.of("round")),
SIGN(FunctionName.of("sign")),
SIGNUM(FunctionName.of("signum")),
SINH(FunctionName.of("sinh")),
SQRT(FunctionName.of("sqrt")),
CBRT(FunctionName.of("cbrt")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public static void register(BuiltinFunctionRepository repository) {
repository.register(rint());
repository.register(round());
repository.register(sign());
repository.register(signum());
repository.register(sinh());
repository.register(sqrt());
repository.register(truncate());
Expand Down Expand Up @@ -489,6 +490,18 @@ private static DefaultFunctionResolver sign() {
v -> new ExprIntegerValue(Math.signum(v.doubleValue())), INTEGER);
}

/**
* Definition of signum(x) function.
* Returns the sign of the argument as -1.0, 0, or 1.0
* depending on whether x is negative, zero, or positive
* The supported signature is
* BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE -> INTEGER
*/
private static DefaultFunctionResolver signum() {
return baseMathFunction(BuiltinFunctionName.SIGNUM.getName(),
v -> new ExprIntegerValue(Math.signum(v.doubleValue())), INTEGER);
}

/**
* Definition of sinh(x) function.
* Returns the hyperbolix sine of x, defined as (((e^x) - (e^(-x))) / 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1890,6 +1890,84 @@ public void sign_missing_value() {
assertTrue(sign.valueOf(valueEnv()).isMissing());
}

/**
* Test signum with byte value.
*/
@ParameterizedTest(name = "signum({0})")
@ValueSource(bytes = {2, 0, -2})
public void signum_bytes_value(Byte value) {
FunctionExpression signum = DSL.signum(DSL.literal(value));
assertThat(
signum.valueOf(valueEnv()),
allOf(hasType(INTEGER), hasValue((int) Math.signum(value))));
assertEquals(String.format("signum(%s)", value), signum.toString());
}

/**
* Test signum with short value.
*/
@ParameterizedTest(name = "signum({0})")
@ValueSource(shorts = {2, 0, -2})
public void signum_short_value(Short value) {
FunctionExpression signum = DSL.signum(DSL.literal(value));
assertThat(
signum.valueOf(valueEnv()),
allOf(hasType(INTEGER), hasValue((int) Math.signum(value))));
assertEquals(String.format("signum(%s)", value), signum.toString());
}

/**
* Test signum with integer value.
*/
@ParameterizedTest(name = "signum({0})")
@ValueSource(ints = {2, 0, -2})
public void signum_int_value(Integer value) {
FunctionExpression signum = DSL.signum(DSL.literal(value));
assertThat(
signum.valueOf(valueEnv()),
allOf(hasType(INTEGER), hasValue((int) Math.signum(value))));
assertEquals(String.format("signum(%s)", value), signum.toString());
}

/**
* Test signum with long value.
*/
@ParameterizedTest(name = "signum({0})")
@ValueSource(longs = {2L, 0L, -2L})
public void signum_long_value(Long value) {
FunctionExpression signum = DSL.signum(DSL.literal(value));
assertThat(
signum.valueOf(valueEnv()),
allOf(hasType(INTEGER), hasValue((int) Math.signum(value))));
assertEquals(String.format("signum(%s)", value), signum.toString());
}

/**
* Test signum with float value.
*/
@ParameterizedTest(name = "signum({0})")
@ValueSource(floats = {2F, 0F, -2F})
public void signum_float_value(Float value) {
FunctionExpression signum = DSL.signum(DSL.literal(value));
assertThat(
signum.valueOf(valueEnv()),
allOf(hasType(INTEGER), hasValue((int) Math.signum(value))));
assertEquals(String.format("signum(%s)", value), signum.toString());
}

/**
* Test signum with double value.
*/
@ParameterizedTest(name = "signum({0})")
@ValueSource(doubles = {2, 0, -2})
public void signum_double_value(Double value) {
FunctionExpression signum = DSL.signum(DSL.literal(value));
assertThat(
signum.valueOf(valueEnv()),
allOf(hasType(INTEGER), hasValue((int) Math.signum(value))));
assertEquals(String.format("signum(%s)", value), signum.toString());
}

/**
* Test sinh with byte value.
*/
Expand Down
18 changes: 16 additions & 2 deletions docs/user/dql/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -784,9 +784,23 @@ SIGNUM
Description
>>>>>>>>>>>

Specifications:
Usage: Returns the sign of the argument as -1, 0, or 1, depending on whether the number is negative, zero, or positive

Argument type: BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE

Return type: INTEGER

Synonyms: `SIGN`_

1. SIGNUM(NUMBER T) -> T
Example::

os> SELECT SIGNUM(1), SIGNUM(0), SIGNUM(-1.1)
fetched rows / total rows = 1/1
+-------------+-------------+----------------+
| SIGNUM(1) | SIGNUM(0) | SIGNUM(-1.1) |
|-------------+-------------+----------------|
| 1 | 0 | -1 |
+-------------+-------------+----------------+


SIN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,6 @@ public void testRound() throws IOException {
verifyDataRows(result, rows(-4.0));
}

/**
* Test sign function with double value.
*/
@Test
public void testSign() throws IOException {
JSONObject result = executeQuery("select sign(1.1)");
Expand All @@ -182,6 +179,16 @@ public void testSign() throws IOException {
}

@Test
public void testSignum() throws IOException {
JSONObject result = executeQuery("select signum(1.1)");
verifySchema(result, schema("signum(1.1)", null, "integer"));
verifyDataRows(result, rows(1));

result = executeQuery("select signum(-1.1)");
verifySchema(result, schema("signum(-1.1)", null, "integer"));
verifyDataRows(result, rows(-1));
}

public void testSinh() throws IOException {
JSONObject result = executeQuery("select sinh(1)");
verifySchema(result, schema("sinh(1)", null, "double"));
Expand Down
2 changes: 1 addition & 1 deletion sql/src/main/antlr/OpenSearchSQLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ aggregationFunctionName

mathematicalFunctionName
: ABS | CBRT | CEIL | CEILING | CONV | CRC32 | E | EXP | EXPM1 | FLOOR | LN | LOG | LOG10 | LOG2 | MOD | PI | POW | POWER
| RAND | RINT | ROUND | SIGN | SQRT | TRUNCATE
| RAND | RINT | ROUND | SIGN | SIGNUM | SQRT | TRUNCATE
| trigonometricFunctionName
;

Expand Down

0 comments on commit 6496692

Please sign in to comment.