Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ object FunctionRegistry {
expression[StringToMap]("str_to_map"),
expression[Sqrt]("sqrt"),
expression[Tan]("tan"),
expression[Cot]("cot"),
expression[Tanh]("tanh"),

expression[Add]("+"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,24 @@ case class Sqrt(child: Expression) extends UnaryMathExpression(math.sqrt, "SQRT"
""")
case class Tan(child: Expression) extends UnaryMathExpression(math.tan, "TAN")

@ExpressionDescription(
usage = "_FUNC_(expr) - Returns the cotangent of `expr`.",
extended = """
Examples:
> SELECT _FUNC_(1);
0.6420926159343306
""")
case class Cot(child: Expression)
extends UnaryMathExpression((x: Double) => 1 / math.tan(x), "COT") {
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
defineCodeGen(ctx, ev, c =>
s"""
${ev.value} = 1 / java.lang.Math.tan($c);
"""
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it fits one line, could you change it to?

defineCodeGen(ctx, ev, c => s"${ev.value} = 1 / java.lang.Math.tan($c);")

}
}

@ExpressionDescription(
usage = "_FUNC_(expr) - Returns the hyperbolic tangent of `expr`.",
extended = """
Expand Down
8 changes: 8 additions & 0 deletions sql/core/src/main/scala/org/apache/spark/sql/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1990,6 +1990,14 @@ object functions {
*/
def tan(columnName: String): Column = tan(Column(columnName))

/**
* Computes the cotangent of the given column.
*
* @group math_funcs
* @since 2.3.0
*/
def cot(e: Column): Column = withExpr { Cot(e.expr) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let us remove this too. Thanks!


/**
* Computes the hyperbolic tangent of the given value.
*
Expand Down
1 change: 1 addition & 0 deletions sql/core/src/test/resources/sql-tests/inputs/operators.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ select 1 - 2;
select 2 * 5;
select 5 % 3;
select pmod(-7, 3);
select cot(1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this to the end of this .sql file? It can reduce the line of code changes.


-- check operator precedence.
-- We follow Oracle operator precedence in the table below that lists the levels of precedence
Expand Down
38 changes: 23 additions & 15 deletions sql/core/src/test/resources/sql-tests/results/operators.sql.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 34
-- Number of queries: 35


-- !query 0
Expand Down Expand Up @@ -227,60 +227,68 @@ struct<pmod(-7, 3):int>


-- !query 28
explain select 'a' || 1 + 2
select cot(1)
-- !query 28 schema
struct<plan:string>
struct<COT(CAST(1 AS DOUBLE)):double>
-- !query 28 output
== Physical Plan ==
*Project [null AS (CAST(concat(a, CAST(1 AS STRING)) AS DOUBLE) + CAST(2 AS DOUBLE))#x]
+- Scan OneRowRelation[]
0.6420926159343306


-- !query 29
explain select 1 - 2 || 'b'
explain select 'a' || 1 + 2
-- !query 29 schema
struct<plan:string>
-- !query 29 output
== Physical Plan ==
*Project [-1b AS concat(CAST((1 - 2) AS STRING), b)#x]
*Project [null AS (CAST(concat(a, CAST(1 AS STRING)) AS DOUBLE) + CAST(2 AS DOUBLE))#x]
+- Scan OneRowRelation[]


-- !query 30
explain select 2 * 4 + 3 || 'b'
explain select 1 - 2 || 'b'
-- !query 30 schema
struct<plan:string>
-- !query 30 output
== Physical Plan ==
*Project [11b AS concat(CAST(((2 * 4) + 3) AS STRING), b)#x]
*Project [-1b AS concat(CAST((1 - 2) AS STRING), b)#x]
+- Scan OneRowRelation[]


-- !query 31
explain select 3 + 1 || 'a' || 4 / 2
explain select 2 * 4 + 3 || 'b'
-- !query 31 schema
struct<plan:string>
-- !query 31 output
== Physical Plan ==
*Project [4a2.0 AS concat(concat(CAST((3 + 1) AS STRING), a), CAST((CAST(4 AS DOUBLE) / CAST(2 AS DOUBLE)) AS STRING))#x]
*Project [11b AS concat(CAST(((2 * 4) + 3) AS STRING), b)#x]
+- Scan OneRowRelation[]


-- !query 32
explain select 1 == 1 OR 'a' || 'b' == 'ab'
explain select 3 + 1 || 'a' || 4 / 2
-- !query 32 schema
struct<plan:string>
-- !query 32 output
== Physical Plan ==
*Project [true AS ((1 = 1) OR (concat(a, b) = ab))#x]
*Project [4a2.0 AS concat(concat(CAST((3 + 1) AS STRING), a), CAST((CAST(4 AS DOUBLE) / CAST(2 AS DOUBLE)) AS STRING))#x]
+- Scan OneRowRelation[]


-- !query 33
explain select 'a' || 'c' == 'ac' AND 2 == 3
explain select 1 == 1 OR 'a' || 'b' == 'ab'
-- !query 33 schema
struct<plan:string>
-- !query 33 output
== Physical Plan ==
*Project [true AS ((1 = 1) OR (concat(a, b) = ab))#x]
+- Scan OneRowRelation[]


-- !query 34
explain select 'a' || 'c' == 'ac' AND 2 == 3
-- !query 34 schema
struct<plan:string>
-- !query 34 output
== Physical Plan ==
*Project [false AS ((concat(a, c) = ac) AND (2 = 3))#x]
+- Scan OneRowRelation[]
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ class MathFunctionsSuite extends QueryTest with SharedSQLContext {
Row(1, -1))
}

test("cot") {
testOneToOneMathFunction(cot, (d: Double) => 1 / math.tan(d))
checkAnswer(
sql(s"SELECT cot(null), cot(0), cot(-1), cot(${Double.MaxValue})"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move these tests to operators.sql?

Row(null, Double.PositiveInfinity, -0.6420926159343306, -201.53099572900314))
}

test("pow / power") {
testTwoToOneMathFunction(pow, pow, math.pow)

Expand Down