Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -543,6 +543,28 @@ 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 = {
nullSafeCodeGen(ctx, ev, c =>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please use defineCodeGen

s"""
if (${ev.value} == null) {
${ev.isNull} = true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Internally, it is already nullSafeCodeGen, and thus, this is not needed.

} else {
${ev.value} = 1 / java.lang.Math.tan($c);
}
"""
)

Copy link
Copy Markdown
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
16 changes: 16 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,22 @@ object functions {
*/
def tan(columnName: String): Column = tan(Column(columnName))

/**
* Computes the cotangent of the given column.
*
* @group math_funcs
* @since 2.2.0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: 2.3.0?

*/
def cot(e: Column): Column = withExpr { Cot(e.expr) }

Copy link
Copy Markdown
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 cotangent of the given column name.
*
* @group math_funcs
* @since 2.2.0
*/
def cot(colName: String): Column = cot(Column(colName))

/**

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure though, do we strictly need to add this function in this file? If we add this in FunctionRegistry, you know we can use this thru selectExpr. ISTM this file seems to include frequently-used functions only. cc: @gatorsmile

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sure. Let us keep this file untouched in this PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We do not add these functions here in this pr. See: #17999 (diff)

* Computes the hyperbolic tangent of the given value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ class MathFunctionsSuite extends QueryTest with SharedSQLContext {
Row(1, -1))
}

test("cot") {
testOneToOneMathFunction(cot, (d: Double) => 1 / math.tan(d))
}

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

Expand Down