Skip to content

Commit

Permalink
[CALCITE-6472] Add degree based trig functions to PostgreSQL function…
Browse files Browse the repository at this point in the history
… library

Added the following functions to only the PostgreSQL function library:
* COSD
* SIND
* TAND
* ACOSD
* ASIND
* ATAND
  • Loading branch information
normanj-bitquill committed Jul 16, 2024
1 parent 95fedab commit 6534dca
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
import static org.apache.calcite.linq4j.tree.ExpressionType.UnaryPlus;
import static org.apache.calcite.sql.fun.SqlInternalOperators.LITERAL_AGG;
import static org.apache.calcite.sql.fun.SqlInternalOperators.THROW_UNLESS;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.ACOSD;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.ACOSH;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAYS_OVERLAP;
Expand All @@ -143,7 +144,9 @@
import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_SIZE;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_TO_STRING;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.ARRAY_UNION;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.ASIND;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.ASINH;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.ATAND;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.ATANH;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.BITAND_AGG;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.BITOR_AGG;
Expand All @@ -166,6 +169,7 @@
import static org.apache.calcite.sql.fun.SqlLibraryOperators.CONCAT_WS_MSSQL;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.CONCAT_WS_SPARK;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.CONTAINS_SUBSTR;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.COSD;
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.CSC;
Expand Down Expand Up @@ -266,6 +270,7 @@
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;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.SIND;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.SINH;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.SORT_ARRAY;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.SOUNDEX;
Expand All @@ -275,6 +280,7 @@
import static org.apache.calcite.sql.fun.SqlLibraryOperators.STARTS_WITH;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.STRCMP;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.STR_TO_MAP;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.TAND;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.TANH;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.TIME;
import static org.apache.calcite.sql.fun.SqlLibraryOperators.TIMESTAMP;
Expand Down Expand Up @@ -677,14 +683,18 @@ Builder populate() {
defineReflective(RANDOM, BuiltInMethod.RAND.method);

defineMethod(ACOS, BuiltInMethod.ACOS.method, NullPolicy.STRICT);
defineMethod(ACOSD, BuiltInMethod.ACOSD.method, NullPolicy.STRICT);
defineMethod(ACOSH, BuiltInMethod.ACOSH.method, NullPolicy.STRICT);
defineMethod(ASIN, BuiltInMethod.ASIN.method, NullPolicy.STRICT);
defineMethod(ASIND, BuiltInMethod.ASIND.method, NullPolicy.STRICT);
defineMethod(ASINH, BuiltInMethod.ASINH.method, NullPolicy.STRICT);
defineMethod(ATAN, BuiltInMethod.ATAN.method, NullPolicy.STRICT);
defineMethod(ATAN2, BuiltInMethod.ATAN2.method, NullPolicy.STRICT);
defineMethod(ATAND, BuiltInMethod.ATAND.method, NullPolicy.STRICT);
defineMethod(ATANH, BuiltInMethod.ATANH.method, NullPolicy.STRICT);
defineMethod(CBRT, BuiltInMethod.CBRT.method, NullPolicy.STRICT);
defineMethod(COS, BuiltInMethod.COS.method, NullPolicy.STRICT);
defineMethod(COSD, BuiltInMethod.COSD.method, NullPolicy.STRICT);
defineMethod(COSH, BuiltInMethod.COSH.method, NullPolicy.STRICT);
defineMethod(COT, BuiltInMethod.COT.method, NullPolicy.STRICT);
defineMethod(COTH, BuiltInMethod.COTH.method, NullPolicy.STRICT);
Expand All @@ -701,8 +711,10 @@ Builder populate() {
defineMethod(SECH, BuiltInMethod.SECH.method, NullPolicy.STRICT);
defineMethod(SIGN, BuiltInMethod.SIGN.method, NullPolicy.STRICT);
defineMethod(SIN, BuiltInMethod.SIN.method, NullPolicy.STRICT);
defineMethod(SIND, BuiltInMethod.SIND.method, NullPolicy.STRICT);
defineMethod(SINH, BuiltInMethod.SINH.method, NullPolicy.STRICT);
defineMethod(TAN, BuiltInMethod.TAN.method, NullPolicy.STRICT);
defineMethod(TAND, BuiltInMethod.TAND.method, NullPolicy.STRICT);
defineMethod(TANH, BuiltInMethod.TANH.method, NullPolicy.STRICT);
defineMethod(TRUNC_BIG_QUERY, BuiltInMethod.STRUNCATE.method, NullPolicy.STRICT);
defineMethod(TRUNCATE, BuiltInMethod.STRUNCATE.method, NullPolicy.STRICT);
Expand Down
66 changes: 66 additions & 0 deletions core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -3053,6 +3053,17 @@ public static double acos(double b0) {
return Math.acos(b0);
}

// ACOSD
/** SQL <code>ACOSD</code> operator applied to BigDecimal values. */
public static double acosd(BigDecimal b0) {
return Math.toDegrees(Math.acos(b0.doubleValue()));
}

/** SQL <code>ACOSD</code> operator applied to double values. */
public static double acosd(double b0) {
return Math.toDegrees(Math.acos(b0));
}

// ACOSH
/** SQL <code>ACOSH</code> operator applied to BigDecimal values. */
public static double acosh(BigDecimal b0) {
Expand All @@ -3078,6 +3089,17 @@ public static double asin(double b0) {
return Math.asin(b0);
}

// ASIND
/** SQL <code>ASIND</code> operator applied to BigDecimal values. */
public static double asind(BigDecimal b0) {
return Math.toDegrees(Math.asin(b0.doubleValue()));
}

/** SQL <code>ASIND</code> operator applied to double values. */
public static double asind(double b0) {
return Math.toDegrees(Math.asin(b0));
}

// ASINH
/** SQL <code>ASINH</code> operator applied to BigDecimal values. */
public static double asinh(BigDecimal b0) {
Expand Down Expand Up @@ -3108,6 +3130,17 @@ public static double atan(double b0) {
return Math.atan(b0);
}

// ATAND
/** SQL <code>ATAN</code> operator applied to BigDecimal values. */
public static double atand(BigDecimal b0) {
return Math.toDegrees(Math.atan(b0.doubleValue()));
}

/** SQL <code>ATAND</code> operator applied to double values. */
public static double atand(double b0) {
return Math.toDegrees(Math.atan(b0));
}

// ATAN2
/** SQL <code>ATAN2</code> operator applied to double/BigDecimal values. */
public static double atan2(double b0, BigDecimal b1) {
Expand Down Expand Up @@ -3173,6 +3206,17 @@ public static double cos(double b0) {
return Math.cos(b0);
}

// COSD
/** SQL <code>COSD</code> operator applied to BigDecimal values. */
public static double cosd(BigDecimal b0) {
return Math.cos(Math.toRadians(b0.doubleValue()));
}

/** SQL <code>COSD</code> operator applied to double values. */
public static double cosd(double b0) {
return Math.cos(Math.toRadians(b0));
}

// COSH
/** SQL <code>COSH</code> operator applied to BigDecimal values. */
public static double cosh(BigDecimal b) {
Expand Down Expand Up @@ -3397,6 +3441,17 @@ public static double sin(double b0) {
return Math.sin(b0);
}

// SIND
/** SQL <code>SIND</code> operator applied to BigDecimal values. */
public static double sind(BigDecimal b0) {
return Math.sin(Math.toRadians(b0.doubleValue()));
}

/** SQL <code>SIND</code> operator applied to double values. */
public static double sind(double b0) {
return Math.sin(Math.toRadians(b0));
}

// SINH
/** SQL <code>SINH</code> operator applied to BigDecimal values. */
public static double sinh(BigDecimal b) {
Expand All @@ -3419,6 +3474,17 @@ public static double tan(double b0) {
return Math.tan(b0);
}

// TAND
/** SQL <code>TAND</code> operator applied to BigDecimal values. */
public static double tand(BigDecimal b0) {
return Math.tan(Math.toRadians(b0.doubleValue()));
}

/** SQL <code>TAND</code> operator applied to double values. */
public static double tand(double b0) {
return Math.tan(Math.toRadians(b0));
}

// TANH
/** SQL <code>TANH</code> operator applied to BigDecimal values. */
public static double tanh(BigDecimal b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2194,6 +2194,60 @@ private static RelDataType deriveTypeMapFromEntries(SqlOperatorBinding opBinding
OperandTypes.NUMERIC,
SqlFunctionCategory.NUMERIC);

/** The {@code COSD(numeric)} function; returns the cosine
* of {@code value}. {@code value} is treated as degrees. */
@LibraryOperator(libraries = {POSTGRESQL}, exceptLibraries = {REDSHIFT})
public static final SqlFunction COSD =
SqlBasicFunction.create("COSD",
ReturnTypes.DOUBLE_NULLABLE,
OperandTypes.NUMERIC,
SqlFunctionCategory.NUMERIC);

/** The {@code SIND(numeric)} function; returns the sine
* of {@code value}. {@code value} is treated as degrees. */
@LibraryOperator(libraries = {POSTGRESQL}, exceptLibraries = {REDSHIFT})
public static final SqlFunction SIND =
SqlBasicFunction.create("SIND",
ReturnTypes.DOUBLE_NULLABLE,
OperandTypes.NUMERIC,
SqlFunctionCategory.NUMERIC);

/** The {@code TAND(numeric)} function; returns the tangent
* of {@code value}. {@code value} is treated as degrees. */
@LibraryOperator(libraries = {POSTGRESQL}, exceptLibraries = {REDSHIFT})
public static final SqlFunction TAND =
SqlBasicFunction.create("TAND",
ReturnTypes.DOUBLE_NULLABLE,
OperandTypes.NUMERIC,
SqlFunctionCategory.NUMERIC);

/** The {@code ACOSD(numeric)} function; returns the inverse cosine
* of {@code value} in degrees. */
@LibraryOperator(libraries = {POSTGRESQL}, exceptLibraries = {REDSHIFT})
public static final SqlFunction ACOSD =
SqlBasicFunction.create("ACOSD",
ReturnTypes.DOUBLE_NULLABLE,
OperandTypes.NUMERIC,
SqlFunctionCategory.NUMERIC);

/** The {@code ACOSD(numeric)} function; returns the inverse sine
* of {@code value} in degrees. */
@LibraryOperator(libraries = {POSTGRESQL}, exceptLibraries = {REDSHIFT})
public static final SqlFunction ASIND =
SqlBasicFunction.create("ASIND",
ReturnTypes.DOUBLE_NULLABLE,
OperandTypes.NUMERIC,
SqlFunctionCategory.NUMERIC);

/** The {@code ACOSD(numeric)} function; returns the inverse tangent
* of {@code value} in degrees. */
@LibraryOperator(libraries = {POSTGRESQL}, exceptLibraries = {REDSHIFT})
public static final SqlFunction ATAND =
SqlBasicFunction.create("ATAND",
ReturnTypes.DOUBLE_NULLABLE,
OperandTypes.NUMERIC,
SqlFunctionCategory.NUMERIC);

/** The "COTH(value)" function; returns the hyperbolic secant
* of {@code value}. */
@LibraryOperator(libraries = {ALL})
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/org/apache/calcite/util/BuiltInMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -493,14 +493,18 @@ public enum BuiltInMethod {
CEIL(SqlFunctions.class, "ceil", int.class, int.class),
ABS(SqlFunctions.class, "abs", long.class),
ACOS(SqlFunctions.class, "acos", double.class),
ACOSD(SqlFunctions.class, "acosd", double.class),
ACOSH(SqlFunctions.class, "acosh", double.class),
ASIN(SqlFunctions.class, "asin", double.class),
ASIND(SqlFunctions.class, "asind", double.class),
ASINH(SqlFunctions.class, "asinh", double.class),
ATAN(SqlFunctions.class, "atan", double.class),
ATAN2(SqlFunctions.class, "atan2", double.class, double.class),
ATAND(SqlFunctions.class, "atand", double.class),
ATANH(SqlFunctions.class, "atanh", double.class),
CBRT(SqlFunctions.class, "cbrt", double.class),
COS(SqlFunctions.class, "cos", double.class),
COSD(SqlFunctions.class, "cosd", double.class),
COSH(SqlFunctions.class, "cosh", long.class),
COT(SqlFunctions.class, "cot", double.class),
COTH(SqlFunctions.class, "coth", double.class),
Expand Down Expand Up @@ -529,7 +533,9 @@ public enum BuiltInMethod {
SECH(SqlFunctions.class, "sech", double.class),
SIGN(SqlFunctions.class, "sign", long.class),
SIN(SqlFunctions.class, "sin", double.class),
SIND(SqlFunctions.class, "sind", double.class),
TAN(SqlFunctions.class, "tan", double.class),
TAND(SqlFunctions.class, "tand", double.class),
TANH(SqlFunctions.class, "tanh", long.class),
SINH(SqlFunctions.class, "sinh", long.class),
TRUNCATE(SqlFunctions.class, "truncate", String.class, int.class),
Expand Down
6 changes: 6 additions & 0 deletions site/_docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2689,6 +2689,7 @@ In the following:
|:- |:-----------------------------------------------|:-----------
| p | expr :: type | Casts *expr* to *type*
| m | expr1 <=> expr2 | Whether two values are equal, treating null values as the same, and it's similar to `IS NOT DISTINCT FROM`
| p | ACOSD(numeric) | Returns the inverse cosine of *numeric* in degrees
| * | ACOSH(numeric) | Returns the inverse hyperbolic cosine of *numeric*
| s | ARRAY([expr [, expr ]*]) | Construct an array in Apache Spark. The function allows users to use `ARRAY()` to create an empty array
| s | ARRAY_APPEND(array, element) | Appends an *element* to the end of the *array* and returns the result. Type of *element* should be similar to type of the elements of the *array*. If the *array* is null, the function will return null. If an *element* that is null, the null *element* will be added to the end of the *array*
Expand All @@ -2714,7 +2715,9 @@ In the following:
| s | ARRAYS_OVERLAP(array1, array2) | Returns true if *array1 contains at least a non-null element present also in *array2*. If the arrays have no common element and they are both non-empty and either of them contains a null element null is returned, false otherwise
| s | ARRAYS_ZIP(array [, array ]*) | Returns a merged *array* of structs in which the N-th struct contains all N-th values of input arrays
| s | SORT_ARRAY(array [, ascendingOrder]) | Sorts the *array* in ascending or descending order according to the natural ordering of the array elements. The default order is ascending if *ascendingOrder* is not specified. Null elements will be placed at the beginning of the returned array in ascending order or at the end of the returned array in descending order
| p | ASIND(numeric) | Returns the inverse sine of *numeric* in degrees
| * | ASINH(numeric) | Returns the inverse hyperbolic sine of *numeric*
| p | ATAND(numeric) | Returns the inverse tangent of *numeric* in degrees
| * | ATANH(numeric) | Returns the inverse hyperbolic tangent of *numeric*
| f | BITAND_AGG(value) | Equivalent to `BIT_AND(value)`
| f | BITOR_AGG(value) | Equivalent to `BIT_OR(value)`
Expand All @@ -2736,6 +2739,7 @@ In the following:
| b | CONTAINS_SUBSTR(expression, string [ , json_scope =&gt; json_scope_value ]) | Returns whether *string* exists as a substring in *expression*. Optional *json_scope* argument specifies what scope to search if *expression* is in JSON format. Returns NULL if a NULL exists in *expression* that does not result in a match
| q | CONVERT(type, expression [ , style ]) | Equivalent to `CAST(expression AS type)`; ignores the *style* operand
| p r | CONVERT_TIMEZONE(tz1, tz2, datetime) | Converts the timezone of *datetime* from *tz1* to *tz2*
| p | COSD(numeric) | Returns the cosine of *numeric* in degrees
| * | COSH(numeric) | Returns the hyperbolic cosine of *numeric*
| * | COTH(numeric) | Returns the hyperbolic cotangent of *numeric*
| * | CSC(numeric) | Returns the cosecant of *numeric* in radians
Expand Down Expand Up @@ -2870,6 +2874,7 @@ In the following:
| b m p r s | 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
| p | SIND(numeric) | Returns the sine of *numeric* in degrees
| * | SINH(numeric) | Returns the hyperbolic sine of *numeric*
| b m o p r | SOUNDEX(string) | Returns the phonetic representation of *string*; throws if *string* is encoded with multi-byte encoding such as UTF-8
| s | SOUNDEX(string) | Returns the phonetic representation of *string*; return original *string* if *string* is encoded with multi-byte encoding such as UTF-8
Expand All @@ -2880,6 +2885,7 @@ In the following:
| m | STRCMP(string, string) | Returns 0 if both of the strings are same and returns -1 when the first argument is smaller than the second and 1 when the second one is smaller than the first one
| b r p | STRPOS(string, substring) | Equivalent to `POSITION(substring IN string)`
| b m o p r | SUBSTR(string, position [, substringLength ]) | Returns a portion of *string*, beginning at character *position*, *substringLength* characters long. SUBSTR calculates lengths using characters as defined by the input character set
| p | TAND(numeric) | Returns the tangent of *numeric* in degrees
| * | TANH(numeric) | Returns the hyperbolic tangent of *numeric*
| b | TIME(hour, minute, second) | Returns a TIME value *hour*, *minute*, *second* (all of type INTEGER)
| b | TIME(timestamp) | Extracts the TIME from *timestamp* (a local time; BigQuery's DATETIME type)
Expand Down
Loading

0 comments on commit 6534dca

Please sign in to comment.