Search in sources :

Example 31 with PrimitiveObjectInspectorFactory.writableLongObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableLongObjectInspector in project hive by apache.

the class TestGenericUDFOPPositive method testLong.

@Test
public void testLong() throws HiveException {
    GenericUDFOPPositive udf = new GenericUDFOPPositive();
    LongWritable input = new LongWritable(3234747);
    ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableLongObjectInspector };
    DeferredObject[] args = { new DeferredJavaObject(input) };
    PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
    Assert.assertEquals(TypeInfoFactory.longTypeInfo, oi.getTypeInfo());
    LongWritable res = (LongWritable) udf.evaluate(args);
    Assert.assertEquals(3234747L, res.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) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) LongWritable(org.apache.hadoop.io.LongWritable) Test(org.junit.Test)

Example 32 with PrimitiveObjectInspectorFactory.writableLongObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableLongObjectInspector in project hive by apache.

the class TestGenericUDFOPMultiply method testLongTimesDecimal.

@Test
public void testLongTimesDecimal() throws HiveException {
    GenericUDFOPMultiply udf = new GenericUDFOPMultiply();
    LongWritable left = new LongWritable(104);
    HiveDecimalWritable right = new HiveDecimalWritable(HiveDecimal.create("234.97"));
    ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableLongObjectInspector, PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(9, 4)) };
    DeferredObject[] args = { new DeferredJavaObject(left), new DeferredJavaObject(right) };
    PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
    Assert.assertEquals(TypeInfoFactory.getDecimalTypeInfo(29, 4), oi.getTypeInfo());
    HiveDecimalWritable res = (HiveDecimalWritable) udf.evaluate(args);
    Assert.assertEquals(HiveDecimal.create("24436.88"), res.getHiveDecimal());
}
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) 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) LongWritable(org.apache.hadoop.io.LongWritable) Test(org.junit.Test)

Example 33 with PrimitiveObjectInspectorFactory.writableLongObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableLongObjectInspector in project hive by apache.

the class TestGenericUDFNextDay method testNextDayErrorArg1.

public void testNextDayErrorArg1() throws HiveException {
    @SuppressWarnings("resource") GenericUDFNextDay udf = new GenericUDFNextDay();
    ObjectInspector valueOI0 = PrimitiveObjectInspectorFactory.writableLongObjectInspector;
    ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory.writableStringObjectInspector;
    ObjectInspector[] arguments = { valueOI0, valueOI1 };
    try {
        udf.initialize(arguments);
        assertTrue("UDFArgumentException expected", false);
    } catch (UDFArgumentException e) {
        assertEquals("next_day only takes STRING_GROUP, DATE_GROUP, VOID_GROUP types as 1st argument, got LONG", e.getMessage());
    }
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)

Example 34 with PrimitiveObjectInspectorFactory.writableLongObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableLongObjectInspector in project hive by apache.

the class GenericUDFGrouping method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length < 2) {
        throw new UDFArgumentLengthException("grouping() requires at least 2 argument, got " + arguments.length);
    }
    if (arguments[0].getCategory() != Category.PRIMITIVE) {
        throw new UDFArgumentTypeException(0, "The first argument to grouping() must be primitive");
    }
    PrimitiveObjectInspector arg1OI = (PrimitiveObjectInspector) arguments[0];
    // INT can happen in cases where grouping() is used without grouping sets, in all other cases it should be LONG.
    if (!(arg1OI.getPrimitiveCategory() == PrimitiveCategory.INT || arg1OI.getPrimitiveCategory() == PrimitiveCategory.LONG)) {
        throw new UDFArgumentTypeException(0, "The first argument to grouping() must be an int/long. Got: " + arg1OI.getPrimitiveCategory());
    }
    groupingIdOI = arg1OI;
    indices = new int[arguments.length - 1];
    for (int i = 1; i < arguments.length; i++) {
        PrimitiveObjectInspector arg2OI = (PrimitiveObjectInspector) arguments[i];
        if (!(arg2OI instanceof ConstantObjectInspector)) {
            throw new UDFArgumentTypeException(i, "Must be a constant. Got: " + arg2OI.getClass().getSimpleName());
        }
        indices[i - 1] = PrimitiveObjectInspectorUtils.getInt(((ConstantObjectInspector) arguments[i]).getWritableConstantValue(), arg2OI);
    }
    return PrimitiveObjectInspectorFactory.writableLongObjectInspector;
}
Also used : UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)

Aggregations

ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)29 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)22 LongWritable (org.apache.hadoop.io.LongWritable)22 DeferredJavaObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject)17 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)17 Test (org.junit.Test)17 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)8 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)6 UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)5 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)4 UDFArgumentTypeException (org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException)4 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)3 IntWritable (org.apache.hadoop.io.IntWritable)3 ArrayList (java.util.ArrayList)2 ByteWritable (org.apache.hadoop.hive.serde2.io.ByteWritable)2 ShortWritable (org.apache.hadoop.hive.serde2.io.ShortWritable)2 ConstantObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)2 Converter (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter)2 HiveDecimalObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector)2 FloatWritable (org.apache.hadoop.io.FloatWritable)2