Search in sources :

Example 11 with BinaryObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector in project hive by apache.

the class GenericUDFDecode method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 2) {
        throw new UDFArgumentLengthException("Decode() requires exactly two arguments");
    }
    if (arguments[0].getCategory() != Category.PRIMITIVE) {
        throw new UDFArgumentTypeException(0, "The first argument to Decode() must be primitive");
    }
    PrimitiveCategory category = ((PrimitiveObjectInspector) arguments[0]).getPrimitiveCategory();
    if (category == PrimitiveCategory.BINARY) {
        bytesOI = (BinaryObjectInspector) arguments[0];
    } else if (category == PrimitiveCategory.VOID) {
        bytesOI = (VoidObjectInspector) arguments[0];
    } else {
        throw new UDFArgumentTypeException(0, "The first argument to Decode() must be binary");
    }
    if (arguments[1].getCategory() != Category.PRIMITIVE) {
        throw new UDFArgumentTypeException(1, "The second argument to Decode() must be primitive");
    }
    charsetOI = (PrimitiveObjectInspector) arguments[1];
    if (PrimitiveGrouping.STRING_GROUP != PrimitiveObjectInspectorUtils.getPrimitiveGrouping(charsetOI.getPrimitiveCategory())) {
        throw new UDFArgumentTypeException(1, "The second argument to Decode() must be from string group");
    }
    // If the character set for decoding is constant, we can optimize that
    if (arguments[1] instanceof ConstantObjectInspector) {
        String charSetName = ((ConstantObjectInspector) arguments[1]).getWritableConstantValue().toString();
        decoder = Charset.forName(charSetName).newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
    }
    return PrimitiveObjectInspectorFactory.javaStringObjectInspector;
}
Also used : UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) VoidObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.VoidObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)

Example 12 with BinaryObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector in project hive by apache.

the class ColumnStatisticsObjTranslator method unpackTimestampStats.

private static void unpackTimestampStats(ObjectInspector oi, Object o, ColumnStatsField csf, ColumnStatisticsObj statsObj) {
    switch(csf) {
        case COUNT_NULLS:
            long cn = ((LongObjectInspector) oi).get(o);
            statsObj.getStatsData().getTimestampStats().setNumNulls(cn);
            break;
        case MIN:
            TimestampWritableV2 min = ((TimestampObjectInspector) oi).getPrimitiveWritableObject(o);
            statsObj.getStatsData().getTimestampStats().setLowValue(new Timestamp(min.getSeconds()));
            break;
        case MAX:
            TimestampWritableV2 max = ((TimestampObjectInspector) oi).getPrimitiveWritableObject(o);
            statsObj.getStatsData().getTimestampStats().setHighValue(new Timestamp(max.getSeconds()));
            break;
        case NDV:
            long ndv = ((LongObjectInspector) oi).get(o);
            statsObj.getStatsData().getTimestampStats().setNumDVs(ndv);
            break;
        case BITVECTOR:
            PrimitiveObjectInspector poi = (PrimitiveObjectInspector) oi;
            byte[] buf = ((BinaryObjectInspector) poi).getPrimitiveJavaObject(o);
            statsObj.getStatsData().getTimestampStats().setBitVectors(buf);
            break;
        default:
            throw new RuntimeException("Unsupported column stat for TIMESTAMP : " + csf);
    }
}
Also used : LongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector) TimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector) BinaryObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) Timestamp(org.apache.hadoop.hive.metastore.api.Timestamp)

Example 13 with BinaryObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector in project hive by apache.

the class ColumnStatisticsObjTranslator method unpackDecimalStats.

private static void unpackDecimalStats(ObjectInspector oi, Object o, ColumnStatsField csf, ColumnStatisticsObj statsObj) {
    switch(csf) {
        case COUNT_NULLS:
            long cn = ((LongObjectInspector) oi).get(o);
            statsObj.getStatsData().getDecimalStats().setNumNulls(cn);
            break;
        case MIN:
            HiveDecimal min = ((HiveDecimalObjectInspector) oi).getPrimitiveJavaObject(o);
            statsObj.getStatsData().getDecimalStats().setLowValue(convertToThriftDecimal(min));
            break;
        case MAX:
            HiveDecimal max = ((HiveDecimalObjectInspector) oi).getPrimitiveJavaObject(o);
            statsObj.getStatsData().getDecimalStats().setHighValue(convertToThriftDecimal(max));
            break;
        case NDV:
            long ndv = ((LongObjectInspector) oi).get(o);
            statsObj.getStatsData().getDecimalStats().setNumDVs(ndv);
            break;
        case BITVECTOR:
            PrimitiveObjectInspector poi = (PrimitiveObjectInspector) oi;
            byte[] buf = ((BinaryObjectInspector) poi).getPrimitiveJavaObject(o);
            statsObj.getStatsData().getDecimalStats().setBitVectors(buf);
            break;
        default:
            throw new RuntimeException("Unsupported column stat for DECIMAL : " + csf);
    }
}
Also used : LongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector) HiveDecimal(org.apache.hadoop.hive.common.type.HiveDecimal) BinaryObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector) HiveDecimalObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)

Example 14 with BinaryObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector in project hive by apache.

the class LazyUtils method writePrimitive.

/**
 * Write out a binary representation of a PrimitiveObject to a byte stream.
 *
 * @param out ByteStream.Output, an unsynchronized version of ByteArrayOutputStream, used as a
 *            backing buffer for the the DataOutputStream
 * @param o the PrimitiveObject
 * @param oi the PrimitiveObjectInspector
 * @throws IOException on error during the write operation
 */
public static void writePrimitive(OutputStream out, Object o, PrimitiveObjectInspector oi) throws IOException {
    DataOutputStream dos = new DataOutputStream(out);
    try {
        switch(oi.getPrimitiveCategory()) {
            case BOOLEAN:
                boolean b = ((BooleanObjectInspector) oi).get(o);
                dos.writeBoolean(b);
                break;
            case BYTE:
                byte bt = ((ByteObjectInspector) oi).get(o);
                dos.writeByte(bt);
                break;
            case SHORT:
                short s = ((ShortObjectInspector) oi).get(o);
                dos.writeShort(s);
                break;
            case INT:
                int i = ((IntObjectInspector) oi).get(o);
                dos.writeInt(i);
                break;
            case LONG:
                long l = ((LongObjectInspector) oi).get(o);
                dos.writeLong(l);
                break;
            case FLOAT:
                float f = ((FloatObjectInspector) oi).get(o);
                dos.writeFloat(f);
                break;
            case DOUBLE:
                double d = ((DoubleObjectInspector) oi).get(o);
                dos.writeDouble(d);
                break;
            case BINARY:
                {
                    BytesWritable bw = ((BinaryObjectInspector) oi).getPrimitiveWritableObject(o);
                    out.write(bw.getBytes(), 0, bw.getLength());
                    break;
                }
            case DECIMAL:
                {
                    HiveDecimalWritable hdw = ((HiveDecimalObjectInspector) oi).getPrimitiveWritableObject(o);
                    hdw.write(dos);
                    break;
                }
            default:
                throw new RuntimeException("Hive internal error.");
        }
    } finally {
        // closing the underlying ByteStream should have no effect, the data should still be
        // accessible
        dos.close();
    }
}
Also used : LongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector) IntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector) DataOutputStream(java.io.DataOutputStream) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) BytesWritable(org.apache.hadoop.io.BytesWritable) FloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector) ByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector) DoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DoubleObjectInspector) ShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.ShortObjectInspector) BooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector)

Example 15 with BinaryObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector 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)

Aggregations

BinaryObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector)33 LongObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector)26 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)23 DoubleObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.DoubleObjectInspector)19 HiveDecimalObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector)17 TimestampObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector)17 StringObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector)16 ByteObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector)15 FloatObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector)15 IntObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector)15 ShortObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.ShortObjectInspector)15 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)14 DateObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector)14 BooleanObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector)13 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)12 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)12 HiveCharObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveCharObjectInspector)12 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)11 HiveVarcharObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveVarcharObjectInspector)11 BytesWritable (org.apache.hadoop.io.BytesWritable)11