Search in sources :

Example 1 with DeferredJavaObject

use of org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject in project hive by apache.

the class TestGenericUDFAbs method testFloat.

public void testFloat() throws HiveException {
    GenericUDFAbs udf = new GenericUDFAbs();
    ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableFloatObjectInspector;
    ObjectInspector[] arguments = { valueOI };
    udf.initialize(arguments);
    DeferredObject valueObj = new DeferredJavaObject(new FloatWritable(107.78f));
    DeferredObject[] args = { valueObj };
    DoubleWritable output = (DoubleWritable) udf.evaluate(args);
    // Make sure flow and double equality compare works
    assertTrue("abs() test for Float failed ", Math.abs(107.78 - output.get()) < 0.0001);
    valueObj = new DeferredJavaObject(new FloatWritable(-107.78f));
    args[0] = valueObj;
    output = (DoubleWritable) udf.evaluate(args);
    assertTrue("abs() test for Float failed ", Math.abs(107.78 - output.get()) < 0.0001);
}
Also used : PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) FloatWritable(org.apache.hadoop.io.FloatWritable) GenericUDFAbs(org.apache.hadoop.hive.ql.udf.generic.GenericUDFAbs) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) DoubleWritable(org.apache.hadoop.hive.serde2.io.DoubleWritable)

Example 2 with DeferredJavaObject

use of org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject 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);
}
Also used : PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) GenericUDFAbs(org.apache.hadoop.hive.ql.udf.generic.GenericUDFAbs) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)

Example 3 with DeferredJavaObject

use of org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject in project hive by apache.

the class TestGenericUDFAbs method testInt.

public void testInt() throws HiveException {
    GenericUDFAbs udf = new GenericUDFAbs();
    ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableIntObjectInspector;
    ObjectInspector[] arguments = { valueOI };
    udf.initialize(arguments);
    DeferredObject valueObj = new DeferredJavaObject(new IntWritable(107));
    DeferredObject[] args = { valueObj };
    IntWritable output = (IntWritable) udf.evaluate(args);
    assertEquals("abs() test for INT failed ", 107, output.get());
    valueObj = new DeferredJavaObject(new IntWritable(-107));
    args[0] = valueObj;
    output = (IntWritable) udf.evaluate(args);
    assertEquals("abs() test for INT failed ", 107, output.get());
}
Also used : PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) GenericUDFAbs(org.apache.hadoop.hive.ql.udf.generic.GenericUDFAbs) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) IntWritable(org.apache.hadoop.io.IntWritable)

Example 4 with DeferredJavaObject

use of org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject in project hive by apache.

the class TestGenericUDFAbs method testDouble.

public void testDouble() throws HiveException {
    GenericUDFAbs udf = new GenericUDFAbs();
    ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableDoubleObjectInspector;
    ObjectInspector[] arguments = { valueOI };
    udf.initialize(arguments);
    DeferredObject valueObj = new DeferredJavaObject(new DoubleWritable(107.78));
    DeferredObject[] args = { valueObj };
    DoubleWritable output = (DoubleWritable) udf.evaluate(args);
    assertEquals("abs() test for Double failed ", 107.78, output.get());
    valueObj = new DeferredJavaObject(new DoubleWritable(-107.78));
    args[0] = valueObj;
    output = (DoubleWritable) udf.evaluate(args);
    assertEquals("abs() test for Double failed ", 107.78, output.get());
}
Also used : PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) GenericUDFAbs(org.apache.hadoop.hive.ql.udf.generic.GenericUDFAbs) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) DoubleWritable(org.apache.hadoop.hive.serde2.io.DoubleWritable)

Example 5 with DeferredJavaObject

use of org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject in project hive by apache.

the class TestGenericUDFAddMonths method runAndVerify.

private void runAndVerify(String str, short months, String expResult, GenericUDF udf) throws HiveException {
    DeferredObject valueObj0 = new DeferredJavaObject(new Text(str));
    DeferredObject valueObj1 = new DeferredJavaObject(new ShortWritable(months));
    DeferredObject[] args = { valueObj0, valueObj1 };
    Text output = (Text) udf.evaluate(args);
    assertEquals("add_months() test ", expResult, output != null ? output.toString() : null);
}
Also used : DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) Text(org.apache.hadoop.io.Text) ShortWritable(org.apache.hadoop.hive.serde2.io.ShortWritable)

Aggregations

DeferredJavaObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject)203 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)200 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)158 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)129 Test (org.junit.Test)123 Text (org.apache.hadoop.io.Text)53 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)39 LongWritable (org.apache.hadoop.io.LongWritable)34 DateWritable (org.apache.hadoop.hive.serde2.io.DateWritable)29 IntWritable (org.apache.hadoop.io.IntWritable)28 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)26 ByteWritable (org.apache.hadoop.hive.serde2.io.ByteWritable)21 TimestampWritable (org.apache.hadoop.hive.serde2.io.TimestampWritable)20 FloatWritable (org.apache.hadoop.io.FloatWritable)14 ShortWritable (org.apache.hadoop.hive.serde2.io.ShortWritable)13 HiveVarcharWritable (org.apache.hadoop.hive.serde2.io.HiveVarcharWritable)12 HiveIntervalDayTimeWritable (org.apache.hadoop.hive.serde2.io.HiveIntervalDayTimeWritable)10 Date (java.sql.Date)8 HiveIntervalYearMonthWritable (org.apache.hadoop.hive.serde2.io.HiveIntervalYearMonthWritable)8 HiveCharWritable (org.apache.hadoop.hive.serde2.io.HiveCharWritable)7