Skip to content

Commit

Permalink
[FLINK-6893] [table] Add BIN function support
Browse files Browse the repository at this point in the history
This closes apache#4128.
  • Loading branch information
sunjincheng121 authored and twalthr committed Jan 10, 2018
1 parent d42759d commit 2914e59
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 4 deletions.
10 changes: 10 additions & 0 deletions docs/dev/table/sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -1746,6 +1746,16 @@ CONCAT_WS(separator, string1, string2,...)
<p>Returns the string that results from concatenating the arguments using a separator. The separator is added between the strings to be concatenated. Returns NULL If the separator is NULL. CONCAT_WS() does not skip empty strings. However, it does skip any NULL argument. E.g. <code>CONCAT_WS("~", "AA", "BB", "", "CC")</code> returns <code>AA~BB~~CC</code></p>
</td>
</tr>
<tr>
<td>
{% highlight text %}
BIN(numeric)
{% endhighlight %}
</td>
<td>
<p>Returns a string representation of the binary value of Numeric, Returns NULL if Numeric is NULL.</p>
</td>
</tr>

</tbody>
</table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,5 @@ object BuiltInMethods {
Types.lookupMethod(
classOf[ScalarFunctions], "concat_ws", classOf[String], classOf[Array[String]])

val BIN = Types.lookupMethod(classOf[JLong], "toBinaryString", classOf[Long])
}
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,11 @@ object FunctionGenerator {
DOUBLE_TYPE_INFO,
BuiltInMethods.LOG_WITH_BASE)

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

// ----------------------------------------------------------------------------------------------
// Temporal functions
// ----------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -502,6 +507,12 @@ object FunctionGenerator {
new DateFormatCallGen
)

addSqlFunctionMethod(
ScalarSqlFunctions.BIN,
Seq(LONG_TYPE_INFO),
STRING_TYPE_INFO,
BuiltInMethods.BIN)

// ----------------------------------------------------------------------------------------------
// Cryptographic Hash functions
// ----------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* 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}
Expand All @@ -33,6 +34,14 @@ object ScalarSqlFunctions {
OperandTypes.NILADIC,
SqlFunctionCategory.NUMERIC)

val BIN = new SqlFunction(
"BIN",
SqlKind.OTHER_FUNCTION,
ReturnTypes.explicit(SqlTypeName.VARCHAR),
null,
OperandTypes.family(SqlTypeFamily.INTEGER),
SqlFunctionCategory.NUMERIC)

val CONCAT = new SqlFunction(
"CONCAT",
SqlKind.OTHER_FUNCTION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ class BasicOperatorTable extends ReflectiveSqlOperatorTable {
SqlStdOperatorTable.RAND_INTEGER,
ScalarSqlFunctions.CONCAT,
ScalarSqlFunctions.CONCAT_WS,
ScalarSqlFunctions.BIN,
SqlStdOperatorTable.TIMESTAMP_ADD,
ScalarSqlFunctions.LOG,
ScalarSqlFunctions.MD5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,18 @@ class ScalarFunctionsTest extends ScalarTypesTestBase {
"Flink~~~~xx")
}

@Test
def testBin(): Unit = {
testSqlApi("BIN(f2)", "101010")
testSqlApi("BIN(f3)", "101011")
testSqlApi("BIN(f4)", "101100")
testSqlApi("BIN(f7)", "11")
testSqlApi("BIN(12)", "1100")
testSqlApi("BIN(10)", "1010")
testSqlApi("BIN(0)", "0")
testSqlApi("BIN(f32)","1111111111111111111111111111111111111111111111111111111111111111")
}

// ----------------------------------------------------------------------------------------------
// Math functions
// ----------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ScalarFunctionsValidationTest extends ScalarTypesTestBase {
}

@Test(expected = classOf[IllegalArgumentException])
def testInvalidLog2(): Unit ={
def testInvalidLog2(): Unit = {
// invalid arithmetic argument
testSqlApi(
"LOG(-1)",
Expand Down Expand Up @@ -68,17 +68,17 @@ class ScalarFunctionsValidationTest extends ScalarTypesTestBase {
// ----------------------------------------------------------------------------------------------

@Test(expected = classOf[SqlParserException])
def testTimestampAddWithWrongTimestampInterval(): Unit ={
def testTimestampAddWithWrongTimestampInterval(): Unit = {
testSqlApi("TIMESTAMPADD(XXX, 1, timestamp '2016-02-24'))", "2016-06-16")
}

@Test(expected = classOf[SqlParserException])
def testTimestampAddWithWrongTimestampFormat(): Unit ={
def testTimestampAddWithWrongTimestampFormat(): Unit = {
testSqlApi("TIMESTAMPADD(YEAR, 1, timestamp '2016-02-24'))", "2016-06-16")
}

@Test(expected = classOf[ValidationException])
def testTimestampAddWithWrongQuantity(): Unit ={
def testTimestampAddWithWrongQuantity(): Unit = {
testSqlApi("TIMESTAMPADD(YEAR, 1.0, timestamp '2016-02-24 12:42:25')", "2016-06-16")
}

Expand Down Expand Up @@ -112,4 +112,19 @@ class ScalarFunctionsValidationTest extends ScalarTypesTestBase {
"true"
)
}

@Test(expected = classOf[ValidationException])
def testInvalidBin1(): Unit = {
testSqlApi("BIN(f12)", "101010") // float type
}

@Test(expected = classOf[ValidationException])
def testInvalidBin2(): Unit = {
testSqlApi("BIN(f15)", "101010") // BigDecimal type
}

@Test(expected = classOf[ValidationException])
def testInvalidBin3(): Unit = {
testSqlApi("BIN(f16)", "101010") // Date type
}
}

0 comments on commit 2914e59

Please sign in to comment.