private static BigDecimal readBigDecimal(HiveDecimalWritable hiveDecimalWritable) { HiveDecimal hiveDecimal = hiveDecimalWritable.getHiveDecimal(); return hiveDecimal.bigDecimalValue(); }
@Override public HiveDecimal getPrimitiveJavaObject(Object o) { if (o == null) { return null; } return enforcePrecisionScale(((HiveDecimalWritable)o).getHiveDecimal()); }
public HiveDecimal get(HiveDecimalWritable iw) { return iw.getHiveDecimal(); } };
@Override public Object set(Object o, HiveDecimalWritable t) { return t == null ? null : enforcePrecisionScale(t.getHiveDecimal()); }
private static Object coercePrimitiveObject(final PrimitiveObjectInspector inspector, final Object object) { if (object instanceof HiveDecimalWritable) { // inspector on HiveDecimal rounds off to integer for some reason. return ((HiveDecimalWritable) object).getHiveDecimal().doubleValue(); } else if (object instanceof DateWritable) { return object.toString(); } else { return inspector.getPrimitiveJavaObject(object); } }
public void updateValueNoCheck(HiveDecimalWritable value, short scale) { double dval = value.getHiveDecimal().doubleValue(); this.sum += dval; this.count += 1; double t = this.count*dval - this.sum; this.variance += (t*t) / ((double)this.count*(this.count-1)); }
@Override public Object evaluateOutput(AggregationBuffer agg) throws HiveException { Aggregation myagg = (Aggregation) agg; // !isSet checks for overflow. if (myagg.isNull || !myagg.sum.isSet()) { return null; } else { return myagg.sum.getHiveDecimal(); } }
public void updateValueNoCheck(HiveDecimalWritable value, short scale) { double dval = value.getHiveDecimal().doubleValue(); this.sum += dval; this.count += 1; double t = this.count*dval - this.sum; this.variance += (t*t) / ((double)this.count*(this.count-1)); }
@Override public int scale() { if (value == null) { return super.scale(); } return value.getHiveDecimal().scale(); }
public void updateValueWithCheckAndInit(HiveDecimalWritable value, short scale) { if (this.isNull) { this.init(); } double dval = value.getHiveDecimal().doubleValue(); this.sum += dval; this.count += 1; if(this.count > 1) { double t = this.count*dval - this.sum; this.variance += (t*t) / ((double)this.count*(this.count-1)); } }
public void updateValueWithCheckAndInit(HiveDecimalWritable value, short scale) { if (this.isNull) { this.init(); } double dval = value.getHiveDecimal().doubleValue(); this.sum += dval; this.count += 1; if(this.count > 1) { double t = this.count*dval - this.sum; this.variance += (t*t) / ((double)this.count*(this.count-1)); } }
public void updateValueWithCheckAndInit(HiveDecimalWritable value, short scale) { if (this.isNull) { this.init(); } double dval = value.getHiveDecimal().doubleValue(); this.sum += dval; this.count += 1; if(this.count > 1) { double t = this.count*dval - this.sum; this.variance += (t*t) / ((double)this.count*(this.count-1)); } }
public void updateValueWithCheckAndInit(HiveDecimalWritable value, short scale) { if (this.isNull) { this.init(); } double dval = value.getHiveDecimal().doubleValue(); this.sum += dval; this.count += 1; if(this.count > 1) { double t = this.count*dval - this.sum; this.variance += (t*t) / ((double)this.count*(this.count-1)); } }
byte[] validatedScaledDecimal(int inpScale) { hiveDecimalWritable.mutateEnforcePrecisionScale(hivePrecision, hiveScale); if (hiveDecimalWritable.isSet()) { this.isValid = true; return hiveDecimalWritable.getHiveDecimal().bigIntegerBytesScaled(inpScale); } else { this.isValid = false; return null; } }
@Override public HiveDecimal getPrimitiveJavaObject(Object o) { if (o == null) { return null; } DecimalTypeInfo decimalTypeInfo = (DecimalTypeInfo) typeInfo; // We do not want to modify the writable provided by the object o since it is not a copy. HiveDecimalWritable decWritable = ((LazyHiveDecimal)o).getWritableObject(); HiveDecimalWritable result = HiveDecimalWritable.enforcePrecisionScale( decWritable, decimalTypeInfo.getPrecision(), decimalTypeInfo.getScale()); return (result != null && result.isSet() ? result.getHiveDecimal() : null); }
@Test public void testAbs() { DecimalColumnVector dcv = new DecimalColumnVector(4 ,20, 13); HiveDecimal d1 = HiveDecimal.create("19.56778"); DecimalUtil.abs(0, d1, dcv); Assert.assertEquals(0, d1.compareTo(dcv.vector[0].getHiveDecimal())); HiveDecimal d2 = HiveDecimal.create("-25.34567"); HiveDecimal expected2 = HiveDecimal.create("25.34567"); DecimalUtil.abs(0, d2, dcv); Assert.assertEquals(0, expected2.compareTo(dcv.vector[0].getHiveDecimal())); }
@Test public void testCastLongToDecimal() throws HiveException { VectorizedRowBatch b = getBatchLongDecimal(); VectorExpression expr = new CastLongToDecimal(0, 1); expr.evaluate(b); DecimalColumnVector r = (DecimalColumnVector) b.cols[1]; assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("0"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-1"))); assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("99999999999999"))); }
@Test public void testCastStringToDecimal() throws HiveException { VectorizedRowBatch b = getBatchStringDecimal(); VectorExpression expr = new CastStringToDecimal(0, 1); expr.evaluate(b); DecimalColumnVector r = (DecimalColumnVector) b.cols[1]; assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("1.10"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-2.20"))); assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("99999999999999.0"))); }
private void runDecimal(double v, IntWritable scale, Double expV, GenericUDF udf) throws HiveException { HiveDecimal hd = HiveDecimal.create(BigDecimal.valueOf(v)); DeferredObject valueObj0 = new DeferredJavaObject(new HiveDecimalWritable(hd)); DeferredObject valueObj1 = new DeferredJavaObject(scale); DeferredObject[] args = { valueObj0, valueObj1 }; HiveDecimalWritable output = (HiveDecimalWritable) udf.evaluate(args); Assert.assertEquals("bround() test ", expV.doubleValue(), output.getHiveDecimal().doubleValue(), 0.00001); } }
@Test public void testDecimalColModuloDecimalColumn() throws HiveException { VectorizedRowBatch b = getVectorizedRowBatch3DecimalCols(); DecimalColumnVector in1 = (DecimalColumnVector) b.cols[1]; for (int i = 0; i < 3; i++) { in1.vector[i].set(HiveDecimal.create("0.50")); } VectorExpression expr = new DecimalColModuloDecimalColumn(0, 1, 2); expr.evaluate(b); DecimalColumnVector r = (DecimalColumnVector) b.cols[2]; assertTrue(r.vector[0].getHiveDecimal().equals(HiveDecimal.create("0.20"))); assertTrue(r.vector[1].getHiveDecimal().equals(HiveDecimal.create("-0.30"))); assertTrue(r.vector[2].getHiveDecimal().equals(HiveDecimal.create("0"))); }