use of org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector in project hive by apache.
the class AbstractTestGenericUDFOPNumeric method verifyReturnType.
protected void verifyReturnType(GenericUDF udf, String typeStr1, String typeStr2, String expectedTypeStr) throws HiveException {
// Lookup type infos for our input types and expected return type
PrimitiveTypeInfo type1 = TypeInfoFactory.getPrimitiveTypeInfo(typeStr1);
PrimitiveTypeInfo type2 = TypeInfoFactory.getPrimitiveTypeInfo(typeStr2);
PrimitiveTypeInfo expectedType = TypeInfoFactory.getPrimitiveTypeInfo(expectedTypeStr);
// Initialize UDF which will output the return type for the UDF.
ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(type1), PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(type2) };
PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
Assert.assertEquals("Return type for " + udf.getDisplayString(new String[] { typeStr1, typeStr2 }), expectedType, oi.getTypeInfo());
}
use of org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector in project hive by apache.
the class TestGenericUDFAbs method testHiveDecimal.
public void testHiveDecimal() throws HiveException {
GenericUDFAbs udf = new GenericUDFAbs();
int prec = 12;
int scale = 9;
ObjectInspector valueOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(prec, scale));
ObjectInspector[] arguments = { valueOI };
PrimitiveObjectInspector outputOI = (PrimitiveObjectInspector) udf.initialize(arguments);
// Make sure result precision/scale matches the input prec/scale
assertEquals("result precision for abs()", prec, outputOI.precision());
assertEquals("result scale for abs()", scale, outputOI.scale());
DeferredObject valueObj = new DeferredJavaObject(new HiveDecimalWritable(HiveDecimal.create("107.123456789")));
DeferredObject[] args = { valueObj };
HiveDecimalWritable output = (HiveDecimalWritable) udf.evaluate(args);
assertEquals("abs() test for HiveDecimal failed ", 107.123456789, output.getHiveDecimal().doubleValue());
valueObj = new DeferredJavaObject(new HiveDecimalWritable(HiveDecimal.create("-107.123456789")));
args[0] = valueObj;
output = (HiveDecimalWritable) udf.evaluate(args);
assertEquals("abs() test for HiveDecimal failed ", 107.123456789, output.getHiveDecimal().doubleValue());
// null input
args[0] = new DeferredJavaObject(null);
output = (HiveDecimalWritable) udf.evaluate(args);
assertEquals("abs(null)", null, output);
// if value too large, should also be null
args[0] = new DeferredJavaObject(new HiveDecimalWritable(HiveDecimal.create("-1000.123456")));
output = (HiveDecimalWritable) udf.evaluate(args);
assertEquals("abs() of too large decimal value", null, output);
}
use of org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector 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.objectinspector.PrimitiveObjectInspector in project hive by apache.
the class TestGenericUDFOPPlus method testTimestampPlusIntervalDayTime.
@Test
public void testTimestampPlusIntervalDayTime() throws Exception {
GenericUDFOPPlus udf = new GenericUDFOPPlus();
TimestampWritable left = new TimestampWritable(Timestamp.valueOf("2001-01-01 00:00:00"));
HiveIntervalDayTimeWritable right = new HiveIntervalDayTimeWritable(HiveIntervalDayTime.valueOf("1 2:3:4.567"));
ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableTimestampObjectInspector, PrimitiveObjectInspectorFactory.writableHiveIntervalDayTimeObjectInspector };
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("2001-01-02 2:3:4.567"), res.getTimestamp());
}
use of org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector in project hive by apache.
the class TestGenericUDFOPPositive method testShort.
@Test
public void testShort() throws HiveException {
GenericUDFOPPositive udf = new GenericUDFOPPositive();
ShortWritable input = new ShortWritable((short) 74);
ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableShortObjectInspector };
DeferredObject[] args = { new DeferredJavaObject(input) };
PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
Assert.assertEquals(TypeInfoFactory.shortTypeInfo, oi.getTypeInfo());
ShortWritable res = (ShortWritable) udf.evaluate(args);
Assert.assertEquals((short) 74, res.get());
}
Aggregations