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