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;
}
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);
}
}
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);
}
}
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();
}
}
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());
}
}
Aggregations