Skip to content

Commit bff021d

Browse files
wangyumgatorsmile
authored andcommitted
[SPARK-20751][SQL] Add built-in SQL Function - COT
## What changes were proposed in this pull request? Add built-in SQL Function - COT. ## How was this patch tested? unit tests Author: Yuming Wang <wgyumg@gmail.com> Closes #17999 from wangyum/SPARK-20751.
1 parent dba2ca2 commit bff021d

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ object FunctionRegistry {
234234
expression[StringToMap]("str_to_map"),
235235
expression[Sqrt]("sqrt"),
236236
expression[Tan]("tan"),
237+
expression[Cot]("cot"),
237238
expression[Tanh]("tanh"),
238239

239240
expression[Add]("+"),

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,20 @@ case class Sqrt(child: Expression) extends UnaryMathExpression(math.sqrt, "SQRT"
543543
""")
544544
case class Tan(child: Expression) extends UnaryMathExpression(math.tan, "TAN")
545545

546+
@ExpressionDescription(
547+
usage = "_FUNC_(expr) - Returns the cotangent of `expr`.",
548+
extended = """
549+
Examples:
550+
> SELECT _FUNC_(1);
551+
0.6420926159343306
552+
""")
553+
case class Cot(child: Expression)
554+
extends UnaryMathExpression((x: Double) => 1 / math.tan(x), "COT") {
555+
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
556+
defineCodeGen(ctx, ev, c => s"${ev.value} = 1 / java.lang.Math.tan($c);")
557+
}
558+
}
559+
546560
@ExpressionDescription(
547561
usage = "_FUNC_(expr) - Returns the hyperbolic tangent of `expr`.",
548562
extended = """

sql/core/src/test/resources/sql-tests/inputs/operators.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,9 @@ explain select 2 * 4 + 3 || 'b';
5353
explain select 3 + 1 || 'a' || 4 / 2;
5454
explain select 1 == 1 OR 'a' || 'b' == 'ab';
5555
explain select 'a' || 'c' == 'ac' AND 2 == 3;
56+
57+
-- math functions
58+
select cot(1);
59+
select cot(null);
60+
select cot(0);
61+
select cot(-1);

sql/core/src/test/resources/sql-tests/results/operators.sql.out

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- Automatically generated by SQLQueryTestSuite
2-
-- Number of queries: 34
2+
-- Number of queries: 38
33

44

55
-- !query 0
@@ -284,3 +284,35 @@ struct<plan:string>
284284
== Physical Plan ==
285285
*Project [false AS ((concat(a, c) = ac) AND (2 = 3))#x]
286286
+- Scan OneRowRelation[]
287+
288+
289+
-- !query 34
290+
select cot(1)
291+
-- !query 34 schema
292+
struct<COT(CAST(1 AS DOUBLE)):double>
293+
-- !query 34 output
294+
0.6420926159343306
295+
296+
297+
-- !query 35
298+
select cot(null)
299+
-- !query 35 schema
300+
struct<COT(CAST(NULL AS DOUBLE)):double>
301+
-- !query 35 output
302+
NULL
303+
304+
305+
-- !query 36
306+
select cot(0)
307+
-- !query 36 schema
308+
struct<COT(CAST(0 AS DOUBLE)):double>
309+
-- !query 36 output
310+
Infinity
311+
312+
313+
-- !query 37
314+
select cot(-1)
315+
-- !query 37 schema
316+
struct<COT(CAST(-1 AS DOUBLE)):double>
317+
-- !query 37 output
318+
-0.6420926159343306

0 commit comments

Comments
 (0)