Search in sources :

Example 6 with TypeInfoFactory.getCharTypeInfo

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

the class TestGenericUDFPrintf method testVarcharFormat.

@Test
public void testVarcharFormat() throws HiveException {
    GenericUDFPrintf udf = new GenericUDFPrintf();
    ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getVarcharTypeInfo(7)), PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getCharTypeInfo(5)) };
    HiveCharWritable argChar = new HiveCharWritable();
    argChar.set("hello");
    HiveVarcharWritable formatVarchar = new HiveVarcharWritable();
    formatVarchar.set("arg1=%s");
    DeferredObject[] args = { new DeferredJavaObject(formatVarchar), new DeferredJavaObject(argChar) };
    PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
    Assert.assertEquals(PrimitiveObjectInspectorFactory.writableStringObjectInspector, oi);
    Text res = (Text) udf.evaluate(args);
    Assert.assertEquals("arg1=hello", 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)

Example 7 with TypeInfoFactory.getCharTypeInfo

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

the class GenericUDFUpper method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 1) {
        throw new UDFArgumentLengthException("UPPER requires 1 argument, got " + arguments.length);
    }
    if (arguments[0].getCategory() != Category.PRIMITIVE) {
        throw new UDFArgumentException("UPPER only takes primitive types, got " + argumentOI.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 8 with TypeInfoFactory.getCharTypeInfo

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

the class GenericUDFConcat method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    // Loop through all the inputs to determine the appropriate return type/length.
    // Return type:
    //  All CHAR inputs: return CHAR
    //  All VARCHAR inputs: return VARCHAR
    //  All CHAR/VARCHAR inputs: return VARCHAR
    //  All BINARY inputs: return BINARY
    //  Otherwise return STRING
    argumentOIs = arguments;
    PrimitiveCategory currentCategory;
    PrimitiveObjectInspector poi;
    boolean fixedLengthReturnValue = true;
    // Only for char/varchar return types
    int returnLength = 0;
    for (int idx = 0; idx < arguments.length; ++idx) {
        if (arguments[idx].getCategory() != Category.PRIMITIVE) {
            throw new UDFArgumentException("CONCAT only takes primitive arguments");
        }
        poi = (PrimitiveObjectInspector) arguments[idx];
        currentCategory = poi.getPrimitiveCategory();
        if (idx == 0) {
            returnType = currentCategory;
        }
        switch(currentCategory) {
            case BINARY:
                fixedLengthReturnValue = false;
                if (returnType != currentCategory) {
                    // mix of binary/non-binary args
                    returnType = PrimitiveCategory.STRING;
                }
                break;
            case CHAR:
            case VARCHAR:
                if (!fixedLengthReturnValue) {
                    returnType = PrimitiveCategory.STRING;
                }
                if (fixedLengthReturnValue && currentCategory == PrimitiveCategory.VARCHAR) {
                    returnType = PrimitiveCategory.VARCHAR;
                }
                break;
            default:
                returnType = PrimitiveCategory.STRING;
                fixedLengthReturnValue = false;
                break;
        }
        // max length for the char/varchar, then the return type reverts to string.
        if (fixedLengthReturnValue) {
            returnLength += GenericUDFUtils.StringHelper.getFixedStringSizeForType(poi);
            if ((returnType == PrimitiveCategory.VARCHAR && returnLength > HiveVarchar.MAX_VARCHAR_LENGTH) || (returnType == PrimitiveCategory.CHAR && returnLength > HiveChar.MAX_CHAR_LENGTH)) {
                returnType = PrimitiveCategory.STRING;
                fixedLengthReturnValue = false;
            }
        }
    }
    if (returnType == PrimitiveCategory.BINARY) {
        bw = new BytesWritable[arguments.length];
        return PrimitiveObjectInspectorFactory.writableBinaryObjectInspector;
    } else {
        // treat all inputs as string, the return value will be converted to the appropriate type.
        createStringConverters();
        returnHelper = new GenericUDFUtils.StringHelper(returnType);
        BaseCharTypeInfo typeInfo;
        switch(returnType) {
            case STRING:
                return PrimitiveObjectInspectorFactory.writableStringObjectInspector;
            case CHAR:
                typeInfo = TypeInfoFactory.getCharTypeInfo(returnLength);
                return PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);
            case VARCHAR:
                typeInfo = TypeInfoFactory.getVarcharTypeInfo(returnLength);
                return PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);
            default:
                throw new UDFArgumentException("Unexpected CONCAT return type of " + returnType);
        }
    }
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) BaseCharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.BaseCharTypeInfo) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)

Example 9 with TypeInfoFactory.getCharTypeInfo

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

the class TestGenericUDFCeil method testChar.

@Test
public void testChar() throws HiveException {
    GenericUDFCeil udf = new GenericUDFCeil();
    HiveChar vc = new HiveChar("-32300.004747", 12);
    HiveCharWritable input = new HiveCharWritable(vc);
    CharTypeInfo inputTypeInfo = TypeInfoFactory.getCharTypeInfo(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(-32300L, 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) CharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) HiveCharWritable(org.apache.hadoop.hive.serde2.io.HiveCharWritable) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) LongWritable(org.apache.hadoop.io.LongWritable) Test(org.junit.Test)

Example 10 with TypeInfoFactory.getCharTypeInfo

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

the class TestGenericUDFFloor method testChar.

@Test
public void testChar() throws HiveException {
    GenericUDFFloor udf = new GenericUDFFloor();
    HiveChar vc = new HiveChar("32300.004747", 12);
    HiveCharWritable input = new HiveCharWritable(vc);
    CharTypeInfo inputTypeInfo = TypeInfoFactory.getCharTypeInfo(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(32300L, 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) CharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) HiveCharWritable(org.apache.hadoop.hive.serde2.io.HiveCharWritable) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) LongWritable(org.apache.hadoop.io.LongWritable) Test(org.junit.Test)

Aggregations

PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)10 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)9 DeferredJavaObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject)7 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)7 HiveCharWritable (org.apache.hadoop.hive.serde2.io.HiveCharWritable)7 Test (org.junit.Test)7 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)4 CharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo)4 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)3 HiveVarcharWritable (org.apache.hadoop.hive.serde2.io.HiveVarcharWritable)3 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)3 BaseCharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.BaseCharTypeInfo)3 Text (org.apache.hadoop.io.Text)3 UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)2 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)2 PrimitiveObjectInspectorConverter (org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter)2 StringConverter (org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.StringConverter)2 LongWritable (org.apache.hadoop.io.LongWritable)2