This is an automated email from the ASF dual-hosted git repository.

jhyde pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new 62a9ee9423 [CALCITE-5610] Add COTH, CSCH, SECH functions
62a9ee9423 is described below

commit 62a9ee9423787c49750b080f5e33dcc2e04e03ec
Author: Tanner Clary <[email protected]>
AuthorDate: Thu Mar 23 15:13:34 2023 -0700

    [CALCITE-5610] Add COTH, CSCH, SECH functions
    
    The functions are enabled in all libraries.
    
    Close apache/calcite#3130
---
 .../calcite/adapter/enumerable/RexImpTable.java    |  6 +++
 .../org/apache/calcite/runtime/SqlFunctions.java   | 30 +++++++++++
 .../calcite/sql/fun/SqlLibraryOperators.java       | 27 ++++++++++
 site/_docs/reference.md                            |  5 +-
 .../org/apache/calcite/test/SqlOperatorTest.java   | 60 ++++++++++++++++++++++
 5 files changed, 127 insertions(+), 1 deletion(-)

diff --git 
a/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java 
b/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java
index 3931ddf5b7..26746290c5 100644
--- a/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java
+++ b/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java
@@ -124,6 +124,8 @@ import static 
org.apache.calcite.sql.fun.SqlLibraryOperators.COMPRESS;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.CONCAT2;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.CONCAT_FUNCTION;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.COSH;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.COTH;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.CSCH;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.DATE;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.DATEADD;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.DATETIME;
@@ -173,6 +175,7 @@ import static 
org.apache.calcite.sql.fun.SqlLibraryOperators.RIGHT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.RLIKE;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.RPAD;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.SAFE_CAST;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.SECH;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.SHA1;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.SHA256;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.SHA512;
@@ -520,10 +523,13 @@ public class RexImpTable {
       defineMethod(COS, "cos", NullPolicy.STRICT);
       defineMethod(COSH, "cosh", NullPolicy.STRICT);
       defineMethod(COT, "cot", NullPolicy.STRICT);
+      defineMethod(COTH, "coth", NullPolicy.STRICT);
+      defineMethod(CSCH, "csch", NullPolicy.STRICT);
       defineMethod(DEGREES, "degrees", NullPolicy.STRICT);
       defineMethod(POW, "power", NullPolicy.STRICT);
       defineMethod(RADIANS, "radians", NullPolicy.STRICT);
       defineMethod(ROUND, "sround", NullPolicy.STRICT);
+      defineMethod(SECH, "sech", NullPolicy.STRICT);
       defineMethod(SIGN, "sign", NullPolicy.STRICT);
       defineMethod(SIN, "sin", NullPolicy.STRICT);
       defineMethod(SINH, "sinh", NullPolicy.STRICT);
diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java 
b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
index 0e5a12dc56..be211650a0 100644
--- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
+++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
@@ -1940,6 +1940,26 @@ public class SqlFunctions {
     return 1.0d / Math.tan(b0);
   }
 
+  /** SQL <code>COTH</code> operator applied to BigDecimal values. */
+  public static double coth(BigDecimal b0) {
+    return 1.0d / Math.tanh(b0.doubleValue());
+  }
+
+  /** SQL <code>COTH</code> operator applied to double values. */
+  public static double coth(double b0) {
+    return 1.0d / Math.tanh(b0);
+  }
+
+  /** SQL <code>CSCH</code> operator applied to BigDecimal values. */
+  public static double csch(BigDecimal b0) {
+    return 1.0d / Math.sinh(b0.doubleValue());
+  }
+
+  /** SQL <code>CSCH</code> operator applied to double values. */
+  public static double csch(double b0) {
+    return 1.0d / Math.sinh(b0);
+  }
+
   // DEGREES
   /** SQL <code>DEGREES</code> operator applied to BigDecimal values. */
   public static double degrees(BigDecimal b0) {
@@ -1962,6 +1982,16 @@ public class SqlFunctions {
     return Math.toRadians(b0);
   }
 
+  /** SQL <code>SECH</code> operator applied to BigDecimal values. */
+  public static double sech(BigDecimal b0) {
+    return 1.0d / Math.cosh(b0.doubleValue());
+  }
+
+  /** SQL <code>SECH</code> operator applied to double values. */
+  public static double sech(double b0) {
+    return 1.0d / Math.cosh(b0);
+  }
+
   // SQL ROUND
   /** SQL <code>ROUND</code> operator applied to int values. */
   public static int sround(int b0) {
diff --git 
a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java 
b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
index 6c6a7b022b..492fb14493 100644
--- a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
+++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
@@ -1205,6 +1205,15 @@ public abstract class SqlLibraryOperators {
           OperandTypes.NUMERIC,
           SqlFunctionCategory.NUMERIC);
 
+  /** The "COTH(value)" function; returns the hyperbolic cotangent
+   * of {@code value}. */
+  @LibraryOperator(libraries = {ALL})
+  public static final SqlFunction COTH =
+      SqlBasicFunction.create("COTH",
+          ReturnTypes.DOUBLE_NULLABLE,
+          OperandTypes.NUMERIC,
+          SqlFunctionCategory.NUMERIC);
+
   @LibraryOperator(libraries = {ALL})
   public static final SqlFunction COSH =
       SqlBasicFunction.create("COSH",
@@ -1212,6 +1221,24 @@ public abstract class SqlLibraryOperators {
           OperandTypes.NUMERIC,
           SqlFunctionCategory.NUMERIC);
 
+  /** The "COTH(value)" function; returns the hyperbolic secant
+   * of {@code value}. */
+  @LibraryOperator(libraries = {ALL})
+  public static final SqlFunction SECH =
+      SqlBasicFunction.create("SECH",
+          ReturnTypes.DOUBLE_NULLABLE,
+          OperandTypes.NUMERIC,
+          SqlFunctionCategory.NUMERIC);
+
+  /** The "COTH(value)" function; returns the hyperbolic cosecant
+   * of {@code value}. */
+  @LibraryOperator(libraries = {ALL})
+  public static final SqlFunction CSCH =
+      SqlBasicFunction.create("CSCH",
+          ReturnTypes.DOUBLE_NULLABLE,
+          OperandTypes.NUMERIC,
+          SqlFunctionCategory.NUMERIC);
+
   @LibraryOperator(libraries = {ALL})
   public static final SqlFunction SINH =
       SqlBasicFunction.create("SINH",
diff --git a/site/_docs/reference.md b/site/_docs/reference.md
index 9d8d11beb3..e11d31fa89 100644
--- a/site/_docs/reference.md
+++ b/site/_docs/reference.md
@@ -2652,12 +2652,14 @@ BigQuery's type system uses confusingly different names 
for types and functions:
 | b | ARRAY_REVERSE(array)                           | Reverses elements of 
*array*
 | m s | CHAR(integer)                                | Returns the character 
whose ASCII code is *integer* % 256, or null if *integer* &lt; 0
 | b o p | CHR(integer)                               | Returns the character 
whose UTF-8 code is *integer*
-| * | COSH(numeric)                                  | Returns the hyperbolic 
cosine of *numeric*
 | o | CONCAT(string, string)                         | Concatenates two strings
 | b m p | CONCAT(string [, string ]*)                | Concatenates two or 
more strings
 | m | COMPRESS(string)                               | Compresses a string 
using zlib compression and returns the result as a binary string
 | q | CONVERT(type, expression [ , style ])          | Equivalent to 
`CAST(expression AS type)`; ignores the *style* operand
 | p | CONVERT_TIMEZONE(tz1, tz2, datetime)           | Converts the timezone 
of *datetime* from *tz1* to *tz2*
+| * | COSH(numeric)                                  | Returns the hyperbolic 
cosine of *numeric*
+| * | COTH(numeric)                                  | Returns the hyperbolic 
cotangent of *numeric*
+| * | CSCH(numeric)                                  | Returns the hyperbolic 
cosecant of *numeric*
 | b | CURRENT_DATETIME([ timeZone ])                 | Returns the current 
time as a TIMESTAMP from *timezone*
 | m | DAYNAME(datetime)                              | Returns the name, in 
the connection's locale, of the weekday in *datetime*; for example, it returns 
'星期日' for both DATE '2020-02-10' and TIMESTAMP '2020-02-10 10:10:10'
 | b | DATE(timestamp)                                | Extracts the DATE from 
a *timestamp*
@@ -2734,6 +2736,7 @@ BigQuery's type system uses confusingly different names 
for types and functions:
 | b o | RPAD(string, length[, pattern ])             | Returns a string or 
bytes value that consists of *string* appended to *length* with *pattern*
 | b o | RTRIM(string)                                | Returns *string* with 
all blanks removed from the end
 | b | SAFE_CAST(value AS type)                       | Converts *value* to 
*type*, returning NULL if conversion fails
+| * | SECH(numeric)                                  | Returns the hyperbolic 
secant of *numeric*
 | b m p | SHA1(string)                               | Calculates a SHA-1 hash 
value of *string* and returns it as a hex string
 | b p | SHA256(string)                               | Calculates a SHA-256 
hash value of *string* and returns it as a hex string
 | b p | SHA512(string)                               | Calculates a SHA-512 
hash value of *string* and returns it as a hex string
diff --git a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java 
b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
index b2f6f468a5..ae4ff74037 100644
--- a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
+++ b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
@@ -5573,6 +5573,46 @@ public class SqlOperatorTest {
     f.checkNull("cot(cast(null as double))");
   }
 
+  @Test void testCothFunc() {
+    final SqlOperatorFixture f0 = fixture().setFor(SqlLibraryOperators.COTH);
+    f0.checkFails("^coth(1)^",
+        "No match found for function signature COTH\\(<NUMERIC>\\)",
+        false);
+    final Consumer<SqlOperatorFixture> consumer = f -> {
+      f.checkType("coth(1)", "DOUBLE NOT NULL");
+      f.checkType("coth(cast(1 as float))", "DOUBLE NOT NULL");
+      f.checkType("coth(case when false then 1 else null end)", "DOUBLE");
+      f.checkType("coth('abc')", "DOUBLE NOT NULL");
+      f.checkScalarApprox("coth(1)", "DOUBLE NOT NULL",
+          isWithin(1.3130d, 0.0001d));
+      f.checkScalarApprox(" coth(cast(1 as decimal(1, 0)))", "DOUBLE NOT NULL",
+          isWithin(1.3130d, 0.0001d));
+      f.checkNull("coth(cast(null as integer))");
+      f.checkNull("coth(cast(null as double))");
+    };
+    f0.forEachLibrary(list(SqlLibrary.BIG_QUERY, SqlLibrary.ORACLE), consumer);
+  }
+
+  @Test void testCschFunc() {
+    final SqlOperatorFixture f0 = fixture().setFor(SqlLibraryOperators.CSCH);
+    f0.checkFails("^csch(1)^",
+        "No match found for function signature CSCH\\(<NUMERIC>\\)",
+        false);
+    final Consumer<SqlOperatorFixture> consumer = f -> {
+      f.checkType("csch(1)", "DOUBLE NOT NULL");
+      f.checkType("csch(cast(1 as float))", "DOUBLE NOT NULL");
+      f.checkType("csch(case when false then 1 else null end)", "DOUBLE");
+      f.checkType("csch('abc')", "DOUBLE NOT NULL");
+      f.checkScalarApprox("csch(1)", "DOUBLE NOT NULL",
+          isWithin(0.8509d, 0.0001d));
+      f.checkScalarApprox(" csch(cast(1 as decimal(1, 0)))", "DOUBLE NOT NULL",
+          isWithin(0.8509d, 0.0001d));
+      f.checkNull("csch(cast(null as integer))");
+      f.checkNull("csch(cast(null as double))");
+    };
+    f0.forEachLibrary(list(SqlLibrary.BIG_QUERY, SqlLibrary.ORACLE), consumer);
+  }
+
   @Test void testDegreesFunc() {
     final SqlOperatorFixture f = fixture();
     f.setFor(SqlStdOperatorTable.DEGREES, VmName.EXPAND);
@@ -5690,6 +5730,26 @@ public class SqlOperatorTest {
     f.checkNull("sign(cast(null as double))");
   }
 
+  @Test void testSechFunc() {
+    final SqlOperatorFixture f0 = fixture().setFor(SqlLibraryOperators.SECH);
+    f0.checkFails("^sech(1)^",
+        "No match found for function signature SECH\\(<NUMERIC>\\)",
+        false);
+    final Consumer<SqlOperatorFixture> consumer = f -> {
+      f.checkType("sech(1)", "DOUBLE NOT NULL");
+      f.checkType("sech(cast(1 as float))", "DOUBLE NOT NULL");
+      f.checkType("sech(case when false then 1 else null end)", "DOUBLE");
+      f.checkType("sech('abc')", "DOUBLE NOT NULL");
+      f.checkScalarApprox("sech(1)", "DOUBLE NOT NULL",
+          isWithin(0.6481d, 0.0001d));
+      f.checkScalarApprox(" sech(cast(1 as decimal(1, 0)))", "DOUBLE NOT NULL",
+          isWithin(0.6481d, 0.0001d));
+      f.checkNull("sech(cast(null as integer))");
+      f.checkNull("sech(cast(null as double))");
+    };
+    f0.forEachLibrary(list(SqlLibrary.BIG_QUERY, SqlLibrary.ORACLE), consumer);
+  }
+
   @Test void testSinFunc() {
     final SqlOperatorFixture f = fixture();
     f.setFor(SqlStdOperatorTable.SIN, VmName.EXPAND);

Reply via email to