Skip to content

Commit

Permalink
Added COSH to V2 engine (#244) (#1428) (#1565)
Browse files Browse the repository at this point in the history
* Added COSH to V2 engine, updated documentation, unit/IT tests

Signed-off-by: Matthew Wells <matthew.wells@improving.com>
(cherry picked from commit 8dad71f)
  • Loading branch information
matthewryanwells authored Apr 20, 2023
1 parent 723777f commit aa5b3e0
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 3 deletions.
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 @@ -307,6 +307,10 @@ public static FunctionExpression cos(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.COS, expressions);
}

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

public static FunctionExpression cot(Expression... expressions) {
return compile(FunctionProperties.None, BuiltinFunctionName.COT, expressions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public enum BuiltinFunctionName {
ATAN(FunctionName.of("atan")),
ATAN2(FunctionName.of("atan2")),
COS(FunctionName.of("cos")),
COSH(FunctionName.of("cosh")),
COT(FunctionName.of("cot")),
DEGREES(FunctionName.of("degrees")),
RADIANS(FunctionName.of("radians")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public static void register(BuiltinFunctionRepository repository) {
repository.register(atan());
repository.register(atan2());
repository.register(cos());
repository.register(cosh());
repository.register(cot());
repository.register(degrees());
repository.register(radians());
Expand Down Expand Up @@ -619,6 +620,17 @@ private static DefaultFunctionResolver cos() {
v -> new ExprDoubleValue(Math.cos(v.doubleValue())), DOUBLE);
}

/**
* Definition of cosh(x) function.
* Returns the hyperbolic cosine of x, defined as (((e^x) + (e^(-x))) / 2)
* The supported signature is
* BYTE/SHORT/INTEGER/LONG/FLOAT/DOUBLE -> DOUBLE
*/
private static DefaultFunctionResolver cosh() {
return baseMathFunction(BuiltinFunctionName.COSH.getName(),
v -> new ExprDoubleValue(Math.cosh(v.doubleValue())), DOUBLE);
}

/**
* Definition of cot(x) function.
* Calculates the cotangent of x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2435,6 +2435,72 @@ public void cos_missing_value() {
assertTrue(cos.valueOf(valueEnv()).isMissing());
}

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

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

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

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

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

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

/**
* Test cot with integer, long, float, double values.
*/
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 @@ -333,9 +333,21 @@ COSH
Description
>>>>>>>>>>>

Specifications:
Usage: cosh(x) calculates the hyperbolic cosine of x, defined as (((e^x) + (e^(-x))) / 2)

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

Return Type: DOUBLE

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

os> SELECT COSH(2), COSH(1.5)
fetched rows / total rows = 1/1
+--------------------+-------------------+
| COSH(2) | COSH(1.5) |
|--------------------+-------------------|
| 3.7621956910836314 | 2.352409615243247 |
+--------------------+-------------------+


COT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ public void testConv() throws IOException {
verifyDataRows(result, rows("17"));
}

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

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

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

@Test
public void testCrc32() throws IOException {
JSONObject result = executeQuery("select crc32('MySQL')");
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 @@ -480,7 +480,7 @@ mathematicalFunctionName
;

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

arithmeticFunctionName
Expand Down

0 comments on commit aa5b3e0

Please sign in to comment.