private HiveDecimal enforcePrecisionScale(HiveDecimal dec) { return HiveDecimalUtils.enforcePrecisionScale(dec, (DecimalTypeInfo)typeInfo); }
public static TypeInfo getDecimalTypeForPrimitiveCategories( PrimitiveTypeInfo a, PrimitiveTypeInfo b) { int prec1 = HiveDecimalUtils.getPrecisionForType(a); int prec2 = HiveDecimalUtils.getPrecisionForType(b); int scale1 = HiveDecimalUtils.getScaleForType(a); int scale2 = HiveDecimalUtils.getScaleForType(b); int intPart = Math.max(prec1 - scale1, prec2 - scale2); int decPart = Math.max(scale1, scale2); int prec = Math.min(intPart + decPart, HiveDecimal.MAX_PRECISION); int scale = Math.min(decPart, HiveDecimal.MAX_PRECISION - intPart); return TypeInfoFactory.getDecimalTypeInfo(prec, scale); }
public DecimalTypeInfo(int precision, int scale) { super(serdeConstants.DECIMAL_TYPE_NAME); HiveDecimalUtils.validateParameter(precision, scale); this.precision = precision; this.scale = scale; }
/** * Default implementation. Maybe overridden by exact types. */ @Override public int precision() { return HiveDecimalUtils.getPrecisionForType(typeInfo); }
/** * Default implementation. Maybe overridden by exact types. */ @Override public int scale() { return HiveDecimalUtils.getScaleForType(typeInfo); }
public static PrimitiveTypeInfo deriveMinArgumentCast( ExprNodeDesc childExpr, TypeInfo targetType) { assert targetType instanceof PrimitiveTypeInfo : "Not a primitive type" + targetType; PrimitiveTypeInfo pti = (PrimitiveTypeInfo)targetType; // We only do the minimum cast for decimals. Other types are assumed safe; fix if needed. // We also don't do anything for non-primitive children (maybe we should assert). if ((pti.getPrimitiveCategory() != PrimitiveCategory.DECIMAL) || (!(childExpr.getTypeInfo() instanceof PrimitiveTypeInfo))) { return pti; } PrimitiveTypeInfo childTi = (PrimitiveTypeInfo)childExpr.getTypeInfo(); // If the child is also decimal, no cast is needed (we hope - can target type be narrower?). return HiveDecimalUtils.getDecimalTypeForPrimitiveCategory(childTi); }
return TypeInfoFactory.getVarcharTypeInfo(maxLength); case DECIMAL: return HiveDecimalUtils.getDecimalTypeForPrimitiveCategories(a, b); default:
private int getPrecisionForType(PrimitiveTypeInfo typeInfo) { if (isFloatFamily(typeInfo.getTypeName())) { return HiveDecimal.MAX_PRECISION; } return HiveDecimalUtils.getPrecisionForType(typeInfo); }
/** * Creates a DecimalTypeInfo object with appropriate precision and scale for the given * inputTypeInfo. */ private TypeInfo updatePrecision(TypeInfo inputTypeInfo, DecimalTypeInfo returnType) { if (!(inputTypeInfo instanceof PrimitiveTypeInfo)) { return returnType; } PrimitiveTypeInfo ptinfo = (PrimitiveTypeInfo) inputTypeInfo; int precision = getPrecisionForType(ptinfo); // TODO: precision and scale would be practically invalid for string conversion (38,38) int scale = HiveDecimalUtils.getScaleForType(ptinfo); return new DecimalTypeInfo(precision, scale); }
public static PrimitiveTypeInfo deriveMinArgumentCast( ExprNodeDesc childExpr, TypeInfo targetType) { assert targetType instanceof PrimitiveTypeInfo : "Not a primitive type" + targetType; PrimitiveTypeInfo pti = (PrimitiveTypeInfo)targetType; // We only do the minimum cast for decimals. Other types are assumed safe; fix if needed. // We also don't do anything for non-primitive children (maybe we should assert). if ((pti.getPrimitiveCategory() != PrimitiveCategory.DECIMAL) || (!(childExpr.getTypeInfo() instanceof PrimitiveTypeInfo))) return pti; PrimitiveTypeInfo childTi = (PrimitiveTypeInfo)childExpr.getTypeInfo(); // If the child is also decimal, no cast is needed (we hope - can target type be narrower?). return HiveDecimalUtils.getDecimalTypeForPrimitiveCategory(childTi); }
return TypeInfoFactory.getVarcharTypeInfo(maxLength); case DECIMAL: return HiveDecimalUtils.getDecimalTypeForPrimitiveCategories(a, b); default:
private HiveDecimalWritable enforcePrecisionScale(HiveDecimalWritable writable) { return HiveDecimalUtils.enforcePrecisionScale(writable, (DecimalTypeInfo)typeInfo); }
public static DecimalTypeInfo getDecimalTypeForPrimitiveCategory(PrimitiveTypeInfo a) { if (a instanceof DecimalTypeInfo) return (DecimalTypeInfo)a; int prec = HiveDecimalUtils.getPrecisionForType(a); int scale = HiveDecimalUtils.getScaleForType(a); prec = Math.min(prec, HiveDecimal.MAX_PRECISION); scale = Math.min(scale, HiveDecimal.MAX_PRECISION - (prec - scale)); return TypeInfoFactory.getDecimalTypeInfo(prec, scale); } }
private int getPrecisionForType(PrimitiveTypeInfo typeInfo) { if (isFloatFamily(typeInfo.getTypeName())) { return HiveDecimal.MAX_PRECISION; } return HiveDecimalUtils.getPrecisionForType(typeInfo); }
/** * Creates a DecimalTypeInfo object with appropriate precision and scale for the given * inputTypeInfo. */ private TypeInfo updatePrecision(TypeInfo inputTypeInfo, DecimalTypeInfo returnType) { if (!(inputTypeInfo instanceof PrimitiveTypeInfo)) { return returnType; } PrimitiveTypeInfo ptinfo = (PrimitiveTypeInfo) inputTypeInfo; int precision = getPrecisionForType(ptinfo); // TODO: precision and scale would be practically invalid for string conversion (38,38) int scale = HiveDecimalUtils.getScaleForType(ptinfo); return new DecimalTypeInfo(precision, scale); }
HiveDecimalUtils.validateParameter(precision, scale); } else if (params.length == 2) { HiveDecimalUtils.validateParameter(precision, scale); } else if (params.length > 2) { throw new IllegalArgumentException("Type decimal only takes two parameter, but " +
public static PrimitiveTypeInfo deriveMinArgumentCast( ExprNodeDesc childExpr, TypeInfo targetType) { assert targetType instanceof PrimitiveTypeInfo : "Not a primitive type" + targetType; PrimitiveTypeInfo pti = (PrimitiveTypeInfo)targetType; // We only do the minimum cast for decimals. Other types are assumed safe; fix if needed. // We also don't do anything for non-primitive children (maybe we should assert). if ((pti.getPrimitiveCategory() != PrimitiveCategory.DECIMAL) || (!(childExpr.getTypeInfo() instanceof PrimitiveTypeInfo))) return pti; PrimitiveTypeInfo childTi = (PrimitiveTypeInfo)childExpr.getTypeInfo(); // If the child is also decimal, no cast is needed (we hope - can target type be narrower?). return HiveDecimalUtils.getDecimalTypeForPrimitiveCategory(childTi); }
return TypeInfoFactory.getVarcharTypeInfo(maxLength); case DECIMAL: return HiveDecimalUtils.getDecimalTypeForPrimitiveCategories(a, b); default:
private HiveDecimal enforcePrecisionScale(HiveDecimal dec) { return HiveDecimalUtils.enforcePrecisionScale(dec,(DecimalTypeInfo)typeInfo); }
public static TypeInfo getDecimalTypeForPrimitiveCategories( PrimitiveTypeInfo a, PrimitiveTypeInfo b) { int prec1 = HiveDecimalUtils.getPrecisionForType(a); int prec2 = HiveDecimalUtils.getPrecisionForType(b); int scale1 = HiveDecimalUtils.getScaleForType(a); int scale2 = HiveDecimalUtils.getScaleForType(b); int intPart = Math.max(prec1 - scale1, prec2 - scale2); int decPart = Math.max(scale1, scale2); int prec = Math.min(intPart + decPart, HiveDecimal.MAX_PRECISION); int scale = Math.min(decPart, HiveDecimal.MAX_PRECISION - intPart); return TypeInfoFactory.getDecimalTypeInfo(prec, scale); }