Repository: flink
Updated Branches:
  refs/heads/master 9420a775f -> 5fdc72069


[FLINK-3497] [table] Add SQRT function


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

Branch: refs/heads/master
Commit: 5fdc7206936248dca3310b78206246c9f9c5b9a8
Parents: 9420a77
Author: twalthr <twal...@apache.org>
Authored: Thu Sep 15 12:01:34 2016 +0200
Committer: twalthr <twal...@apache.org>
Committed: Thu Sep 15 12:01:34 2016 +0200

----------------------------------------------------------------------
 docs/dev/table_api.md                           | 22 ++++++++++++++++++++
 .../flink/api/scala/table/expressionDsl.scala   |  5 +++++
 .../table/codegen/calls/BuiltInMethods.scala    |  2 ++
 .../table/codegen/calls/ScalarFunctions.scala   |  6 ++++++
 .../api/table/expressions/mathExpressions.scala | 13 ++++++++++++
 .../api/table/validate/FunctionCatalog.scala    |  1 +
 .../table/expressions/ScalarFunctionsTest.scala | 15 +++++++++++++
 7 files changed, 64 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/5fdc7206/docs/dev/table_api.md
----------------------------------------------------------------------
diff --git a/docs/dev/table_api.md b/docs/dev/table_api.md
index b419bfb..fe3ddc3 100644
--- a/docs/dev/table_api.md
+++ b/docs/dev/table_api.md
@@ -1323,6 +1323,17 @@ NUMERIC.power(NUMERIC)
     <tr>
       <td>
         {% highlight java %}
+NUMERIC.sqrt()
+{% endhighlight %}
+      </td>
+      <td>
+        <p>Calculates the square root of a given value.</p>
+      </td>
+    </tr>
+
+    <tr>
+      <td>
+        {% highlight java %}
 NUMERIC.abs()
 {% endhighlight %}
       </td>
@@ -1720,6 +1731,17 @@ NUMERIC.power(NUMERIC)
     <tr>
       <td>
         {% highlight scala %}
+NUMERIC.sqrt()
+{% endhighlight %}
+      </td>
+      <td>
+        <p>Calculates the square root of a given value.</p>
+      </td>
+    </tr>
+
+    <tr>
+      <td>
+        {% highlight scala %}
 NUMERIC.abs()
 {% endhighlight %}
       </td>

http://git-wip-us.apache.org/repos/asf/flink/blob/5fdc7206/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/scala/table/expressionDsl.scala
----------------------------------------------------------------------
diff --git 
a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/scala/table/expressionDsl.scala
 
b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/scala/table/expressionDsl.scala
index 61c6a65..003b8b2 100644
--- 
a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/scala/table/expressionDsl.scala
+++ 
b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/scala/table/expressionDsl.scala
@@ -133,6 +133,11 @@ trait ImplicitExpressionOperations {
   def power(other: Expression) = Power(expr, other)
 
   /**
+    * Calculates the square root of a given value.
+    */
+  def sqrt() = Sqrt(expr)
+
+  /**
     * Calculates the absolute value of given value.
     */
   def abs() = Abs(expr)

http://git-wip-us.apache.org/repos/asf/flink/blob/5fdc7206/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/BuiltInMethods.scala
----------------------------------------------------------------------
diff --git 
a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/BuiltInMethods.scala
 
b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/BuiltInMethods.scala
index c3fbed3..a2715f0 100644
--- 
a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/BuiltInMethods.scala
+++ 
b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/BuiltInMethods.scala
@@ -26,6 +26,8 @@ object BuiltInMethods {
   val LOG10 = Types.lookupMethod(classOf[Math], "log10", classOf[Double])
   val EXP = Types.lookupMethod(classOf[Math], "exp", classOf[Double])
   val POWER = Types.lookupMethod(classOf[Math], "pow", classOf[Double], 
classOf[Double])
+  val POWER_DEC = Types.lookupMethod(
+    classOf[SqlFunctions], "power", classOf[Long], classOf[JBigDecimal])
   val LN = Types.lookupMethod(classOf[Math], "log", classOf[Double])
   val ABS = Types.lookupMethod(classOf[SqlFunctions], "abs", classOf[Double])
   val ABS_DEC = Types.lookupMethod(classOf[SqlFunctions], "abs", 
classOf[JBigDecimal])

http://git-wip-us.apache.org/repos/asf/flink/blob/5fdc7206/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/ScalarFunctions.scala
----------------------------------------------------------------------
diff --git 
a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/ScalarFunctions.scala
 
b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/ScalarFunctions.scala
index 90a6bd5..a39829c 100644
--- 
a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/ScalarFunctions.scala
+++ 
b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/codegen/calls/ScalarFunctions.scala
@@ -159,6 +159,12 @@ object ScalarFunctions {
     DOUBLE_TYPE_INFO,
     BuiltInMethods.POWER)
 
+  addSqlFunctionMethod(
+    POWER,
+    Seq(DOUBLE_TYPE_INFO, BIG_DEC_TYPE_INFO),
+    DOUBLE_TYPE_INFO,
+    BuiltInMethods.POWER_DEC)
+
   addSqlFunction(
     ABS,
     Seq(DOUBLE_TYPE_INFO),

http://git-wip-us.apache.org/repos/asf/flink/blob/5fdc7206/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/mathExpressions.scala
----------------------------------------------------------------------
diff --git 
a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/mathExpressions.scala
 
b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/mathExpressions.scala
index 5f0acdf..80adf5f 100644
--- 
a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/mathExpressions.scala
+++ 
b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/expressions/mathExpressions.scala
@@ -114,3 +114,16 @@ case class Power(left: Expression, right: Expression) 
extends BinaryExpression w
     relBuilder.call(SqlStdOperatorTable.POWER, left.toRexNode, right.toRexNode)
   }
 }
+
+case class Sqrt(child: Expression) extends UnaryExpression with InputTypeSpec {
+  override private[flink] def resultType: TypeInformation[_] = DOUBLE_TYPE_INFO
+
+  override private[flink] def expectedTypes: Seq[TypeInformation[_]] =
+    Seq(DOUBLE_TYPE_INFO)
+
+  override def toString: String = s"sqrt($child)"
+
+  override private[flink] def toRexNode(implicit relBuilder: RelBuilder): 
RexNode = {
+    relBuilder.call(SqlStdOperatorTable.POWER, child.toRexNode, 
Literal(0.5).toRexNode)
+  }
+}

http://git-wip-us.apache.org/repos/asf/flink/blob/5fdc7206/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/validate/FunctionCatalog.scala
----------------------------------------------------------------------
diff --git 
a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/validate/FunctionCatalog.scala
 
b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/validate/FunctionCatalog.scala
index f818e14..e8a2971 100644
--- 
a/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/validate/FunctionCatalog.scala
+++ 
b/flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/validate/FunctionCatalog.scala
@@ -151,6 +151,7 @@ object FunctionCatalog {
     "ln" -> classOf[Ln],
     "power" -> classOf[Power],
     "mod" -> classOf[Mod],
+    "sqrt" -> classOf[Sqrt],
 
     // temporal functions
     "extract" -> classOf[Extract],

http://git-wip-us.apache.org/repos/asf/flink/blob/5fdc7206/flink-libraries/flink-table/src/test/scala/org/apache/flink/api/table/expressions/ScalarFunctionsTest.scala
----------------------------------------------------------------------
diff --git 
a/flink-libraries/flink-table/src/test/scala/org/apache/flink/api/table/expressions/ScalarFunctionsTest.scala
 
b/flink-libraries/flink-table/src/test/scala/org/apache/flink/api/table/expressions/ScalarFunctionsTest.scala
index c9485fe..672b876 100644
--- 
a/flink-libraries/flink-table/src/test/scala/org/apache/flink/api/table/expressions/ScalarFunctionsTest.scala
+++ 
b/flink-libraries/flink-table/src/test/scala/org/apache/flink/api/table/expressions/ScalarFunctionsTest.scala
@@ -358,6 +358,21 @@ class ScalarFunctionsTest extends ExpressionTestBase {
   }
 
   @Test
+  def testSqrt(): Unit = {
+    testAllApis(
+      25.sqrt(),
+      "25.sqrt()",
+      "SQRT(25)",
+      "5.0")
+
+    testAllApis(
+      2.2.sqrt(),
+      "2.2.sqrt()",
+      "POWER(CAST(2.2 AS DOUBLE), CAST(0.5 AS DOUBLE))", // TODO fix FLINK-4621
+      math.sqrt(2.2).toString)
+  }
+
+  @Test
   def testLn(): Unit = {
     testAllApis(
       'f2.ln(),

Reply via email to