Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added SINH function to V2 engine #1434

Merged
merged 1 commit into from
Mar 15, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 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 @@ -226,6 +226,10 @@ public static FunctionExpression sign(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.SIGN, expressions);
}

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

public static FunctionExpression sqrt(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.SQRT, expressions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public enum BuiltinFunctionName {
RAND(FunctionName.of("rand")),
ROUND(FunctionName.of("round")),
SIGN(FunctionName.of("sign")),
SINH(FunctionName.of("sinh")),
SQRT(FunctionName.of("sqrt")),
CBRT(FunctionName.of("cbrt")),
TRUNCATE(FunctionName.of("truncate")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public static void register(BuiltinFunctionRepository repository) {
repository.register(power());
repository.register(round());
repository.register(sign());
repository.register(sinh());
repository.register(sqrt());
repository.register(truncate());
repository.register(pi());
Expand Down Expand Up @@ -475,6 +476,17 @@ private static DefaultFunctionResolver sign() {
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)
* The supported signature is
* BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
*/
private static DefaultFunctionResolver sinh() {
return baseMathFunction(BuiltinFunctionName.SINH.getName(),
v -> new ExprDoubleValue(Math.sinh(v.doubleValue())), DOUBLE);
}

/**
* Definition of sqrt(x) function.
* Calculate the square root of a non-negative number x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1804,6 +1804,72 @@ public void sign_missing_value() {
assertTrue(sign.valueOf(valueEnv()).isMissing());
}

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

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

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

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

/**
* Test sinh with float value.
*/
@ParameterizedTest(name = "sinh({0})")
@ValueSource(floats = {-1.5F, -1F, 1F, 1.5F, 2F, 2.7F, Float.MAX_VALUE, Float.MIN_VALUE})
public void sinh_float_value(Float value) {
FunctionExpression sinh = DSL.sinh(DSL.literal(value));
assertThat(sinh.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.sinh(value))));
assertEquals(String.format("sinh(%s)", value), sinh.toString());
}

/**
* Test sinh with double value.
*/
@ParameterizedTest(name = "sinh({0})")
@ValueSource(doubles = {-1.5, -1D, 1D, 1.5D, 2D, 2.7D, Double.MAX_VALUE, Double.MIN_VALUE})
public void sinh_double_value(Double value) {
FunctionExpression sinh = DSL.sinh(DSL.literal(value));
assertThat(sinh.valueOf(valueEnv()), allOf(hasType(DOUBLE), hasValue(Math.sinh(value))));
assertEquals(String.format("sinh(%s)", value), sinh.toString());
}

/**
* Test sqrt with int value.
*/
Expand Down
16 changes: 14 additions & 2 deletions docs/user/dql/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -794,9 +794,21 @@ SINH
Description
>>>>>>>>>>>

Specifications:
Usage: sinh(x) calculate the hyperbolic sine of x, defined as (((e^x) - (e^(-x))) / 2)

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

Return type: DOUBLE

1. SINH(NUMBER T) -> DOUBLE
Example::

os> SELECT SINH(2), SINH(1.5)
fetched rows / total rows = 1/1
+-------------------+--------------------+
| SINH(2) | SINH(1.5) |
|-------------------+--------------------|
| 3.626860407847019 | 2.1292794550948173 |
+-------------------+--------------------+


SQRT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,21 @@ public void testSign() throws IOException {
verifyDataRows(result, rows(-1));
}

@Test
public void testSinh() throws IOException {
JSONObject result = executeQuery("select sinh(1)");
verifySchema(result, schema("sinh(1)", null, "double"));
verifyDataRows(result, rows(1.1752011936438014));

result = executeQuery("select sinh(-1)");
verifySchema(result, schema("sinh(-1)", null, "double"));
verifyDataRows(result, rows(-1.1752011936438014));

result = executeQuery("select sinh(1.5)");
verifySchema(result, schema("sinh(1.5)", null, "double"));
verifyDataRows(result, rows(2.1292794550948173));
}

@Test
public void testTruncate() throws IOException {
JSONObject result = executeQuery("select truncate(56.78, 1)");
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 @@ -424,7 +424,7 @@ mathematicalFunctionName
;

trigonometricFunctionName
: ACOS | ASIN | ATAN | ATAN2 | COS | COT | DEGREES | RADIANS | SIN | TAN
: ACOS | ASIN | ATAN | ATAN2 | COS | COT | DEGREES | RADIANS | SIN | SINH | TAN
;

dateTimeFunctionName
Expand Down