/** * The intermediate sum field has 10 more integer digits with the same scale. * This is exposed as static so that the vectorized AVG operator use the same logic * @param precision * @param scale * @return */ public static DecimalTypeInfo deriveSumFieldTypeInfo(int precision, int scale) { int intPart = precision - scale; intPart = Math.min(intPart + 10, HiveDecimal.MAX_PRECISION - scale); return TypeInfoFactory.getDecimalTypeInfo(intPart + scale, scale); }
@Override protected DecimalTypeInfo deriveResultDecimalTypeInfo(int prec1, int scale1, int prec2, int scale2) { int scale = Math.max(scale1, scale2); int prec = Math.min(HiveDecimal.MAX_PRECISION, Math.max(prec1 - scale1, prec2 - scale2) + scale); return TypeInfoFactory.getDecimalTypeInfo(prec, scale); }
public static DecimalTypeInfo deriveResultDecimalTypeInfo(int precision, int scale, Mode mode) { if (mode == Mode.FINAL || mode == Mode.COMPLETE) { int intPart = precision - scale; // The avg() result type has the same number of integer digits and 4 more decimal digits. scale = Math.min(scale + 4, HiveDecimal.MAX_SCALE - intPart); return TypeInfoFactory.getDecimalTypeInfo(intPart + scale, scale); } else { // For intermediate sum field return GenericUDAFAverage.deriveSumFieldTypeInfo(precision, scale); } }
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 static DecimalTypeInfo getOutputTypeInfo(DecimalTypeInfo inputTypeInfo, int dec) { int prec = inputTypeInfo.precision(); int scale = inputTypeInfo.scale(); int intParts = prec - scale; // If we are rounding, we may introduce one more integer digit. int newIntParts = dec < scale ? intParts + 1 : intParts; int newScale = dec < 0 ? 0 : Math.min(dec, HiveDecimal.MAX_SCALE); int newPrec = Math.min(newIntParts + newScale, HiveDecimal.MAX_PRECISION); return TypeInfoFactory.getDecimalTypeInfo(newPrec, newScale); }
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); }
private void testDecimalDivisionResultType(int prec1, int scale1, int prec2, int scale2, int prec3, int scale3) throws HiveException { GenericUDFOPDivide udf = new GenericUDFOPDivide(); ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(prec1, scale1)), PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(prec2, scale2)) }; PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs); Assert.assertEquals(TypeInfoFactory.getDecimalTypeInfo(prec3, scale3), oi.getTypeInfo()); }
@Test public void testVectorExpressionWriterDecimal() throws HiveException { DecimalTypeInfo typeInfo = TypeInfoFactory.getDecimalTypeInfo(38, 18); testWriterDecimal(typeInfo); }
@Test public void testVectorExpressionSetterDecimal() throws HiveException { DecimalTypeInfo typeInfo = TypeInfoFactory.getDecimalTypeInfo(38, 18); testSetterDecimal(typeInfo); }
@Test public void testDecimalPosModDecimalSameParams() throws HiveException { GenericUDFPosMod udf = new GenericUDFPosMod(); ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(5, 2)), PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(5, 2)) }; PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs); Assert.assertEquals(TypeInfoFactory.getDecimalTypeInfo(5, 2), oi.getTypeInfo()); }
@Test public void testDecimalRoundingMetaData1() throws UDFArgumentException { GenericUDFRound udf = new GenericUDFRound(); ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(7, 3)), PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(TypeInfoFactory.intTypeInfo, new IntWritable(-2)) }; PrimitiveObjectInspector outputOI = (PrimitiveObjectInspector) udf.initialize(inputOIs); DecimalTypeInfo outputTypeInfo = (DecimalTypeInfo)outputOI.getTypeInfo(); Assert.assertEquals(TypeInfoFactory.getDecimalTypeInfo(5, 0), outputTypeInfo); }
@Test public void testDecimalMinusDecimalSameParams() throws HiveException { GenericUDFOPMinus udf = new GenericUDFOPMinus(); ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(5, 2)), PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(5, 2)) }; PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs); Assert.assertEquals(TypeInfoFactory.getDecimalTypeInfo(6, 2), oi.getTypeInfo()); }
@Test public void testDecimalRoundingMetaData() throws UDFArgumentException { GenericUDFRound udf = new GenericUDFRound(); ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(7, 3)), PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(TypeInfoFactory.intTypeInfo, new IntWritable(2)) }; PrimitiveObjectInspector outputOI = (PrimitiveObjectInspector) udf.initialize(inputOIs); DecimalTypeInfo outputTypeInfo = (DecimalTypeInfo)outputOI.getTypeInfo(); Assert.assertEquals(TypeInfoFactory.getDecimalTypeInfo(7, 2), outputTypeInfo); }
@Test public void testDecimalPosModDecimal() throws HiveException { GenericUDFPosMod udf = new GenericUDFPosMod(); ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(3, 1)), PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(5, 2)) }; PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs); Assert.assertEquals(TypeInfoFactory.getDecimalTypeInfo(5, 2), oi.getTypeInfo()); }
@Test public void testDecimalModDecimalSameParams() throws HiveException { GenericUDFOPMod udf = new GenericUDFOPMod(); ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(5, 2)), PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(5, 2)) }; PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs); Assert.assertEquals(TypeInfoFactory.getDecimalTypeInfo(5, 2), oi.getTypeInfo()); }
@Test public void testDecimalRoundingMetaData2() throws UDFArgumentException { GenericUDFRound udf = new GenericUDFRound(); ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(7, 3)), PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(TypeInfoFactory.intTypeInfo, new IntWritable(5)) }; PrimitiveObjectInspector outputOI = (PrimitiveObjectInspector) udf.initialize(inputOIs); DecimalTypeInfo outputTypeInfo = (DecimalTypeInfo)outputOI.getTypeInfo(); Assert.assertEquals(TypeInfoFactory.getDecimalTypeInfo(9, 5), outputTypeInfo); }
@Test public void testDecimalPlusDecimalSameParams() throws HiveException { GenericUDFOPPlus udf = new GenericUDFOPPlus(); ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(5, 2)), PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(5, 2)) }; PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs); Assert.assertEquals(TypeInfoFactory.getDecimalTypeInfo(6, 2), oi.getTypeInfo()); }
@Test public void testDecimalTimesDecimalSameParams() throws HiveException { GenericUDFOPMultiply udf = new GenericUDFOPMultiply(); ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(5, 2)), PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(5, 2)) }; PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs); Assert.assertEquals(TypeInfoFactory.getDecimalTypeInfo(11, 4), oi.getTypeInfo()); }
@Test public void testDecimalDivideDecimalSameParams() throws HiveException { GenericUDFOPDivide udf = new GenericUDFOPDivide(); ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(5, 2)), PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(5, 2)) }; PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs); Assert.assertEquals(TypeInfoFactory.getDecimalTypeInfo(13, 8), oi.getTypeInfo()); }
@Test public void testDecimalModDecimal() throws HiveException { GenericUDFOPMod udf = new GenericUDFOPMod(); ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(3, 1)), PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(5, 2)) }; PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs); Assert.assertEquals(TypeInfoFactory.getDecimalTypeInfo(4, 2), oi.getTypeInfo()); }