spark-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lix...@apache.org
Subject spark git commit: [SPARK-20751][SQL] Add built-in SQL Function - COT
Date Fri, 19 May 2017 16:40:25 GMT
Repository: spark
Updated Branches:
  refs/heads/master dba2ca2c1 -> bff021dfa


[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.


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/bff021df
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/bff021df
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/bff021df

Branch: refs/heads/master
Commit: bff021dfaf4b5c9ab84c71dad2a37f60907e32d6
Parents: dba2ca2
Author: Yuming Wang <wgyumg@gmail.com>
Authored: Fri May 19 09:40:22 2017 -0700
Committer: Xiao Li <gatorsmile@gmail.com>
Committed: Fri May 19 09:40:22 2017 -0700

----------------------------------------------------------------------
 .../catalyst/analysis/FunctionRegistry.scala    |  1 +
 .../catalyst/expressions/mathExpressions.scala  | 14 ++++++++
 .../resources/sql-tests/inputs/operators.sql    |  6 ++++
 .../sql-tests/results/operators.sql.out         | 34 +++++++++++++++++++-
 4 files changed, 54 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/bff021df/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala
----------------------------------------------------------------------
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala
index 6fc154f..d2042ad 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala
@@ -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]("+"),

http://git-wip-us.apache.org/repos/asf/spark/blob/bff021df/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala
----------------------------------------------------------------------
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala
index de1a46d..a7bf81e 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala
@@ -544,6 +544,20 @@ 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);")
+  }
+}
+
+@ExpressionDescription(
   usage = "_FUNC_(expr) - Returns the hyperbolic tangent of `expr`.",
   extended = """
     Examples:

http://git-wip-us.apache.org/repos/asf/spark/blob/bff021df/sql/core/src/test/resources/sql-tests/inputs/operators.sql
----------------------------------------------------------------------
diff --git a/sql/core/src/test/resources/sql-tests/inputs/operators.sql b/sql/core/src/test/resources/sql-tests/inputs/operators.sql
index 6339d69..1920a10 100644
--- a/sql/core/src/test/resources/sql-tests/inputs/operators.sql
+++ b/sql/core/src/test/resources/sql-tests/inputs/operators.sql
@@ -53,3 +53,9 @@ explain select 2 * 4  + 3 || 'b';
 explain select 3 + 1 || 'a' || 4 / 2;
 explain select 1 == 1 OR 'a' || 'b' ==  'ab';
 explain select 'a' || 'c' == 'ac' AND 2 == 3;
+
+-- math functions
+select cot(1);
+select cot(null);
+select cot(0);
+select cot(-1);

http://git-wip-us.apache.org/repos/asf/spark/blob/bff021df/sql/core/src/test/resources/sql-tests/results/operators.sql.out
----------------------------------------------------------------------
diff --git a/sql/core/src/test/resources/sql-tests/results/operators.sql.out b/sql/core/src/test/resources/sql-tests/results/operators.sql.out
index e0236f4..abd1821 100644
--- a/sql/core/src/test/resources/sql-tests/results/operators.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/operators.sql.out
@@ -1,5 +1,5 @@
 -- Automatically generated by SQLQueryTestSuite
--- Number of queries: 34
+-- Number of queries: 38
 
 
 -- !query 0
@@ -284,3 +284,35 @@ struct<plan:string>
 == Physical Plan ==
 *Project [false AS ((concat(a, c) = ac) AND (2 = 3))#x]
 +- Scan OneRowRelation[]
+
+
+-- !query 34
+select cot(1)
+-- !query 34 schema
+struct<COT(CAST(1 AS DOUBLE)):double>
+-- !query 34 output
+0.6420926159343306
+
+
+-- !query 35
+select cot(null)
+-- !query 35 schema
+struct<COT(CAST(NULL AS DOUBLE)):double>
+-- !query 35 output
+NULL
+
+
+-- !query 36
+select cot(0)
+-- !query 36 schema
+struct<COT(CAST(0 AS DOUBLE)):double>
+-- !query 36 output
+Infinity
+
+
+-- !query 37
+select cot(-1)
+-- !query 37 schema
+struct<COT(CAST(-1 AS DOUBLE)):double>
+-- !query 37 output
+-0.6420926159343306


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org


Mime
View raw message