paul-rogers commented on a change in pull request #2006: DRILL-7615: UNION ALL query returns the wrong result for the decimal value URL: https://github.com/apache/drill/pull/2006#discussion_r386776641
########## File path: common/src/main/java/org/apache/drill/common/types/Types.java ########## @@ -795,15 +807,25 @@ public static boolean isSortable(MinorType type) { */ public static MajorType.Builder calculateTypePrecisionAndScale(MajorType leftType, MajorType rightType, MajorType.Builder typeBuilder) { if (leftType.getMinorType().equals(rightType.getMinorType())) { - final boolean isScalarString = Types.isScalarStringType(leftType) && Types.isScalarStringType(rightType); - final boolean isDecimal = isDecimalType(leftType); + boolean isScalarString = Types.isScalarStringType(leftType) && Types.isScalarStringType(rightType); + boolean isDecimal = isDecimalType(leftType); - if ((isScalarString || isDecimal) && leftType.hasPrecision() && rightType.hasPrecision()) { + if (isScalarString && leftType.hasPrecision() && rightType.hasPrecision()) { typeBuilder.setPrecision(Math.max(leftType.getPrecision(), rightType.getPrecision())); } - if (isDecimal && leftType.hasScale() && rightType.hasScale()) { - typeBuilder.setScale(Math.max(leftType.getScale(), rightType.getScale())); + if (isDecimal) { + int scale = Math.max(leftType.getScale(), rightType.getScale()); + // resulting precision should take into account resulting scale value and be calculated as + // sum of two components: + // - max integer digits number (precision - scale) for left and right; + // - resulting scale. + // So for the case of cast(9999 as decimal(4,0)) and cast(1.23 as decimal(3,2)) + // resulting scale would be Max(0, 2) = 2 and resulting precision would be Max(4 - 0, 3 - 2) + 2 = 6. + // In this case, both values would fit into decimal(6, 2): 9999.00, 1.23 + int precision = Math.max(leftType.getPrecision() - leftType.getScale(), rightType.getPrecision() - rightType.getScale()) + scale; + typeBuilder.setPrecision(precision); + typeBuilder.setScale(scale); Review comment: Nit: please split the long lint. ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services