JingsongLi commented on a change in pull request #9152: [FLINK-13314][table-planner-blink] Correct resultType of some PlannerExpression when operands contains DecimalTypeInfo or BigDecimalTypeInfo in Blink planner URL: https://github.com/apache/flink/pull/9152#discussion_r304722257
########## File path: flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/expressions/ReturnTypeInference.scala ########## @@ -0,0 +1,216 @@ +/* + * 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.expressions + +import org.apache.flink.api.common.typeinfo.{BasicTypeInfo, TypeInformation} +import org.apache.flink.table.api.TableException +import org.apache.flink.table.calcite.{FlinkTypeFactory, FlinkTypeSystem} +import org.apache.flink.table.types.TypeInfoLogicalTypeConverter.{fromLogicalTypeToTypeInfo, fromTypeInfoToLogicalType} +import org.apache.flink.table.types.logical.{DecimalType, LogicalType} +import org.apache.flink.table.typeutils.{BigDecimalTypeInfo, DecimalTypeInfo, TypeCoercion} + +import org.apache.calcite.rel.`type`.RelDataType +import org.apache.calcite.sql.`type`.SqlTypeUtil + +import scala.collection.JavaConverters._ + +object ReturnTypeInference { + + private lazy val typeSystem = new FlinkTypeSystem + private lazy val typeFactory = new FlinkTypeFactory(typeSystem) + + /** + * Infer resultType of [[Minus]] expression. + * The decimal type inference keeps consistent with Calcite + * [[org.apache.calcite.sql.type.ReturnTypes]].NULLABLE_SUM + * + * @param minus minus Expression + * @return result type + */ + def infer(minus: Minus): TypeInformation[_] = inferSum(minus) + + /** + * Infer resultType of [[Plus]] expression. + * The decimal type inference keeps consistent with Calcite + * [[org.apache.calcite.sql.type.ReturnTypes]].NULLABLE_SUM + * + * @param plus plus Expression + * @return result type + */ + def infer(plus: Plus): TypeInformation[_] = inferSum(plus) + + private def inferSum(op: BinaryArithmetic): TypeInformation[_] = { + val decimalTypeInference = ( + leftType: RelDataType, + rightType: RelDataType, + wideResultType: LogicalType) => { + if (SqlTypeUtil.isExactNumeric(leftType) && + SqlTypeUtil.isExactNumeric(rightType) && + (SqlTypeUtil.isDecimal(leftType) || SqlTypeUtil.isDecimal(rightType))) { + val lp = leftType.getPrecision + val ls = leftType.getScale + val rp = rightType.getPrecision + val rs = rightType.getScale + val scale = Math.max(ls, rs) + assert(scale <= typeSystem.getMaxNumericScale) + var precision = Math.max(lp - ls, rp - rs) + scale + 1 + precision = Math.min(precision, typeSystem.getMaxNumericPrecision) + assert(precision > 0) + fromLogicalTypeToTypeInfo(wideResultType) match { + case _: DecimalTypeInfo => DecimalTypeInfo.of(precision, scale) + case _: BigDecimalTypeInfo => BigDecimalTypeInfo.of(precision, scale) + } + } else { + val resultType = typeFactory.leastRestrictive( + List(leftType, rightType).asJava) + fromLogicalTypeToTypeInfo(FlinkTypeFactory.toLogicalType(resultType)) + } + } + infer(op, decimalTypeInference) + } + + /** + * Infer resultType of [[Mul]] expression. + * The decimal type inference keeps consistent with Calcite + * [[org.apache.calcite.sql.type.ReturnTypes]].PRODUCT_NULLABLE + * + * @param mul mul Expression + * @return result type + */ + def infer(mul: Mul): TypeInformation[_] = { + val decimalTypeInference = ( + leftType: RelDataType, + rightType: RelDataType) => typeFactory.createDecimalProduct(leftType, rightType) + inferDivOrMul(mul, decimalTypeInference) + } + + /** + * Infer resultType of [[Div]] expression. + * The decimal type inference keeps consistent with + * [[org.apache.flink.table.calcite.type.FlinkReturnTypes]].FLINK_QUOTIENT_NULLABLE + * + * @param div div Expression + * @return result type + */ + def infer(div: Div): TypeInformation[_] = { + val decimalTypeInference = ( + leftType: RelDataType, + rightType: RelDataType) => typeFactory.createDecimalQuotient(leftType, rightType) + inferDivOrMul(div, decimalTypeInference) + } + + private def inferDivOrMul( + op: BinaryArithmetic, + decimalTypeInfer: (RelDataType, RelDataType) => RelDataType + ): TypeInformation[_] = { + val func = ( + leftType: RelDataType, + rightType: RelDataType, + wideResultType: LogicalType) => { + val decimalType = decimalTypeInfer(leftType, rightType) + if (decimalType != null) { + fromLogicalTypeToTypeInfo(FlinkTypeFactory.toLogicalType(decimalType)) + } else { + val resultType = typeFactory.leastRestrictive( + List(leftType, rightType).asJava) + fromLogicalTypeToTypeInfo(FlinkTypeFactory.toLogicalType(resultType)) + } + } + infer(op, func) + } + + /** + * Infer resultType of [[Round]] expression. + * The decimal type inference keeps consistent with Calcite + * [[org.apache.flink.table.calcite.type.FlinkReturnTypes]].ROUND_FUNCTION_NULLABLE + * + * @param round round Expression + * @return result type + */ + def infer(round: Round): TypeInformation[_] = { + val numType = round.left.resultType + numType match { + case _: DecimalTypeInfo | _: BigDecimalTypeInfo => + val lenValue = round.right match { + case Literal(v: Int, BasicTypeInfo.INT_TYPE_INFO) => v + case _ => throw new TableException("This will not happen here!") + } + val numLogicalType = fromTypeInfoToLogicalType(numType) + val numRelDataType = typeFactory.createFieldTypeFromLogicalType(numLogicalType) + val p = numRelDataType.getPrecision + val s = numRelDataType.getScale + val dt = FlinkTypeSystem.inferRoundType(p, s, lenValue) + fromLogicalTypeToTypeInfo(dt) + case t => t + } + } + + /** + * Infer resultType of [[Floor]] expression. + * The decimal type inference keeps consistent with Calcite + * [[org.apache.calcite.sql.type.ReturnTypes]].ARG0_OR_EXACT_NO_SCALE + * + * @param floor floor Expression + * @return result type + */ + def infer(floor: Floor): TypeInformation[_] = getArg0OrExactNoScale(floor) + + /** + * Infer resultType of [[Ceil]] expression. + * The decimal type inference keeps consistent with Calcite + * [[org.apache.calcite.sql.type.ReturnTypes]].ARG0_OR_EXACT_NO_SCALE + * + * @param ceil ceil Expression + * @return result type + */ + def infer(ceil: Ceil): TypeInformation[_] = getArg0OrExactNoScale(ceil) + + private def getArg0OrExactNoScale(op: UnaryExpression) = { + val childType = op.child.resultType + childType match { + case t: DecimalTypeInfo => if (t.scale() == 0) { + t Review comment: Why just return `DecimalTypeInfo.of(t.precision(), 0)`? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
