use of org.apache.hadoop.hive.serde2.io.HiveDecimalWritable in project hive by apache.
the class TestGenericUDFOPPlus method testLongPlusDecimal.
@Test
public void testLongPlusDecimal() throws HiveException {
GenericUDFOPPlus udf = new GenericUDFOPPlus();
// Long
LongWritable left = new LongWritable(104);
HiveDecimalWritable right = new HiveDecimalWritable(HiveDecimal.create("234.97"));
ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableLongObjectInspector, PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(9, 4)) };
DeferredObject[] args = { new DeferredJavaObject(left), new DeferredJavaObject(right) };
PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
Assert.assertEquals(TypeInfoFactory.getDecimalTypeInfo(24, 4), oi.getTypeInfo());
HiveDecimalWritable res = (HiveDecimalWritable) udf.evaluate(args);
Assert.assertEquals(HiveDecimal.create("338.97"), res.getHiveDecimal());
}
use of org.apache.hadoop.hive.serde2.io.HiveDecimalWritable in project hive by apache.
the class TestGenericUDFOPPlus method testDoulePlusDecimal.
@Test
public void testDoulePlusDecimal() throws HiveException {
GenericUDFOPPlus udf = new GenericUDFOPPlus();
// Double
DoubleWritable left = new DoubleWritable(74.52);
HiveDecimalWritable right = new HiveDecimalWritable(HiveDecimal.create("234.97"));
ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableDoubleObjectInspector, PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(5, 2)) };
DeferredObject[] args = { new DeferredJavaObject(left), new DeferredJavaObject(right) };
PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
Assert.assertEquals(TypeInfoFactory.doubleTypeInfo, oi.getTypeInfo());
DoubleWritable res = (DoubleWritable) udf.evaluate(args);
Assert.assertEquals(new Double(309.49), new Double(res.get()));
}
use of org.apache.hadoop.hive.serde2.io.HiveDecimalWritable in project hive by apache.
the class WritableConstantHiveDecimalObjectInspector method getWritableConstantValue.
@Override
public HiveDecimalWritable getWritableConstantValue() {
// We need to enforce precision/scale here.
DecimalTypeInfo decTypeInfo = (DecimalTypeInfo) typeInfo;
HiveDecimalWritable result = new HiveDecimalWritable(value);
result.mutateEnforcePrecisionScale(decTypeInfo.precision(), decTypeInfo.scale());
if (!result.isSet()) {
return null;
}
return result;
}
use of org.apache.hadoop.hive.serde2.io.HiveDecimalWritable in project hive by apache.
the class TimestampUtils method decimalToTimestamp.
/**
* Take a HiveDecimal and return the timestamp representation where the fraction part is the
* nanoseconds and integer part is the number of seconds.
* @param dec
* @return
*/
public static Timestamp decimalToTimestamp(HiveDecimal dec) {
HiveDecimalWritable nanosWritable = new HiveDecimalWritable(dec);
// Clip off seconds portion.
nanosWritable.mutateFractionPortion();
// Bring nanoseconds into integer portion.
nanosWritable.mutateScaleByPowerOfTen(9);
if (!nanosWritable.isSet() || !nanosWritable.isInt()) {
return null;
}
int nanos = nanosWritable.intValue();
if (nanos < 0) {
nanos += 1000000000;
}
nanosWritable.setFromLong(nanos);
HiveDecimalWritable nanoInstant = new HiveDecimalWritable(dec);
nanoInstant.mutateScaleByPowerOfTen(9);
nanoInstant.mutateSubtract(nanosWritable);
// Back to seconds.
nanoInstant.mutateScaleByPowerOfTen(-9);
if (!nanoInstant.isSet() || !nanoInstant.isLong()) {
return null;
}
long seconds = nanoInstant.longValue();
Timestamp t = new Timestamp(seconds * 1000);
t.setNanos(nanos);
return t;
}
use of org.apache.hadoop.hive.serde2.io.HiveDecimalWritable in project hive by apache.
the class TimestampUtils method decimalToTimestamp.
/**
* Take a HiveDecimalWritable and return the timestamp representation where the fraction part
* is the nanoseconds and integer part is the number of seconds.
*
* This is a HiveDecimalWritable variation with supplied scratch objects.
* @param decWritable
* @param scratchDecWritable1
* @param scratchDecWritable2
* @return
*/
public static Timestamp decimalToTimestamp(HiveDecimalWritable decWritable, HiveDecimalWritable scratchDecWritable1, HiveDecimalWritable scratchDecWritable2) {
HiveDecimalWritable nanosWritable = scratchDecWritable1;
nanosWritable.set(decWritable);
// Clip off seconds portion.
nanosWritable.mutateFractionPortion();
// Bring nanoseconds into integer portion.
nanosWritable.mutateScaleByPowerOfTen(9);
if (!nanosWritable.isSet() || !nanosWritable.isInt()) {
return null;
}
int nanos = nanosWritable.intValue();
if (nanos < 0) {
nanos += 1000000000;
}
nanosWritable.setFromLong(nanos);
HiveDecimalWritable nanoInstant = scratchDecWritable2;
nanoInstant.set(decWritable);
nanoInstant.mutateScaleByPowerOfTen(9);
nanoInstant.mutateSubtract(nanosWritable);
// Back to seconds.
nanoInstant.mutateScaleByPowerOfTen(-9);
if (!nanoInstant.isSet() || !nanoInstant.isLong()) {
return null;
}
long seconds = nanoInstant.longValue();
Timestamp timestamp = new Timestamp(seconds * 1000L);
timestamp.setNanos(nanos);
return timestamp;
}
Aggregations