use of org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector in project pxf by greenplum-db.
the class HiveResolver method resolvePrimitive.
private void resolvePrimitive(Object o, PrimitiveObjectInspector oi, List<OneField> record, boolean toFlatten) {
Object val;
switch(oi.getPrimitiveCategory()) {
case BOOLEAN:
{
val = (o != null) ? ((BooleanObjectInspector) oi).get(o) : null;
addOneFieldToRecord(record, DataType.BOOLEAN, val);
break;
}
case SHORT:
{
if (o == null) {
val = null;
} else if (o.getClass().getSimpleName().equals("ByteWritable")) {
val = (short) ((ByteWritable) o).get();
} else {
val = ((ShortObjectInspector) oi).get(o);
}
addOneFieldToRecord(record, DataType.SMALLINT, val);
break;
}
case INT:
{
val = (o != null) ? ((IntObjectInspector) oi).get(o) : null;
addOneFieldToRecord(record, DataType.INTEGER, val);
break;
}
case LONG:
{
val = (o != null) ? ((LongObjectInspector) oi).get(o) : null;
addOneFieldToRecord(record, DataType.BIGINT, val);
break;
}
case FLOAT:
{
val = (o != null) ? ((FloatObjectInspector) oi).get(o) : null;
addOneFieldToRecord(record, DataType.REAL, val);
break;
}
case DOUBLE:
{
val = (o != null) ? ((DoubleObjectInspector) oi).get(o) : null;
addOneFieldToRecord(record, DataType.FLOAT8, val);
break;
}
case DECIMAL:
{
String sVal = null;
if (o != null) {
HiveDecimal hd = ((HiveDecimalObjectInspector) oi).getPrimitiveJavaObject(o);
if (hd != null) {
BigDecimal bd = hd.bigDecimalValue();
sVal = bd.toString();
}
}
addOneFieldToRecord(record, DataType.NUMERIC, sVal);
break;
}
case STRING:
{
val = (o != null) ? ((StringObjectInspector) oi).getPrimitiveJavaObject(o) : null;
// for more complex types, we need to properly handle special characters by escaping the val
val = toFlatten ? val != null ? String.format("\"%s\"", StringEscapeUtils.escapeJava(val.toString())) : "null" : val;
addOneFieldToRecord(record, DataType.TEXT, val);
break;
}
case VARCHAR:
val = (o != null) ? ((HiveVarcharObjectInspector) oi).getPrimitiveJavaObject(o) : null;
addOneFieldToRecord(record, DataType.VARCHAR, toFlatten ? String.format("\"%s\"", val) : val);
break;
case CHAR:
val = (o != null) ? ((HiveCharObjectInspector) oi).getPrimitiveJavaObject(o) : null;
addOneFieldToRecord(record, DataType.BPCHAR, toFlatten ? String.format("\"%s\"", val) : val);
break;
case BINARY:
{
byte[] toEncode = null;
if (o != null) {
BytesWritable bw = ((BinaryObjectInspector) oi).getPrimitiveWritableObject(o);
toEncode = new byte[bw.getLength()];
System.arraycopy(bw.getBytes(), 0, toEncode, 0, bw.getLength());
}
addOneFieldToRecord(record, DataType.BYTEA, toEncode);
break;
}
case TIMESTAMP:
{
val = (o != null) ? ((TimestampObjectInspector) oi).getPrimitiveJavaObject(o) : null;
addOneFieldToRecord(record, DataType.TIMESTAMP, val);
break;
}
case DATE:
val = (o != null) ? ((DateObjectInspector) oi).getPrimitiveJavaObject(o) : null;
addOneFieldToRecord(record, DataType.DATE, val);
break;
case BYTE:
{
/* TINYINT */
val = (o != null) ? (short) ((ByteObjectInspector) oi).get(o) : null;
addOneFieldToRecord(record, DataType.SMALLINT, val);
break;
}
default:
{
throw new UnsupportedTypeException(oi.getTypeName() + " conversion is not supported by " + getClass().getSimpleName());
}
}
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector in project hetu-core by openlookeng.
the class TestDataWritableWriter method writePrimitive.
/**
* It writes the primitive value to the Parquet RecordConsumer.
*
* @param value The object that contains the primitive value.
* @param inspector The object inspector used to get the correct value type.
*/
private void writePrimitive(final Object value, final PrimitiveObjectInspector inspector) {
if (value == null) {
return;
}
switch(inspector.getPrimitiveCategory()) {
case VOID:
return;
case DOUBLE:
recordConsumer.addDouble(((DoubleObjectInspector) inspector).get(value));
break;
case BOOLEAN:
recordConsumer.addBoolean(((BooleanObjectInspector) inspector).get(value));
break;
case FLOAT:
recordConsumer.addFloat(((FloatObjectInspector) inspector).get(value));
break;
case BYTE:
recordConsumer.addInteger(((ByteObjectInspector) inspector).get(value));
break;
case INT:
recordConsumer.addInteger(((IntObjectInspector) inspector).get(value));
break;
case LONG:
recordConsumer.addLong(((LongObjectInspector) inspector).get(value));
break;
case SHORT:
recordConsumer.addInteger(((ShortObjectInspector) inspector).get(value));
break;
case STRING:
String v = ((StringObjectInspector) inspector).getPrimitiveJavaObject(value);
recordConsumer.addBinary(Binary.fromString(v));
break;
case CHAR:
String vChar = ((HiveCharObjectInspector) inspector).getPrimitiveJavaObject(value).getStrippedValue();
recordConsumer.addBinary(Binary.fromString(vChar));
break;
case VARCHAR:
String vVarchar = ((HiveVarcharObjectInspector) inspector).getPrimitiveJavaObject(value).getValue();
recordConsumer.addBinary(Binary.fromString(vVarchar));
break;
case BINARY:
byte[] vBinary = ((BinaryObjectInspector) inspector).getPrimitiveJavaObject(value);
recordConsumer.addBinary(Binary.fromByteArray(vBinary));
break;
case TIMESTAMP:
Timestamp ts = ((TimestampObjectInspector) inspector).getPrimitiveJavaObject(value);
recordConsumer.addBinary(NanoTimeUtils.getNanoTime(ts, false).toBinary());
break;
case DECIMAL:
HiveDecimal vDecimal = ((HiveDecimal) inspector.getPrimitiveJavaObject(value));
DecimalTypeInfo decTypeInfo = (DecimalTypeInfo) inspector.getTypeInfo();
recordConsumer.addBinary(decimalToBinary(vDecimal, decTypeInfo));
break;
case DATE:
Date vDate = ((DateObjectInspector) inspector).getPrimitiveJavaObject(value);
recordConsumer.addInteger(vDate.toEpochDay());
break;
default:
throw new IllegalArgumentException("Unsupported primitive data type: " + inspector.getPrimitiveCategory());
}
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector in project trino by trinodb.
the class TestDataWritableWriter method writePrimitive.
/**
* It writes the primitive value to the Parquet RecordConsumer.
*
* @param value The object that contains the primitive value.
* @param inspector The object inspector used to get the correct value type.
*/
private void writePrimitive(Object value, PrimitiveObjectInspector inspector) {
if (value == null) {
return;
}
switch(inspector.getPrimitiveCategory()) {
case VOID:
return;
case DOUBLE:
recordConsumer.addDouble(((DoubleObjectInspector) inspector).get(value));
break;
case BOOLEAN:
recordConsumer.addBoolean(((BooleanObjectInspector) inspector).get(value));
break;
case FLOAT:
recordConsumer.addFloat(((FloatObjectInspector) inspector).get(value));
break;
case BYTE:
recordConsumer.addInteger(((ByteObjectInspector) inspector).get(value));
break;
case INT:
recordConsumer.addInteger(((IntObjectInspector) inspector).get(value));
break;
case LONG:
recordConsumer.addLong(((LongObjectInspector) inspector).get(value));
break;
case SHORT:
recordConsumer.addInteger(((ShortObjectInspector) inspector).get(value));
break;
case STRING:
String v = ((StringObjectInspector) inspector).getPrimitiveJavaObject(value);
recordConsumer.addBinary(Binary.fromString(v));
break;
case CHAR:
String vChar = ((HiveCharObjectInspector) inspector).getPrimitiveJavaObject(value).getStrippedValue();
recordConsumer.addBinary(Binary.fromString(vChar));
break;
case VARCHAR:
String vVarchar = ((HiveVarcharObjectInspector) inspector).getPrimitiveJavaObject(value).getValue();
recordConsumer.addBinary(Binary.fromString(vVarchar));
break;
case BINARY:
byte[] vBinary = ((BinaryObjectInspector) inspector).getPrimitiveJavaObject(value);
recordConsumer.addBinary(Binary.fromByteArray(vBinary));
break;
case TIMESTAMP:
Timestamp ts = ((TimestampObjectInspector) inspector).getPrimitiveJavaObject(value);
recordConsumer.addBinary(NanoTimeUtils.getNanoTime(ts, false).toBinary());
break;
case DECIMAL:
HiveDecimal vDecimal = ((HiveDecimal) inspector.getPrimitiveJavaObject(value));
DecimalTypeInfo decTypeInfo = (DecimalTypeInfo) inspector.getTypeInfo();
recordConsumer.addBinary(decimalToBinary(vDecimal, decTypeInfo));
break;
case DATE:
Date vDate = ((DateObjectInspector) inspector).getPrimitiveJavaObject(value);
recordConsumer.addInteger(vDate.toEpochDay());
break;
default:
throw new IllegalArgumentException("Unsupported primitive data type: " + inspector.getPrimitiveCategory());
}
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector in project azkaban by azkaban.
the class SerDeUtilsWrapper method buildJSONString.
private static void buildJSONString(StringBuilder sb, Object obj, ObjectInspector objIns) {
String nullStr = "null";
switch(objIns.getCategory()) {
case PRIMITIVE:
{
PrimitiveObjectInspector poi = (PrimitiveObjectInspector) objIns;
if (obj == null) {
sb.append(nullStr);
} else {
switch(poi.getPrimitiveCategory()) {
case BOOLEAN:
{
boolean b = ((BooleanObjectInspector) poi).get(obj);
sb.append(b ? "true" : "false");
break;
}
case BYTE:
{
sb.append(((ByteObjectInspector) poi).get(obj));
break;
}
case SHORT:
{
sb.append(((ShortObjectInspector) poi).get(obj));
break;
}
case INT:
{
sb.append(((IntObjectInspector) poi).get(obj));
break;
}
case LONG:
{
sb.append(((LongObjectInspector) poi).get(obj));
break;
}
case FLOAT:
{
sb.append(((FloatObjectInspector) poi).get(obj));
break;
}
case DOUBLE:
{
sb.append(((DoubleObjectInspector) poi).get(obj));
break;
}
case STRING:
{
sb.append('"');
sb.append(SerDeUtils.escapeString(((StringObjectInspector) poi).getPrimitiveJavaObject(obj)));
sb.append('"');
break;
}
case VARCHAR:
{
sb.append('"');
sb.append(SerDeUtils.escapeString(((HiveVarcharObjectInspector) poi).getPrimitiveJavaObject(obj).toString()));
sb.append('"');
break;
}
case DATE:
{
sb.append('"');
sb.append(((DateObjectInspector) poi).getPrimitiveWritableObject(obj));
sb.append('"');
break;
}
case TIMESTAMP:
{
sb.append('"');
sb.append(((TimestampObjectInspector) poi).getPrimitiveWritableObject(obj));
sb.append('"');
break;
}
case BINARY:
{
BytesWritable bw = ((BinaryObjectInspector) objIns).getPrimitiveWritableObject(obj);
Text txt = new Text();
txt.set(bw.getBytes(), 0, bw.getLength());
// Fix to serialize binary type
sb.append('"');
sb.append(StringEscapeUtils.escapeJava(txt.toString()));
sb.append('"');
break;
}
case DECIMAL:
{
sb.append(((HiveDecimalObjectInspector) objIns).getPrimitiveJavaObject(obj));
break;
}
default:
throw new RuntimeException("Unknown primitive type: " + poi.getPrimitiveCategory());
}
}
break;
}
case LIST:
{
ListObjectInspector loi = (ListObjectInspector) objIns;
ObjectInspector listElementObjectInspector = loi.getListElementObjectInspector();
List<?> olist = loi.getList(obj);
if (olist == null) {
sb.append(nullStr);
} else {
sb.append(SerDeUtils.LBRACKET);
for (int i = 0; i < olist.size(); i++) {
if (i > 0) {
sb.append(SerDeUtils.COMMA);
}
buildJSONString(sb, olist.get(i), listElementObjectInspector);
}
sb.append(SerDeUtils.RBRACKET);
}
break;
}
case MAP:
{
MapObjectInspector moi = (MapObjectInspector) objIns;
ObjectInspector mapKeyObjectInspector = moi.getMapKeyObjectInspector();
ObjectInspector mapValueObjectInspector = moi.getMapValueObjectInspector();
Map<?, ?> omap = moi.getMap(obj);
if (omap == null) {
sb.append(nullStr);
} else {
sb.append(SerDeUtils.LBRACE);
boolean first = true;
for (Object entry : omap.entrySet()) {
if (first) {
first = false;
} else {
sb.append(SerDeUtils.COMMA);
}
Map.Entry<?, ?> e = (Map.Entry<?, ?>) entry;
buildJSONString(sb, e.getKey(), mapKeyObjectInspector);
sb.append(SerDeUtils.COLON);
buildJSONString(sb, e.getValue(), mapValueObjectInspector);
}
sb.append(SerDeUtils.RBRACE);
}
break;
}
case STRUCT:
{
StructObjectInspector soi = (StructObjectInspector) objIns;
List<? extends StructField> structFields = soi.getAllStructFieldRefs();
if (obj == null) {
sb.append(nullStr);
} else {
sb.append(SerDeUtils.LBRACE);
for (int i = 0; i < structFields.size(); i++) {
if (i > 0) {
sb.append(SerDeUtils.COMMA);
}
sb.append(SerDeUtils.QUOTE);
sb.append(structFields.get(i).getFieldName());
sb.append(SerDeUtils.QUOTE);
sb.append(SerDeUtils.COLON);
buildJSONString(sb, soi.getStructFieldData(obj, structFields.get(i)), structFields.get(i).getFieldObjectInspector());
}
sb.append(SerDeUtils.RBRACE);
}
break;
}
case UNION:
{
UnionObjectInspector uoi = (UnionObjectInspector) objIns;
if (obj == null) {
sb.append(nullStr);
} else {
sb.append(SerDeUtils.LBRACE);
sb.append(uoi.getTag(obj));
sb.append(SerDeUtils.COLON);
buildJSONString(sb, uoi.getField(obj), uoi.getObjectInspectors().get(uoi.getTag(obj)));
sb.append(SerDeUtils.RBRACE);
}
break;
}
default:
throw new RuntimeException("Unknown type in ObjectInspector!");
}
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector in project presto by prestodb.
the class SerDeUtils method serializePrimitive.
private static void serializePrimitive(Type type, BlockBuilder builder, Object object, PrimitiveObjectInspector inspector) {
requireNonNull(builder, "parent builder is null");
if (object == null) {
builder.appendNull();
return;
}
switch(inspector.getPrimitiveCategory()) {
case BOOLEAN:
BooleanType.BOOLEAN.writeBoolean(builder, ((BooleanObjectInspector) inspector).get(object));
return;
case BYTE:
TinyintType.TINYINT.writeLong(builder, ((ByteObjectInspector) inspector).get(object));
return;
case SHORT:
SmallintType.SMALLINT.writeLong(builder, ((ShortObjectInspector) inspector).get(object));
return;
case INT:
IntegerType.INTEGER.writeLong(builder, ((IntObjectInspector) inspector).get(object));
return;
case LONG:
BigintType.BIGINT.writeLong(builder, ((LongObjectInspector) inspector).get(object));
return;
case FLOAT:
RealType.REAL.writeLong(builder, floatToRawIntBits(((FloatObjectInspector) inspector).get(object)));
return;
case DOUBLE:
DoubleType.DOUBLE.writeDouble(builder, ((DoubleObjectInspector) inspector).get(object));
return;
case STRING:
type.writeSlice(builder, Slices.utf8Slice(((StringObjectInspector) inspector).getPrimitiveJavaObject(object)));
return;
case VARCHAR:
type.writeSlice(builder, Slices.utf8Slice(((HiveVarcharObjectInspector) inspector).getPrimitiveJavaObject(object).getValue()));
return;
case CHAR:
CharType charType = (CharType) type;
HiveChar hiveChar = ((HiveCharObjectInspector) inspector).getPrimitiveJavaObject(object);
type.writeSlice(builder, truncateToLengthAndTrimSpaces(Slices.utf8Slice(hiveChar.getValue()), charType.getLength()));
return;
case DATE:
DateType.DATE.writeLong(builder, formatDateAsLong(object, (DateObjectInspector) inspector));
return;
case TIMESTAMP:
TimestampType.TIMESTAMP.writeLong(builder, formatTimestampAsLong(object, (TimestampObjectInspector) inspector));
return;
case BINARY:
VARBINARY.writeSlice(builder, Slices.wrappedBuffer(((BinaryObjectInspector) inspector).getPrimitiveJavaObject(object)));
return;
case DECIMAL:
DecimalType decimalType = (DecimalType) type;
HiveDecimalWritable hiveDecimal = ((HiveDecimalObjectInspector) inspector).getPrimitiveWritableObject(object);
if (decimalType.isShort()) {
decimalType.writeLong(builder, DecimalUtils.getShortDecimalValue(hiveDecimal, decimalType.getScale()));
} else {
decimalType.writeSlice(builder, DecimalUtils.getLongDecimalValue(hiveDecimal, decimalType.getScale()));
}
return;
}
throw new RuntimeException("Unknown primitive type: " + inspector.getPrimitiveCategory());
}
Aggregations