Skip to content

Commit

Permalink
[FLINK-6960] [table] Support E() on SQL.
Browse files Browse the repository at this point in the history
This closes apache#4152
  • Loading branch information
sunjincheng121 authored and wuchong committed Jun 25, 2017
1 parent a13a98f commit 524a1fa
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
10 changes: 10 additions & 0 deletions docs/dev/table/sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,16 @@ PI()
<p>Returns a value that is closer than any other value to pi.</p>
</td>
</tr>
<tr>
<td>
{% highlight text %}
E()
{% endhighlight %}
</td>
<td>
<p>Returns a value that is closer than any other value to e.</p>
</td>
</tr>

<tr>
<td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ import org.apache.calcite.util.BuiltInMethod
import org.apache.flink.api.common.typeinfo.BasicTypeInfo._
import org.apache.flink.api.common.typeinfo.{BasicTypeInfo, SqlTimeTypeInfo, TypeInformation}
import org.apache.flink.api.java.typeutils.GenericTypeInfo
import org.apache.flink.table.codegen.{CodeGenerator, GeneratedExpression}
import org.apache.flink.table.functions.utils.{ScalarSqlFunction, TableSqlFunction}

import org.apache.flink.table.functions.sql.ScalarSqlFunctions._
import scala.collection.mutable

/**
Expand Down Expand Up @@ -395,6 +394,11 @@ object FunctionGenerator {
Seq(INT_TYPE_INFO, INT_TYPE_INFO),
new RandCallGen(isRandInteger = true, hasSeed = true))

addSqlFunction(
E,
Seq(),
new ConstantCallGen(DOUBLE_TYPE_INFO, Math.E.toString))

// ----------------------------------------------------------------------------------------------
// Temporal functions
// ----------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.flink.table.functions.sql

import org.apache.calcite.sql.{SqlFunction, SqlFunctionCategory, SqlKind}
import org.apache.calcite.sql.`type`._

/**
* All build-in scalar sql functions.
*/
object ScalarSqlFunctions {
val E = new SqlFunction(
"E",
SqlKind.OTHER_FUNCTION,
ReturnTypes.DOUBLE,
null,
OperandTypes.NILADIC,
SqlFunctionCategory.NUMERIC)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.apache.calcite.sql.util.{ChainedSqlOperatorTable, ListSqlOperatorTabl
import org.apache.calcite.sql.{SqlFunction, SqlOperator, SqlOperatorTable}
import org.apache.flink.table.api._
import org.apache.flink.table.expressions._
import org.apache.flink.table.functions.sql.ScalarSqlFunctions
import org.apache.flink.table.functions.utils.{AggSqlFunction, ScalarSqlFunction, TableSqlFunction}
import org.apache.flink.table.functions.{AggregateFunction, ScalarFunction, TableFunction}

Expand Down Expand Up @@ -391,6 +392,8 @@ class BasicOperatorTable extends ReflectiveSqlOperatorTable {
SqlStdOperatorTable.PI,
SqlStdOperatorTable.RAND,
SqlStdOperatorTable.RAND_INTEGER,
ScalarSqlFunctions.E,

// EXTENSIONS
SqlStdOperatorTable.TUMBLE,
SqlStdOperatorTable.TUMBLE_START,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,17 @@ class ScalarFunctionsTest extends ExpressionTestBase {
random4.nextInt(44).toString)
}

@Test
def testE(): Unit = {
testSqlApi(
"E()",
math.E.toString)

testSqlApi(
"e()",
math.E.toString)
}

// ----------------------------------------------------------------------------------------------
// Temporal functions
// ----------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 524a1fa

Please sign in to comment.