Skip to content

Commit

Permalink
[FLINK-13523][table-planner-blink] Refactor DIVIDE function to keep i…
Browse files Browse the repository at this point in the history
…t compatible with old planner

The behavior of DIVIDE function in blink planner always return double/decimal type which is not standard.
  • Loading branch information
docete authored and wuchong committed Aug 9, 2019
1 parent d544d30 commit 4d560df
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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

/**
Expand Down Expand Up @@ -953,6 +937,7 @@ public List<SqlGroupedWindowFunction> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 4d560df

Please sign in to comment.