use of org.apache.hadoop.hive.serde2.io.HiveIntervalYearMonthWritable in project hive by apache.
the class TestVectorSerDeRow method serializeRow.
private Output serializeRow(Object[] row, VectorRandomRowSource source, SerializeWrite serializeWrite) throws HiveException, IOException {
Output output = new Output();
serializeWrite.set(output);
PrimitiveTypeInfo[] primitiveTypeInfos = source.primitiveTypeInfos();
for (int i = 0; i < primitiveTypeInfos.length; i++) {
Object object = row[i];
PrimitiveCategory primitiveCategory = primitiveTypeInfos[i].getPrimitiveCategory();
switch(primitiveCategory) {
case BOOLEAN:
{
BooleanWritable expectedWritable = (BooleanWritable) object;
boolean value = expectedWritable.get();
serializeWrite.writeBoolean(value);
}
break;
case BYTE:
{
ByteWritable expectedWritable = (ByteWritable) object;
byte value = expectedWritable.get();
serializeWrite.writeByte(value);
}
break;
case SHORT:
{
ShortWritable expectedWritable = (ShortWritable) object;
short value = expectedWritable.get();
serializeWrite.writeShort(value);
}
break;
case INT:
{
IntWritable expectedWritable = (IntWritable) object;
int value = expectedWritable.get();
serializeWrite.writeInt(value);
}
break;
case LONG:
{
LongWritable expectedWritable = (LongWritable) object;
long value = expectedWritable.get();
serializeWrite.writeLong(value);
}
break;
case DATE:
{
DateWritable expectedWritable = (DateWritable) object;
Date value = expectedWritable.get();
serializeWrite.writeDate(value);
}
break;
case FLOAT:
{
FloatWritable expectedWritable = (FloatWritable) object;
float value = expectedWritable.get();
serializeWrite.writeFloat(value);
}
break;
case DOUBLE:
{
DoubleWritable expectedWritable = (DoubleWritable) object;
double value = expectedWritable.get();
serializeWrite.writeDouble(value);
}
break;
case STRING:
{
Text text = (Text) object;
serializeWrite.writeString(text.getBytes(), 0, text.getLength());
}
break;
case CHAR:
{
HiveCharWritable expectedWritable = (HiveCharWritable) object;
HiveChar value = expectedWritable.getHiveChar();
serializeWrite.writeHiveChar(value);
}
break;
case VARCHAR:
{
HiveVarcharWritable expectedWritable = (HiveVarcharWritable) object;
HiveVarchar value = expectedWritable.getHiveVarchar();
serializeWrite.writeHiveVarchar(value);
}
break;
case BINARY:
{
BytesWritable expectedWritable = (BytesWritable) object;
byte[] bytes = expectedWritable.getBytes();
int length = expectedWritable.getLength();
serializeWrite.writeBinary(bytes, 0, length);
}
break;
case TIMESTAMP:
{
TimestampWritable expectedWritable = (TimestampWritable) object;
Timestamp value = expectedWritable.getTimestamp();
serializeWrite.writeTimestamp(value);
}
break;
case INTERVAL_YEAR_MONTH:
{
HiveIntervalYearMonthWritable expectedWritable = (HiveIntervalYearMonthWritable) object;
HiveIntervalYearMonth value = expectedWritable.getHiveIntervalYearMonth();
serializeWrite.writeHiveIntervalYearMonth(value);
}
break;
case INTERVAL_DAY_TIME:
{
HiveIntervalDayTimeWritable expectedWritable = (HiveIntervalDayTimeWritable) object;
HiveIntervalDayTime value = expectedWritable.getHiveIntervalDayTime();
serializeWrite.writeHiveIntervalDayTime(value);
}
break;
case DECIMAL:
{
HiveDecimalWritable expectedWritable = (HiveDecimalWritable) object;
HiveDecimal value = expectedWritable.getHiveDecimal();
serializeWrite.writeHiveDecimal(value, ((DecimalTypeInfo) primitiveTypeInfos[i]).scale());
}
break;
default:
throw new HiveException("Unexpected primitive category " + primitiveCategory);
}
}
return output;
}
use of org.apache.hadoop.hive.serde2.io.HiveIntervalYearMonthWritable in project hive by apache.
the class TestVectorSerDeRow method deserializeAndVerify.
void deserializeAndVerify(Output output, DeserializeRead deserializeRead, VectorRandomRowSource source, Object[] expectedRow) throws HiveException, IOException {
deserializeRead.set(output.getData(), 0, output.getLength());
PrimitiveCategory[] primitiveCategories = source.primitiveCategories();
for (int i = 0; i < primitiveCategories.length; i++) {
Object expected = expectedRow[i];
PrimitiveCategory primitiveCategory = primitiveCategories[i];
PrimitiveTypeInfo primitiveTypeInfo = source.primitiveTypeInfos()[i];
if (!deserializeRead.readNextField()) {
throw new HiveException("Unexpected NULL when reading primitiveCategory " + primitiveCategory + " expected (" + expected.getClass().getName() + ", " + expected.toString() + ") " + " deserializeRead " + deserializeRead.getClass().getName());
}
switch(primitiveCategory) {
case BOOLEAN:
{
Boolean value = deserializeRead.currentBoolean;
BooleanWritable expectedWritable = (BooleanWritable) expected;
if (!value.equals(expectedWritable.get())) {
TestCase.fail("Boolean field mismatch (expected " + expected + " found " + value + ")");
}
}
break;
case BYTE:
{
Byte value = deserializeRead.currentByte;
ByteWritable expectedWritable = (ByteWritable) expected;
if (!value.equals(expectedWritable.get())) {
TestCase.fail("Byte field mismatch (expected " + (int) expected + " found " + (int) value + ")");
}
}
break;
case SHORT:
{
Short value = deserializeRead.currentShort;
ShortWritable expectedWritable = (ShortWritable) expected;
if (!value.equals(expectedWritable.get())) {
TestCase.fail("Short field mismatch (expected " + expected + " found " + value + ")");
}
}
break;
case INT:
{
Integer value = deserializeRead.currentInt;
IntWritable expectedWritable = (IntWritable) expected;
if (!value.equals(expectedWritable.get())) {
TestCase.fail("Int field mismatch (expected " + expected + " found " + value + ")");
}
}
break;
case LONG:
{
Long value = deserializeRead.currentLong;
LongWritable expectedWritable = (LongWritable) expected;
if (!value.equals(expectedWritable.get())) {
TestCase.fail("Long field mismatch (expected " + expected + " found " + value + ")");
}
}
break;
case DATE:
{
DateWritable value = deserializeRead.currentDateWritable;
DateWritable expectedWritable = (DateWritable) expected;
if (!value.equals(expectedWritable)) {
TestCase.fail("Date field mismatch (expected " + expected.toString() + " found " + value.toString() + ")");
}
}
break;
case FLOAT:
{
Float value = deserializeRead.currentFloat;
FloatWritable expectedWritable = (FloatWritable) expected;
if (!value.equals(expectedWritable.get())) {
TestCase.fail("Float field mismatch (expected " + expected + " found " + value + ")");
}
}
break;
case DOUBLE:
{
Double value = deserializeRead.currentDouble;
DoubleWritable expectedWritable = (DoubleWritable) expected;
if (!value.equals(expectedWritable.get())) {
TestCase.fail("Double field mismatch (expected " + expected + " found " + value + ")");
}
}
break;
case STRING:
case CHAR:
case VARCHAR:
case BINARY:
{
byte[] stringBytes = Arrays.copyOfRange(deserializeRead.currentBytes, deserializeRead.currentBytesStart, deserializeRead.currentBytesStart + deserializeRead.currentBytesLength);
Text text = new Text(stringBytes);
String string = text.toString();
switch(primitiveCategory) {
case STRING:
{
Text expectedWritable = (Text) expected;
if (!string.equals(expectedWritable.toString())) {
TestCase.fail("String field mismatch (expected '" + expectedWritable.toString() + "' found '" + string + "')");
}
}
break;
case CHAR:
{
HiveChar hiveChar = new HiveChar(string, ((CharTypeInfo) primitiveTypeInfo).getLength());
HiveCharWritable expectedWritable = (HiveCharWritable) expected;
if (!hiveChar.equals(expectedWritable.getHiveChar())) {
TestCase.fail("Char field mismatch (expected '" + expectedWritable.getHiveChar() + "' found '" + hiveChar + "')");
}
}
break;
case VARCHAR:
{
HiveVarchar hiveVarchar = new HiveVarchar(string, ((VarcharTypeInfo) primitiveTypeInfo).getLength());
HiveVarcharWritable expectedWritable = (HiveVarcharWritable) expected;
if (!hiveVarchar.equals(expectedWritable.getHiveVarchar())) {
TestCase.fail("Varchar field mismatch (expected '" + expectedWritable.getHiveVarchar() + "' found '" + hiveVarchar + "')");
}
}
break;
case BINARY:
{
BytesWritable expectedWritable = (BytesWritable) expected;
if (stringBytes.length != expectedWritable.getLength()) {
TestCase.fail("Byte Array field mismatch (expected " + expected + " found " + stringBytes + ")");
}
byte[] expectedBytes = expectedWritable.getBytes();
for (int b = 0; b < stringBytes.length; b++) {
if (stringBytes[b] != expectedBytes[b]) {
TestCase.fail("Byte Array field mismatch (expected " + expected + " found " + stringBytes + ")");
}
}
}
break;
default:
throw new HiveException("Unexpected primitive category " + primitiveCategory);
}
}
break;
case DECIMAL:
{
HiveDecimal value = deserializeRead.currentHiveDecimalWritable.getHiveDecimal();
if (value == null) {
TestCase.fail("Decimal field evaluated to NULL");
}
HiveDecimalWritable expectedWritable = (HiveDecimalWritable) expected;
if (!value.equals(expectedWritable.getHiveDecimal())) {
DecimalTypeInfo decimalTypeInfo = (DecimalTypeInfo) primitiveTypeInfo;
int precision = decimalTypeInfo.getPrecision();
int scale = decimalTypeInfo.getScale();
TestCase.fail("Decimal field mismatch (expected " + expectedWritable.getHiveDecimal() + " found " + value.toString() + ") precision " + precision + ", scale " + scale);
}
}
break;
case TIMESTAMP:
{
Timestamp value = deserializeRead.currentTimestampWritable.getTimestamp();
TimestampWritable expectedWritable = (TimestampWritable) expected;
if (!value.equals(expectedWritable.getTimestamp())) {
TestCase.fail("Timestamp field mismatch (expected " + expectedWritable.getTimestamp() + " found " + value.toString() + ")");
}
}
break;
case INTERVAL_YEAR_MONTH:
{
HiveIntervalYearMonth value = deserializeRead.currentHiveIntervalYearMonthWritable.getHiveIntervalYearMonth();
HiveIntervalYearMonthWritable expectedWritable = (HiveIntervalYearMonthWritable) expected;
HiveIntervalYearMonth expectedValue = expectedWritable.getHiveIntervalYearMonth();
if (!value.equals(expectedValue)) {
TestCase.fail("HiveIntervalYearMonth field mismatch (expected " + expectedValue + " found " + value.toString() + ")");
}
}
break;
case INTERVAL_DAY_TIME:
{
HiveIntervalDayTime value = deserializeRead.currentHiveIntervalDayTimeWritable.getHiveIntervalDayTime();
HiveIntervalDayTimeWritable expectedWritable = (HiveIntervalDayTimeWritable) expected;
HiveIntervalDayTime expectedValue = expectedWritable.getHiveIntervalDayTime();
if (!value.equals(expectedValue)) {
TestCase.fail("HiveIntervalDayTime field mismatch (expected " + expectedValue + " found " + value.toString() + ")");
}
}
break;
default:
throw new HiveException("Unexpected primitive category " + primitiveCategory);
}
}
TestCase.assertTrue(deserializeRead.isEndOfInputReached());
}
use of org.apache.hadoop.hive.serde2.io.HiveIntervalYearMonthWritable in project hive by apache.
the class TestGenericUDFOPPlus method testTimestampPlusIntervalYearMonth.
@Test
public void testTimestampPlusIntervalYearMonth() throws Exception {
GenericUDFOPPlus udf = new GenericUDFOPPlus();
TimestampWritable left = new TimestampWritable(Timestamp.valueOf("2001-11-15 01:02:03.123456789"));
HiveIntervalYearMonthWritable right = new HiveIntervalYearMonthWritable(HiveIntervalYearMonth.valueOf("2-2"));
ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableTimestampObjectInspector, PrimitiveObjectInspectorFactory.writableHiveIntervalYearMonthObjectInspector };
DeferredObject[] args = { new DeferredJavaObject(left), new DeferredJavaObject(right) };
PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
Assert.assertEquals(TypeInfoFactory.timestampTypeInfo, oi.getTypeInfo());
TimestampWritable res = (TimestampWritable) udf.evaluate(args);
Assert.assertEquals(Timestamp.valueOf("2004-01-15 01:02:03.123456789"), res.getTimestamp());
}
use of org.apache.hadoop.hive.serde2.io.HiveIntervalYearMonthWritable in project hive by apache.
the class TestGenericUDFOPPlus method testIntervalYearMonthPlusDate.
@Test
public void testIntervalYearMonthPlusDate() throws Exception {
GenericUDFOPPlus udf = new GenericUDFOPPlus();
HiveIntervalYearMonthWritable left = new HiveIntervalYearMonthWritable(HiveIntervalYearMonth.valueOf("2-8"));
DateWritable right = new DateWritable(Date.valueOf("2001-06-15"));
ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableHiveIntervalYearMonthObjectInspector, PrimitiveObjectInspectorFactory.writableDateObjectInspector };
DeferredObject[] args = { new DeferredJavaObject(left), new DeferredJavaObject(right) };
PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
Assert.assertEquals(TypeInfoFactory.dateTypeInfo, oi.getTypeInfo());
DateWritable res = (DateWritable) udf.evaluate(args);
Assert.assertEquals(Date.valueOf("2004-02-15"), res.get());
}
use of org.apache.hadoop.hive.serde2.io.HiveIntervalYearMonthWritable in project hive by apache.
the class ObjectInspectorUtils method compare.
/**
* Compare two objects with their respective ObjectInspectors.
* if nullValueOpt is MAXVALUE, treat null as maximum value.
* if nullValueOpt is MINVALUE, treat null as minimum value.
*/
public static int compare(Object o1, ObjectInspector oi1, Object o2, ObjectInspector oi2, MapEqualComparer mapEqualComparer, NullValueOption nullValueOpt) {
if (oi1.getCategory() != oi2.getCategory()) {
return oi1.getCategory().compareTo(oi2.getCategory());
}
int nullCmpRtn = -1;
switch(nullValueOpt) {
case MAXVALUE:
nullCmpRtn = 1;
break;
case MINVALUE:
nullCmpRtn = -1;
break;
}
if (o1 == null) {
return o2 == null ? 0 : nullCmpRtn;
} else if (o2 == null) {
return -nullCmpRtn;
}
switch(oi1.getCategory()) {
case PRIMITIVE:
{
PrimitiveObjectInspector poi1 = ((PrimitiveObjectInspector) oi1);
PrimitiveObjectInspector poi2 = ((PrimitiveObjectInspector) oi2);
if (poi1.getPrimitiveCategory() != poi2.getPrimitiveCategory()) {
return poi1.getPrimitiveCategory().compareTo(poi2.getPrimitiveCategory());
}
switch(poi1.getPrimitiveCategory()) {
case VOID:
return 0;
case BOOLEAN:
{
int v1 = ((BooleanObjectInspector) poi1).get(o1) ? 1 : 0;
int v2 = ((BooleanObjectInspector) poi2).get(o2) ? 1 : 0;
return v1 - v2;
}
case BYTE:
{
int v1 = ((ByteObjectInspector) poi1).get(o1);
int v2 = ((ByteObjectInspector) poi2).get(o2);
return v1 - v2;
}
case SHORT:
{
int v1 = ((ShortObjectInspector) poi1).get(o1);
int v2 = ((ShortObjectInspector) poi2).get(o2);
return v1 - v2;
}
case INT:
{
int v1 = ((IntObjectInspector) poi1).get(o1);
int v2 = ((IntObjectInspector) poi2).get(o2);
return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
}
case LONG:
{
long v1 = ((LongObjectInspector) poi1).get(o1);
long v2 = ((LongObjectInspector) poi2).get(o2);
return v1 > v2 ? 1 : (v1 < v2 ? -1 : 0);
}
case FLOAT:
{
float v1 = ((FloatObjectInspector) poi1).get(o1);
float v2 = ((FloatObjectInspector) poi2).get(o2);
// The IEEE 754 floating point spec specifies that signed -0.0 and 0.0 should be treated as equal.
if (v1 == 0.0f && v2 == 0.0f) {
return 0;
} else {
// Float.compare() treats -0.0 and 0.0 as different
return Float.compare(v1, v2);
}
}
case DOUBLE:
{
double v1 = ((DoubleObjectInspector) poi1).get(o1);
double v2 = ((DoubleObjectInspector) poi2).get(o2);
// The IEEE 754 floating point spec specifies that signed -0.0 and 0.0 should be treated as equal.
if (v1 == 0.0d && v2 == 0.0d) {
return 0;
} else {
// Double.compare() treats -0.0 and 0.0 as different
return Double.compare(v1, v2);
}
}
case STRING:
{
if (poi1.preferWritable() || poi2.preferWritable()) {
Text t1 = (Text) poi1.getPrimitiveWritableObject(o1);
Text t2 = (Text) poi2.getPrimitiveWritableObject(o2);
return t1 == null ? (t2 == null ? 0 : -1) : (t2 == null ? 1 : t1.compareTo(t2));
} else {
String s1 = (String) poi1.getPrimitiveJavaObject(o1);
String s2 = (String) poi2.getPrimitiveJavaObject(o2);
return s1 == null ? (s2 == null ? 0 : -1) : (s2 == null ? 1 : s1.compareTo(s2));
}
}
case CHAR:
{
HiveCharWritable t1 = ((HiveCharObjectInspector) poi1).getPrimitiveWritableObject(o1);
HiveCharWritable t2 = ((HiveCharObjectInspector) poi2).getPrimitiveWritableObject(o2);
return t1.compareTo(t2);
}
case VARCHAR:
{
HiveVarcharWritable t1 = ((HiveVarcharObjectInspector) poi1).getPrimitiveWritableObject(o1);
HiveVarcharWritable t2 = ((HiveVarcharObjectInspector) poi2).getPrimitiveWritableObject(o2);
return t1.compareTo(t2);
}
case BINARY:
{
BytesWritable bw1 = ((BinaryObjectInspector) poi1).getPrimitiveWritableObject(o1);
BytesWritable bw2 = ((BinaryObjectInspector) poi2).getPrimitiveWritableObject(o2);
return bw1.compareTo(bw2);
}
case DATE:
{
DateWritable d1 = ((DateObjectInspector) poi1).getPrimitiveWritableObject(o1);
DateWritable d2 = ((DateObjectInspector) poi2).getPrimitiveWritableObject(o2);
return d1.compareTo(d2);
}
case TIMESTAMP:
{
TimestampWritable t1 = ((TimestampObjectInspector) poi1).getPrimitiveWritableObject(o1);
TimestampWritable t2 = ((TimestampObjectInspector) poi2).getPrimitiveWritableObject(o2);
return t1.compareTo(t2);
}
case INTERVAL_YEAR_MONTH:
{
HiveIntervalYearMonthWritable i1 = ((HiveIntervalYearMonthObjectInspector) poi1).getPrimitiveWritableObject(o1);
HiveIntervalYearMonthWritable i2 = ((HiveIntervalYearMonthObjectInspector) poi2).getPrimitiveWritableObject(o2);
return i1.compareTo(i2);
}
case INTERVAL_DAY_TIME:
{
HiveIntervalDayTimeWritable i1 = ((HiveIntervalDayTimeObjectInspector) poi1).getPrimitiveWritableObject(o1);
HiveIntervalDayTimeWritable i2 = ((HiveIntervalDayTimeObjectInspector) poi2).getPrimitiveWritableObject(o2);
return i1.compareTo(i2);
}
case DECIMAL:
{
HiveDecimalWritable t1 = ((HiveDecimalObjectInspector) poi1).getPrimitiveWritableObject(o1);
HiveDecimalWritable t2 = ((HiveDecimalObjectInspector) poi2).getPrimitiveWritableObject(o2);
return t1.compareTo(t2);
}
default:
{
throw new RuntimeException("Unknown type: " + poi1.getPrimitiveCategory());
}
}
}
case STRUCT:
{
StructObjectInspector soi1 = (StructObjectInspector) oi1;
StructObjectInspector soi2 = (StructObjectInspector) oi2;
List<? extends StructField> fields1 = soi1.getAllStructFieldRefs();
List<? extends StructField> fields2 = soi2.getAllStructFieldRefs();
int minimum = Math.min(fields1.size(), fields2.size());
for (int i = 0; i < minimum; i++) {
int r = compare(soi1.getStructFieldData(o1, fields1.get(i)), fields1.get(i).getFieldObjectInspector(), soi2.getStructFieldData(o2, fields2.get(i)), fields2.get(i).getFieldObjectInspector(), mapEqualComparer, nullValueOpt);
if (r != 0) {
return r;
}
}
return fields1.size() - fields2.size();
}
case LIST:
{
ListObjectInspector loi1 = (ListObjectInspector) oi1;
ListObjectInspector loi2 = (ListObjectInspector) oi2;
int minimum = Math.min(loi1.getListLength(o1), loi2.getListLength(o2));
for (int i = 0; i < minimum; i++) {
int r = compare(loi1.getListElement(o1, i), loi1.getListElementObjectInspector(), loi2.getListElement(o2, i), loi2.getListElementObjectInspector(), mapEqualComparer, nullValueOpt);
if (r != 0) {
return r;
}
}
return loi1.getListLength(o1) - loi2.getListLength(o2);
}
case MAP:
{
if (mapEqualComparer == null) {
throw new RuntimeException("Compare on map type not supported!");
} else {
return mapEqualComparer.compare(o1, (MapObjectInspector) oi1, o2, (MapObjectInspector) oi2);
}
}
case UNION:
{
UnionObjectInspector uoi1 = (UnionObjectInspector) oi1;
UnionObjectInspector uoi2 = (UnionObjectInspector) oi2;
byte tag1 = uoi1.getTag(o1);
byte tag2 = uoi2.getTag(o2);
if (tag1 != tag2) {
return tag1 - tag2;
}
return compare(uoi1.getField(o1), uoi1.getObjectInspectors().get(tag1), uoi2.getField(o2), uoi2.getObjectInspectors().get(tag2), mapEqualComparer, nullValueOpt);
}
default:
throw new RuntimeException("Compare on unknown type: " + oi1.getCategory());
}
}
Aggregations