From 4d560df50d08f22b68e8b8c9a0e2086f45b5f4b4 Mon Sep 17 00:00:00 2001 From: Zhenghua Gao Date: Thu, 1 Aug 2019 18:40:14 +0800 Subject: [PATCH] [FLINK-13523][table-planner-blink] Refactor DIVIDE function to keep it compatible with old planner The behavior of DIVIDE function in blink planner always return double/decimal type which is not standard. --- .../functions/sql/FlinkSqlOperatorTable.java | 17 +---------------- .../expressions/ReturnTypeInference.scala | 5 +---- .../expressions/ScalarFunctionsTest.scala | 2 +- .../planner/expressions/SqlExpressionTest.scala | 2 +- 4 files changed, 4 insertions(+), 22 deletions(-) diff --git a/flink-table/flink-table-planner-blink/src/main/java/org/apache/flink/table/planner/functions/sql/FlinkSqlOperatorTable.java b/flink-table/flink-table-planner-blink/src/main/java/org/apache/flink/table/planner/functions/sql/FlinkSqlOperatorTable.java index b3d91ae97522b..ba453ff3125e0 100644 --- a/flink-table/flink-table-planner-blink/src/main/java/org/apache/flink/table/planner/functions/sql/FlinkSqlOperatorTable.java +++ b/flink-table/flink-table-planner-blink/src/main/java/org/apache/flink/table/planner/functions/sql/FlinkSqlOperatorTable.java @@ -25,7 +25,6 @@ import org.apache.flink.table.planner.plan.type.RepeatFamilyOperandTypeChecker; import org.apache.calcite.sql.SqlAggFunction; -import org.apache.calcite.sql.SqlBinaryOperator; import org.apache.calcite.sql.SqlFunction; import org.apache.calcite.sql.SqlFunctionCategory; import org.apache.calcite.sql.SqlGroupedWindowFunction; @@ -52,7 +51,6 @@ import static org.apache.flink.table.planner.plan.type.FlinkReturnTypes.ARG0_VARCHAR_FORCE_NULLABLE; import static org.apache.flink.table.planner.plan.type.FlinkReturnTypes.FLINK_DIV_NULLABLE; -import static org.apache.flink.table.planner.plan.type.FlinkReturnTypes.FLINK_QUOTIENT_NULLABLE; import static org.apache.flink.table.planner.plan.type.FlinkReturnTypes.STR_MAP_NULLABLE; import static org.apache.flink.table.planner.plan.type.FlinkReturnTypes.VARCHAR_2000_NULLABLE; @@ -108,20 +106,6 @@ public void lookupOperatorOverloads( // Flink specific built-in scalar SQL functions // ----------------------------------------------------------------------------- - // OPERATORS - - /** - * Arithmetic division operator, '/'. Return DOUBLE or DECIMAL with fractional part. - */ - public static final SqlBinaryOperator DIVIDE = new SqlBinaryOperator( - "/", - SqlKind.DIVIDE, - 60, - true, - FLINK_QUOTIENT_NULLABLE, - InferTypes.FIRST_KNOWN, - OperandTypes.DIVISION_OPERATOR); - // FUNCTIONS /** @@ -953,6 +937,7 @@ public List getAuxiliaryFunctions() { public static final SqlOperator AND = SqlStdOperatorTable.AND; public static final SqlOperator AS = SqlStdOperatorTable.AS; public static final SqlOperator CONCAT = SqlStdOperatorTable.CONCAT; + public static final SqlOperator DIVIDE = SqlStdOperatorTable.DIVIDE; public static final SqlOperator DIVIDE_INTEGER = SqlStdOperatorTable.DIVIDE_INTEGER; public static final SqlOperator DOT = SqlStdOperatorTable.DOT; public static final SqlOperator EQUALS = SqlStdOperatorTable.EQUALS; diff --git a/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/expressions/ReturnTypeInference.scala b/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/expressions/ReturnTypeInference.scala index b2c90b0f44558..59110ab7f2b0c 100644 --- a/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/expressions/ReturnTypeInference.scala +++ b/flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/expressions/ReturnTypeInference.scala @@ -138,10 +138,7 @@ object ReturnTypeInference { fromLogicalTypeToTypeInfo(FlinkTypeFactory.toLogicalType(resultType)) } } - val nonDecimalType = op match { - case _: Div => (_: LogicalType) => BasicTypeInfo.DOUBLE_TYPE_INFO - case _: Mul => (t: LogicalType) => fromLogicalTypeToTypeInfo(t) - } + val nonDecimalType = (t: LogicalType) => fromLogicalTypeToTypeInfo(t) inferBinaryArithmetic(op, decimalFunc, nonDecimalType) } diff --git a/flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/planner/expressions/ScalarFunctionsTest.scala b/flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/planner/expressions/ScalarFunctionsTest.scala index 7c01910095aed..0472cf7824972 100644 --- a/flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/planner/expressions/ScalarFunctionsTest.scala +++ b/flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/planner/expressions/ScalarFunctionsTest.scala @@ -1473,7 +1473,7 @@ class ScalarFunctionsTest extends ScalarTypesTestBase { 1514356320000L / 60000.0, // the `/` is Scala operator, not Flink TableApi operator "1514356320000L / 60000", "1514356320000 / 60000", - "2.5239272E7") + "25239272") testAllApis( 'f7 / 2, diff --git a/flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/planner/expressions/SqlExpressionTest.scala b/flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/planner/expressions/SqlExpressionTest.scala index 287746866c8bc..1e7e53c8b837e 100644 --- a/flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/planner/expressions/SqlExpressionTest.scala +++ b/flink-table/flink-table-planner-blink/src/test/scala/org/apache/flink/table/planner/expressions/SqlExpressionTest.scala @@ -91,7 +91,7 @@ class SqlExpressionTest extends ExpressionTestBase { testSqlApi("5+5", "10") testSqlApi("5-5", "0") testSqlApi("5*5", "25") - testSqlApi("5/5", "1.0") + testSqlApi("5/5", "1") testSqlApi("POWER(5, 5)", "3125.0") testSqlApi("ABS(-5)", "5") testSqlApi("MOD(-26, 5)", "-1")