/** * Get the <code>precision</code> of given type. * * @param majorType major type * @return precision value */ public static int getPrecision(MajorType majorType) { if (majorType.hasPrecision()) { return majorType.getPrecision(); } return isScalarStringType(majorType) ? MAX_VARCHAR_LENGTH : UNDEFINED; }
/** * Sets max precision from both types if these types are string scalar types. * Sets max precision and scale from both types if these types are decimal types. * Both types should be of the same minor type. * * @param leftType type from left side * @param rightType type from right side * @param typeBuilder type builder * @return type builder */ public static MajorType.Builder calculateTypePrecisionAndScale(MajorType leftType, MajorType rightType, MajorType.Builder typeBuilder) { if (leftType.getMinorType().equals(rightType.getMinorType())) { boolean isScalarString = Types.isScalarStringType(leftType) && Types.isScalarStringType(rightType); boolean isDecimal = isDecimalType(leftType); if ((isScalarString || isDecimal) && 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())); } } return typeBuilder; }
/** * Get the <code>precision</code> of given type. * * @param majorType major type * @return precision value */ public static int getPrecision(MajorType majorType) { if (majorType.hasPrecision()) { return majorType.getPrecision(); } return isScalarStringType(majorType) ? MAX_VARCHAR_LENGTH : UNDEFINED; }
/** * Sets max precision from both types if these types are string scalar types. * Sets max precision and scale from both types if these types are decimal types. * Both types should be of the same minor type. * * @param leftType type from left side * @param rightType type from right side * @param typeBuilder type builder * @return type builder */ public static MajorType.Builder calculateTypePrecisionAndScale(MajorType leftType, MajorType rightType, MajorType.Builder typeBuilder) { if (leftType.getMinorType().equals(rightType.getMinorType())) { boolean isScalarString = Types.isScalarStringType(leftType) && Types.isScalarStringType(rightType); boolean isDecimal = isDecimalType(leftType); if ((isScalarString || isDecimal) && 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())); } } return typeBuilder; }