/** * Returns true if specified type is decimal data type. * * @param type type to check * @return true if specified type is decimal data type. */ public static boolean isDecimalType(MajorType type) { return isDecimalType(type.getMinorType()); }
/** * 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; }
/** * Returns true if specified type is decimal data type. * * @param type type to check * @return true if specified type is decimal data type. */ public static boolean isDecimalType(MajorType type) { return isDecimalType(type.getMinorType()); }
/** * Checks if implicit cast is allowed between the two input types of the join condition. Currently we allow * implicit casts in join condition only between numeric types and varchar/varbinary types. * @param input1 * @param input2 * @return true if implicit cast is allowed false otherwise */ private static boolean allowImplicitCast(TypeProtos.MinorType input1, TypeProtos.MinorType input2) { // allow implicit cast if both the input types are numeric and any of them is non-decimal // or both of them are decimal if (TypeCastRules.isNumericType(input1) && TypeCastRules.isNumericType(input2) && ((!Types.isDecimalType(input1) && !Types.isDecimalType(input2)) || Types.isDecimalType(input1) && Types.isDecimalType(input2))) { return true; } // allow implicit cast if input types are date/ timestamp if ((input1 == TypeProtos.MinorType.DATE || input1 == TypeProtos.MinorType.TIMESTAMP) && (input2 == TypeProtos.MinorType.DATE || input2 == TypeProtos.MinorType.TIMESTAMP)) { return true; } // allow implicit cast if both the input types are varbinary/ varchar if ((input1 == TypeProtos.MinorType.VARCHAR || input1 == TypeProtos.MinorType.VARBINARY) && (input2 == TypeProtos.MinorType.VARCHAR || input2 == TypeProtos.MinorType.VARBINARY)) { return true; } return false; }
/** * 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; }
public static TypeProtos.MajorType toMajorType(PrimitiveType.PrimitiveTypeName primitiveTypeName, int length, TypeProtos.DataMode mode, SchemaElement schemaElement, OptionManager options) { ConvertedType convertedType = schemaElement.getConverted_type(); MinorType minorType = getMinorType(primitiveTypeName, length, convertedType, options); TypeProtos.MajorType.Builder typeBuilder = TypeProtos.MajorType.newBuilder().setMinorType(minorType).setMode(mode); if (Types.isDecimalType(minorType)) { int precision = schemaElement.getPrecision(); int scale = schemaElement.getScale(); typeBuilder.setPrecision(precision).setScale(scale); } return typeBuilder.build(); }
if (Types.isDecimalType(toType)) {
if (Types.isDecimalType(type)) { newArgs.add(new ValueExpressions.IntExpression(type.getPrecision(), null)); newArgs.add(new ValueExpressions.IntExpression(type.getScale(), null));
/** * Given a SqlOperatorBinding, convert it to FunctionCall * @param opBinding the given SqlOperatorBinding * @return FunctionCall the converted FunctionCall */ public static FunctionCall convertSqlOperatorBindingToFunctionCall(final SqlOperatorBinding opBinding) { final List<LogicalExpression> args = Lists.newArrayList(); for (int i = 0; i < opBinding.getOperandCount(); ++i) { final RelDataType type = opBinding.getOperandType(i); final TypeProtos.MinorType minorType = getDrillTypeFromCalciteType(type); TypeProtos.DataMode dataMode = type.isNullable() ? TypeProtos.DataMode.OPTIONAL : TypeProtos.DataMode.REQUIRED; TypeProtos.MajorType.Builder builder = TypeProtos.MajorType.newBuilder() .setMode(dataMode) .setMinorType(minorType); if (Types.isDecimalType(minorType)) { builder .setScale(type.getScale()) .setPrecision(type.getPrecision()); } args.add(new MajorTypeInLogicalExpression(builder.build())); } final String drillFuncName = FunctionCallFactory.replaceOpWithFuncName(opBinding.getOperator().getName()); return new FunctionCall( drillFuncName, args, ExpressionPosition.UNKNOWN); }
protected PrimitiveType getPrimitiveType(MaterializedField field) { MinorType minorType = field.getType().getMinorType(); String name = field.getName(); int length = ParquetTypeHelper.getLengthForMinorType(minorType); PrimitiveTypeName primitiveTypeName = ParquetTypeHelper.getPrimitiveTypeNameForMinorType(minorType); if (Types.isDecimalType(minorType)) { primitiveTypeName = logicalTypeForDecimals; if (usePrimitiveTypesForDecimals) { if (field.getPrecision() <= ParquetTypeHelper.getMaxPrecisionForPrimitiveType(PrimitiveTypeName.INT32)) { primitiveTypeName = PrimitiveTypeName.INT32; } else if (field.getPrecision() <= ParquetTypeHelper.getMaxPrecisionForPrimitiveType(PrimitiveTypeName.INT64)) { primitiveTypeName = PrimitiveTypeName.INT64; } } length = DecimalUtility.getMaxBytesSizeForPrecision(field.getPrecision()); } Repetition repetition = ParquetTypeHelper.getRepetitionForDataMode(field.getDataMode()); OriginalType originalType = ParquetTypeHelper.getOriginalTypeForMinorType(minorType); DecimalMetadata decimalMetadata = ParquetTypeHelper.getDecimalMetadataForField(field); return new PrimitiveType(repetition, primitiveTypeName, length, name, originalType, decimalMetadata, null); }
} else { if (Types.isDecimalType(parmType)) { } else { if (Types.isDecimalType(parmType)) {