Search in sources :

Example 1 with TypeInfoFactory.getVarcharTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getVarcharTypeInfo in project hive by apache.

the class GenericUDFLower method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 1) {
        throw new UDFArgumentLengthException("LOWER requires 1 argument, got " + arguments.length);
    }
    if (arguments[0].getCategory() != Category.PRIMITIVE) {
        throw new UDFArgumentException("LOWER only takes primitive types, got " + arguments[0].getTypeName());
    }
    argumentOI = (PrimitiveObjectInspector) arguments[0];
    stringConverter = new PrimitiveObjectInspectorConverter.StringConverter(argumentOI);
    PrimitiveCategory inputType = argumentOI.getPrimitiveCategory();
    ObjectInspector outputOI = null;
    BaseCharTypeInfo typeInfo;
    switch(inputType) {
        case CHAR:
            // return type should have same length as the input.
            returnType = inputType;
            typeInfo = TypeInfoFactory.getCharTypeInfo(GenericUDFUtils.StringHelper.getFixedStringSizeForType(argumentOI));
            outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);
            break;
        case VARCHAR:
            // return type should have same length as the input.
            returnType = inputType;
            typeInfo = TypeInfoFactory.getVarcharTypeInfo(GenericUDFUtils.StringHelper.getFixedStringSizeForType(argumentOI));
            outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);
            break;
        default:
            returnType = PrimitiveCategory.STRING;
            outputOI = PrimitiveObjectInspectorFactory.writableStringObjectInspector;
            break;
    }
    returnHelper = new GenericUDFUtils.StringHelper(returnType);
    return outputOI;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) BaseCharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.BaseCharTypeInfo) UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) PrimitiveObjectInspectorConverter(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter) StringConverter(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.StringConverter) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)

Example 2 with TypeInfoFactory.getVarcharTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getVarcharTypeInfo in project hive by apache.

the class TestGenericUDFCeil method testVarchar.

@Test
public void testVarchar() throws HiveException {
    GenericUDFCeil udf = new GenericUDFCeil();
    HiveVarchar vc = new HiveVarchar("32300.004747", 12);
    HiveVarcharWritable input = new HiveVarcharWritable(vc);
    VarcharTypeInfo inputTypeInfo = TypeInfoFactory.getVarcharTypeInfo(12);
    ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(inputTypeInfo) };
    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(32301L, 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) VarcharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) HiveVarcharWritable(org.apache.hadoop.hive.serde2.io.HiveVarcharWritable) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) HiveVarchar(org.apache.hadoop.hive.common.type.HiveVarchar) LongWritable(org.apache.hadoop.io.LongWritable) Test(org.junit.Test)

Example 3 with TypeInfoFactory.getVarcharTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getVarcharTypeInfo in project hive by apache.

the class TestGenericUDFPrintf method testCharVarcharArgs.

@Test
public void testCharVarcharArgs() throws HiveException {
    GenericUDFPrintf udf = new GenericUDFPrintf();
    ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableStringObjectInspector, PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getCharTypeInfo(5)), PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getVarcharTypeInfo(7)) };
    HiveCharWritable argChar = new HiveCharWritable();
    argChar.set("hello");
    HiveVarcharWritable argVarchar = new HiveVarcharWritable();
    argVarchar.set("world");
    DeferredObject[] args = { new DeferredJavaObject(new Text("1st: %s, 2nd: %s")), new DeferredJavaObject(argChar), new DeferredJavaObject(argVarchar) };
    PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
    Assert.assertEquals(PrimitiveObjectInspectorFactory.writableStringObjectInspector, oi);
    Text res = (Text) udf.evaluate(args);
    Assert.assertEquals("1st: hello, 2nd: world", res.toString());
}
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) HiveCharWritable(org.apache.hadoop.hive.serde2.io.HiveCharWritable) HiveVarcharWritable(org.apache.hadoop.hive.serde2.io.HiveVarcharWritable) Text(org.apache.hadoop.io.Text) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) Test(org.junit.Test)

Example 4 with TypeInfoFactory.getVarcharTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getVarcharTypeInfo in project hive by apache.

the class TestGenericUDFOPPositive method testVarchar.

@Test
public void testVarchar() throws HiveException {
    GenericUDFOPPositive udf = new GenericUDFOPPositive();
    HiveVarchar vc = new HiveVarchar("32300.004747", 12);
    HiveVarcharWritable input = new HiveVarcharWritable(vc);
    VarcharTypeInfo inputTypeInfo = TypeInfoFactory.getVarcharTypeInfo(12);
    ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(inputTypeInfo) };
    DeferredObject[] args = { new DeferredJavaObject(input) };
    PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
    Assert.assertEquals(TypeInfoFactory.doubleTypeInfo, oi.getTypeInfo());
    DoubleWritable res = (DoubleWritable) udf.evaluate(args);
    Assert.assertEquals(32300.004747, res.get(), EPSILON);
}
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) VarcharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) HiveVarcharWritable(org.apache.hadoop.hive.serde2.io.HiveVarcharWritable) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) DoubleWritable(org.apache.hadoop.hive.serde2.io.DoubleWritable) HiveVarchar(org.apache.hadoop.hive.common.type.HiveVarchar) Test(org.junit.Test)

Example 5 with TypeInfoFactory.getVarcharTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getVarcharTypeInfo in project hive by apache.

the class TestGenericUDFPrintf method testCharFormat.

@Test
public void testCharFormat() throws HiveException {
    GenericUDFPrintf udf = new GenericUDFPrintf();
    ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getCharTypeInfo(10)), PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getVarcharTypeInfo(7)) };
    HiveCharWritable formatChar = new HiveCharWritable();
    formatChar.set("arg1=%s");
    HiveVarcharWritable argVarchar = new HiveVarcharWritable();
    argVarchar.set("world");
    DeferredObject[] args = { new DeferredJavaObject(formatChar), new DeferredJavaObject(argVarchar) };
    PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
    Assert.assertEquals(PrimitiveObjectInspectorFactory.writableStringObjectInspector, oi);
    Text res = (Text) udf.evaluate(args);
    Assert.assertEquals("arg1=world", res.toString());
}
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) HiveCharWritable(org.apache.hadoop.hive.serde2.io.HiveCharWritable) HiveVarcharWritable(org.apache.hadoop.hive.serde2.io.HiveVarcharWritable) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) Text(org.apache.hadoop.io.Text) Test(org.junit.Test)

Aggregations

PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)10 Test (org.junit.Test)10 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)9 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)7 DeferredJavaObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject)7 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)7 HiveVarcharWritable (org.apache.hadoop.hive.serde2.io.HiveVarcharWritable)7 VarcharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo)4 HashMap (java.util.HashMap)3 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)3 SearchArgument (org.apache.hadoop.hive.ql.io.sarg.SearchArgument)3 HiveCharWritable (org.apache.hadoop.hive.serde2.io.HiveCharWritable)3 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)3 BaseCharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.BaseCharTypeInfo)3 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)3 Text (org.apache.hadoop.io.Text)3 FilterPredicate (org.apache.parquet.filter2.predicate.FilterPredicate)3 MessageType (org.apache.parquet.schema.MessageType)3 UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)2 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)2