This is an automated email from the ASF dual-hosted git repository. xccui pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/flink.git
The following commit(s) were added to refs/heads/master by this push: new 93b93aa [FLINK-9928] Add LOG2 function for table/sql API 93b93aa is described below commit 93b93aa0857c75a6021d3de8910191d36a1c1c18 Author: yanghua <yanghua1...@gmail.com> AuthorDate: Tue Jul 24 19:13:39 2018 +0800 [FLINK-9928] Add LOG2 function for table/sql API This closes #6404. --- docs/dev/table/sql.md | 11 +++++++++ docs/dev/table/tableApi.md | 11 +++++++++ .../flink/table/api/scala/expressionDsl.scala | 5 ++++ .../flink/table/codegen/calls/BuiltInMethods.scala | 2 ++ .../table/codegen/calls/FunctionGenerator.scala | 6 +++++ .../flink/table/expressions/mathExpressions.scala | 12 ++++++++++ .../table/functions/sql/ScalarSqlFunctions.scala | 9 ++++++++ .../table/runtime/functions/ScalarFunctions.scala | 11 +++++++++ .../flink/table/validate/FunctionCatalog.scala | 2 ++ .../table/expressions/ScalarFunctionsTest.scala | 27 ++++++++++++++++++++++ 10 files changed, 96 insertions(+) diff --git a/docs/dev/table/sql.md b/docs/dev/table/sql.md index 1ed06f0..366e3fd 100644 --- a/docs/dev/table/sql.md +++ b/docs/dev/table/sql.md @@ -1411,6 +1411,17 @@ LOG10(numeric) <tr> <td> + {% highlight text %} +LOG2(numeric) +{% endhighlight %} + </td> + <td> + <p>Returns the base 2 logarithm of <i>numeric</i>.</p> + </td> + </tr> + + <tr> + <td> {% highlight text %} LOG(x numeric) LOG(b numeric, x numeric) diff --git a/docs/dev/table/tableApi.md b/docs/dev/table/tableApi.md index b1b8f60..6e202f1 100644 --- a/docs/dev/table/tableApi.md +++ b/docs/dev/table/tableApi.md @@ -2070,6 +2070,17 @@ NUMERIC.log10() <tr> <td> {% highlight java %} +NUMERIC.log2() +{% endhighlight %} + </td> + <td> + <p>Calculates the base 2 logarithm of given value.</p> + </td> + </tr> + + <tr> + <td> + {% highlight java %} numeric1.log() numeric1.log(numeric2) {% endhighlight %} diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/api/scala/expressionDsl.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/api/scala/expressionDsl.scala index 62c62b1..35d2167 100644 --- a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/api/scala/expressionDsl.scala +++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/api/scala/expressionDsl.scala @@ -301,6 +301,11 @@ trait ImplicitExpressionOperations { def log10() = Log10(expr) /** + * Calculates the base 2 logarithm of the given value. + */ + def log2() = Log2(expr) + + /** * Calculates the natural logarithm of the given value. */ def ln() = Ln(expr) diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/codegen/calls/BuiltInMethods.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/codegen/calls/BuiltInMethods.scala index 22298da..0e0f709 100644 --- a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/codegen/calls/BuiltInMethods.scala +++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/codegen/calls/BuiltInMethods.scala @@ -33,6 +33,8 @@ object BuiltInMethods { val LOG10 = Types.lookupMethod(classOf[Math], "log10", classOf[Double]) + val LOG2 = Types.lookupMethod(classOf[ScalarFunctions], "log2", classOf[Double]) + val EXP = Types.lookupMethod(classOf[Math], "exp", classOf[Double]) val POWER = Types.lookupMethod(classOf[Math], "pow", classOf[Double], classOf[Double]) diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/codegen/calls/FunctionGenerator.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/codegen/calls/FunctionGenerator.scala index d264cce..a5c275a 100644 --- a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/codegen/calls/FunctionGenerator.scala +++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/codegen/calls/FunctionGenerator.scala @@ -163,6 +163,12 @@ object FunctionGenerator { BuiltInMethods.LOG10) addSqlFunctionMethod( + LOG2, + Seq(DOUBLE_TYPE_INFO), + DOUBLE_TYPE_INFO, + BuiltInMethods.LOG2) + + addSqlFunctionMethod( LN, Seq(DOUBLE_TYPE_INFO), DOUBLE_TYPE_INFO, diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/expressions/mathExpressions.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/expressions/mathExpressions.scala index 0378ce5..10ba007 100644 --- a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/expressions/mathExpressions.scala +++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/expressions/mathExpressions.scala @@ -92,6 +92,18 @@ case class Log10(child: Expression) extends UnaryExpression with InputTypeSpec { } } +case class Log2(child: Expression) extends UnaryExpression with InputTypeSpec { + override private[flink] def expectedTypes: Seq[TypeInformation[_]] = DOUBLE_TYPE_INFO :: Nil + + override private[flink] def resultType: TypeInformation[_] = DOUBLE_TYPE_INFO + + override private[flink] def toRexNode(implicit relBuilder: RelBuilder) = { + relBuilder.call(ScalarSqlFunctions.LOG2, child.toRexNode) + } + + override def toString: String = s"log2($child)" +} + case class Log(base: Expression, antilogarithm: Expression) extends Expression with InputTypeSpec { def this(antilogarithm: Expression) = this(null, antilogarithm) diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/functions/sql/ScalarSqlFunctions.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/functions/sql/ScalarSqlFunctions.scala index 1af1e68..49fda9d 100644 --- a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/functions/sql/ScalarSqlFunctions.scala +++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/functions/sql/ScalarSqlFunctions.scala @@ -67,6 +67,15 @@ object ScalarSqlFunctions { OperandTypes.family(SqlTypeFamily.NUMERIC, SqlTypeFamily.NUMERIC)), SqlFunctionCategory.NUMERIC) + val LOG2 = new SqlFunction( + "LOG2", + SqlKind.OTHER_FUNCTION, + ReturnTypes.DOUBLE_NULLABLE, + null, + OperandTypes.NUMERIC, + SqlFunctionCategory.NUMERIC + ) + val LPAD = new SqlFunction( "LPAD", SqlKind.OTHER_FUNCTION, diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/runtime/functions/ScalarFunctions.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/runtime/functions/ScalarFunctions.scala index 40f1ec3..50e8f9c 100644 --- a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/runtime/functions/ScalarFunctions.scala +++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/runtime/functions/ScalarFunctions.scala @@ -111,6 +111,17 @@ object ScalarFunctions { } /** + * Returns the logarithm of "x" with base 2. + */ + def log2(x: Double): Double = { + if (x <= 0.0) { + throw new IllegalArgumentException(s"x of 'log2(x)' must be > 0, but x = $x") + } else { + Math.log(x) / Math.log(2) + } + } + + /** * Returns the string str left-padded with the string pad to a length of len characters. * If str is longer than len, the return value is shortened to len characters. */ diff --git a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/validate/FunctionCatalog.scala b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/validate/FunctionCatalog.scala index b4f0424..8a91340 100644 --- a/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/validate/FunctionCatalog.scala +++ b/flink-libraries/flink-table/src/main/scala/org/apache/flink/table/validate/FunctionCatalog.scala @@ -214,6 +214,7 @@ object FunctionCatalog { "exp" -> classOf[Exp], "floor" -> classOf[Floor], "log10" -> classOf[Log10], + "log2" -> classOf[Log2], "ln" -> classOf[Ln], "log" -> classOf[Log], "power" -> classOf[Power], @@ -398,6 +399,7 @@ class BasicOperatorTable extends ReflectiveSqlOperatorTable { SqlStdOperatorTable.MOD, SqlStdOperatorTable.LN, SqlStdOperatorTable.LOG10, + ScalarSqlFunctions.LOG2, SqlStdOperatorTable.ABS, SqlStdOperatorTable.EXP, SqlStdOperatorTable.NULLIF, diff --git a/flink-libraries/flink-table/src/test/scala/org/apache/flink/table/expressions/ScalarFunctionsTest.scala b/flink-libraries/flink-table/src/test/scala/org/apache/flink/table/expressions/ScalarFunctionsTest.scala index 995762a..4eb2e33 100644 --- a/flink-libraries/flink-table/src/test/scala/org/apache/flink/table/expressions/ScalarFunctionsTest.scala +++ b/flink-libraries/flink-table/src/test/scala/org/apache/flink/table/expressions/ScalarFunctionsTest.scala @@ -576,6 +576,33 @@ class ScalarFunctionsTest extends ScalarTypesTestBase { } @Test + def testLog2(): Unit = { + testAllApis( + 'f6.log2(), + "f6.log2", + "LOG2(f6)", + "2.2016338611696504") + + testAllApis( + ('f6 - 'f6 + 100).log2(), + "(f6 - f6 + 100).log2()", + "LOG2(f6 - f6 + 100)", + "6.643856189774725") + + testAllApis( + ('f6 + 20).log2(), + "(f6+20).log2", + "LOG2(f6+20)", + "4.620586410451877") + + testAllApis( + 10.log2(), + "10.log2", + "LOG2(10)", + "3.3219280948873626") + } + + @Test def testPower(): Unit = { // f7: int , f4: long, f6: double testAllApis(