Search in sources :

Example 41 with HiveDecimalWritable

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());
}
Also used : PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) LongWritable(org.apache.hadoop.io.LongWritable) Test(org.junit.Test)

Example 42 with HiveDecimalWritable

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()));
}
Also used : PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) DoubleWritable(org.apache.hadoop.hive.serde2.io.DoubleWritable) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) Test(org.junit.Test)

Example 43 with HiveDecimalWritable

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;
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)

Example 44 with HiveDecimalWritable

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;
}
Also used : HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) Timestamp(java.sql.Timestamp)

Example 45 with HiveDecimalWritable

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;
}
Also used : HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) Timestamp(java.sql.Timestamp)

Aggregations

HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)139 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)33 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)33 Test (org.junit.Test)33 LongWritable (org.apache.hadoop.io.LongWritable)32 IntWritable (org.apache.hadoop.io.IntWritable)29 TimestampWritable (org.apache.hadoop.hive.serde2.io.TimestampWritable)28 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)27 Text (org.apache.hadoop.io.Text)27 HiveDecimal (org.apache.hadoop.hive.common.type.HiveDecimal)26 DeferredJavaObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject)26 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)26 ShortWritable (org.apache.hadoop.hive.serde2.io.ShortWritable)25 ByteWritable (org.apache.hadoop.hive.serde2.io.ByteWritable)24 FloatWritable (org.apache.hadoop.io.FloatWritable)24 DateWritable (org.apache.hadoop.hive.serde2.io.DateWritable)23 BooleanWritable (org.apache.hadoop.io.BooleanWritable)22 BytesWritable (org.apache.hadoop.io.BytesWritable)22 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)21 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)20