Search in sources :

Example 81 with HiveDecimalWritable

use of org.apache.hadoop.hive.serde2.io.HiveDecimalWritable in project hive by apache.

the class TestStreamingAvg method avgHiveDecimal.

public void avgHiveDecimal(Iterator<HiveDecimal> inVals, int inSz, int numPreceding, int numFollowing, Iterator<HiveDecimal> outVals) throws HiveException {
    GenericUDAFAverage fnR = new GenericUDAFAverage();
    TypeInfo[] inputTypes = { TypeInfoFactory.decimalTypeInfo };
    ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableHiveDecimalObjectInspector };
    HiveDecimalWritable[] in = new HiveDecimalWritable[1];
    in[0] = new HiveDecimalWritable();
    TestStreamingSum._agg(fnR, inputTypes, inVals, TypeHandler.HiveDecimalHandler, in, inputOIs, inSz, numPreceding, numFollowing, outVals);
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) GenericUDAFAverage(org.apache.hadoop.hive.ql.udf.generic.GenericUDAFAverage) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)

Example 82 with HiveDecimalWritable

use of org.apache.hadoop.hive.serde2.io.HiveDecimalWritable in project hive by apache.

the class WritableHiveCharObjectInspector method getPrimitiveWritableObject.

@Override
public HiveCharWritable getPrimitiveWritableObject(Object o) {
    // then output new writable with correct params.
    if (o == null) {
        return null;
    }
    if ((o instanceof Text) || (o instanceof TimestampWritableV2) || (o instanceof HiveDecimalWritable) || (o instanceof DoubleWritable) || (o instanceof FloatWritable) || (o instanceof LongWritable) || (o instanceof IntWritable) || (o instanceof BooleanWritable)) {
        String str = o.toString();
        HiveCharWritable hcw = new HiveCharWritable();
        hcw.set(str, ((CharTypeInfo) typeInfo).getLength());
        return hcw;
    }
    HiveCharWritable writable = ((HiveCharWritable) o);
    if (doesWritableMatchTypeParams((HiveCharWritable) o)) {
        return writable;
    }
    return getWritableWithParams(writable);
}
Also used : FloatWritable(org.apache.hadoop.io.FloatWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) HiveCharWritable(org.apache.hadoop.hive.serde2.io.HiveCharWritable) Text(org.apache.hadoop.io.Text) DoubleWritable(org.apache.hadoop.hive.serde2.io.DoubleWritable) LongWritable(org.apache.hadoop.io.LongWritable) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) IntWritable(org.apache.hadoop.io.IntWritable)

Example 83 with HiveDecimalWritable

use of org.apache.hadoop.hive.serde2.io.HiveDecimalWritable in project hive by apache.

the class TeradataBinarySerde method serializeField.

private void serializeField(Object objectForField, ObjectInspector oi, TypeInfo ti) throws IOException, SerDeException {
    switch(oi.getCategory()) {
        case PRIMITIVE:
            PrimitiveObjectInspector poi = (PrimitiveObjectInspector) oi;
            switch(poi.getPrimitiveCategory()) {
                // Teradata Type: BYTEINT
                case BYTE:
                    ByteObjectInspector boi = (ByteObjectInspector) poi;
                    byte b = 0;
                    if (objectForField != null) {
                        b = boi.get(objectForField);
                    }
                    out.write(b);
                    return;
                // Teradata Type: SMALLINT
                case SHORT:
                    ShortObjectInspector spoi = (ShortObjectInspector) poi;
                    short s = 0;
                    if (objectForField != null) {
                        s = spoi.get(objectForField);
                    }
                    out.writeShort(s);
                    return;
                // Teradata Type: INT
                case INT:
                    IntObjectInspector ioi = (IntObjectInspector) poi;
                    int i = 0;
                    if (objectForField != null) {
                        i = ioi.get(objectForField);
                    }
                    out.writeInt(i);
                    return;
                // Teradata Type: BIGINT
                case LONG:
                    LongObjectInspector loi = (LongObjectInspector) poi;
                    long l = 0;
                    if (objectForField != null) {
                        l = loi.get(objectForField);
                    }
                    out.writeLong(l);
                    return;
                // Teradata Type: FLOAT
                case DOUBLE:
                    DoubleObjectInspector doi = (DoubleObjectInspector) poi;
                    double d = 0;
                    if (objectForField != null) {
                        d = doi.get(objectForField);
                    }
                    out.writeDouble(d);
                    return;
                // Teradata Type: VARCHAR
                case VARCHAR:
                    HiveVarcharObjectInspector hvoi = (HiveVarcharObjectInspector) poi;
                    HiveVarcharWritable hv = hvoi.getPrimitiveWritableObject(objectForField);
                    // assert the length of varchar record fits into the table definition
                    if (hv != null) {
                        assert ((VarcharTypeInfo) ti).getLength() >= hv.getHiveVarchar().getCharacterLength();
                    }
                    out.writeVarChar(hv);
                    return;
                // Teradata Type: TIMESTAMP
                case TIMESTAMP:
                    TimestampObjectInspector tsoi = (TimestampObjectInspector) poi;
                    TimestampWritableV2 ts = tsoi.getPrimitiveWritableObject(objectForField);
                    out.writeTimestamp(ts, getTimeStampByteNum(timestampPrecision));
                    return;
                // Teradata Type: DATE
                case DATE:
                    DateObjectInspector dtoi = (DateObjectInspector) poi;
                    DateWritableV2 dw = dtoi.getPrimitiveWritableObject(objectForField);
                    out.writeDate(dw);
                    return;
                // Teradata Type: CHAR
                case CHAR:
                    HiveCharObjectInspector coi = (HiveCharObjectInspector) poi;
                    HiveCharWritable hc = coi.getPrimitiveWritableObject(objectForField);
                    // assert the length of char record fits into the table definition
                    if (hc != null) {
                        assert ((CharTypeInfo) ti).getLength() >= hc.getHiveChar().getCharacterLength();
                    }
                    out.writeChar(hc, getCharByteNum(charCharset) * ((CharTypeInfo) ti).getLength());
                    return;
                // Teradata Type: DECIMAL
                case DECIMAL:
                    DecimalTypeInfo dtype = (DecimalTypeInfo) ti;
                    int precision = dtype.precision();
                    int scale = dtype.scale();
                    HiveDecimalObjectInspector hdoi = (HiveDecimalObjectInspector) poi;
                    HiveDecimalWritable hd = hdoi.getPrimitiveWritableObject(objectForField);
                    // assert the precision of decimal record fits into the table definition
                    if (hd != null) {
                        assert (dtype.getPrecision() >= hd.precision());
                    }
                    out.writeDecimal(hd, getDecimalByteNum(precision), scale);
                    return;
                // Teradata Type: VARBYTE
                case BINARY:
                    BinaryObjectInspector bnoi = (BinaryObjectInspector) poi;
                    BytesWritable byw = bnoi.getPrimitiveWritableObject(objectForField);
                    out.writeVarByte(byw);
                    return;
                default:
                    throw new SerDeException("Unrecognized type: " + poi.getPrimitiveCategory());
            }
        // Currently, serialization of complex types is not supported
        case LIST:
        case MAP:
        case STRUCT:
        default:
            throw new SerDeException("Unrecognized type: " + oi.getCategory());
    }
}
Also used : LongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector) DateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector) IntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector) BinaryObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector) ByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector) TimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector) ShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.ShortObjectInspector) HiveDecimalObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector) HiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveCharObjectInspector) SerDeException(org.apache.hadoop.hive.serde2.SerDeException) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) CharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo) HiveVarcharWritable(org.apache.hadoop.hive.serde2.io.HiveVarcharWritable) DateWritableV2(org.apache.hadoop.hive.serde2.io.DateWritableV2) HiveCharWritable(org.apache.hadoop.hive.serde2.io.HiveCharWritable) BytesWritable(org.apache.hadoop.io.BytesWritable) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) DoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DoubleObjectInspector) HiveVarcharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveVarcharObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)

Example 84 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 85 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)185 Test (org.junit.Test)42 LongWritable (org.apache.hadoop.io.LongWritable)39 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)36 HiveDecimal (org.apache.hadoop.hive.common.type.HiveDecimal)35 IntWritable (org.apache.hadoop.io.IntWritable)35 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)34 DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)31 ShortWritable (org.apache.hadoop.hive.serde2.io.ShortWritable)30 Text (org.apache.hadoop.io.Text)30 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)28 BytesWritable (org.apache.hadoop.io.BytesWritable)28 FloatWritable (org.apache.hadoop.io.FloatWritable)28 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)27 ByteWritable (org.apache.hadoop.hive.serde2.io.ByteWritable)27 BooleanWritable (org.apache.hadoop.io.BooleanWritable)27 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)26 DeferredJavaObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject)26 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)26 DecimalColumnVector (org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector)25