Search in sources :

Example 6 with PrimitiveObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector in project hive by apache.

the class GenericUDFToChar method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 1) {
        throw new UDFArgumentException("CHAR cast requires a value argument");
    }
    try {
        argumentOI = (PrimitiveObjectInspector) arguments[0];
    } catch (ClassCastException e) {
        throw new UDFArgumentException("The function CHAR takes only primitive types");
    }
    // Check if this UDF has been provided with type params for the output char type
    SettableHiveCharObjectInspector outputOI;
    outputOI = (SettableHiveCharObjectInspector) PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);
    converter = new HiveCharConverter(argumentOI, outputOI);
    return outputOI;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) HiveCharConverter(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.HiveCharConverter) SettableHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableHiveCharObjectInspector)

Example 7 with PrimitiveObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector in project hive by apache.

the class GenericUDFToDate method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length < 1) {
        throw new UDFArgumentLengthException("The function CAST as DATE requires at least one argument, got " + arguments.length);
    }
    try {
        argumentOI = (PrimitiveObjectInspector) arguments[0];
        PrimitiveCategory pc = argumentOI.getPrimitiveCategory();
        PrimitiveGrouping pg = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(pc);
        switch(pg) {
            case DATE_GROUP:
            case STRING_GROUP:
            case VOID_GROUP:
                break;
            default:
                throw new UDFArgumentException("CAST as DATE only allows date,string, or timestamp types");
        }
    } catch (ClassCastException e) {
        throw new UDFArgumentException("The function CAST as DATE takes only primitive types");
    }
    dc = new DateConverter(argumentOI, PrimitiveObjectInspectorFactory.writableDateObjectInspector);
    return PrimitiveObjectInspectorFactory.writableDateObjectInspector;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) DateConverter(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.DateConverter) UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory) PrimitiveGrouping(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping)

Example 8 with PrimitiveObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector in project hive by apache.

the class GenericUDFToUnixTimeStamp method initializeInput.

protected void initializeInput(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length < 1) {
        throw new UDFArgumentLengthException("The function " + getName().toUpperCase() + "requires at least one argument");
    }
    for (ObjectInspector argument : arguments) {
        if (arguments[0].getCategory() != Category.PRIMITIVE) {
            throw new UDFArgumentException(getName().toUpperCase() + " only takes string/date/timestamp types, got " + argument.getTypeName());
        }
    }
    PrimitiveObjectInspector arg1OI = (PrimitiveObjectInspector) arguments[0];
    switch(arg1OI.getPrimitiveCategory()) {
        case CHAR:
        case VARCHAR:
        case STRING:
            inputTextConverter = ObjectInspectorConverters.getConverter(arg1OI, PrimitiveObjectInspectorFactory.javaStringObjectInspector);
            if (arguments.length > 1) {
                PrimitiveObjectInspector arg2OI = (PrimitiveObjectInspector) arguments[1];
                if (PrimitiveObjectInspectorUtils.getPrimitiveGrouping(arg2OI.getPrimitiveCategory()) != PrimitiveGrouping.STRING_GROUP) {
                    throw new UDFArgumentException("The time pattern for " + getName().toUpperCase() + " should be string type");
                }
                patternConverter = ObjectInspectorConverters.getConverter(arg2OI, PrimitiveObjectInspectorFactory.javaStringObjectInspector);
            }
            break;
        case DATE:
            inputDateOI = (DateObjectInspector) arguments[0];
            break;
        case TIMESTAMP:
            inputTimestampOI = (TimestampObjectInspector) arguments[0];
            break;
        default:
            throw new UDFArgumentException("The function " + getName().toUpperCase() + " takes only string/date/timestamp types");
    }
}
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) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) TimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector) DateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector) UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)

Example 9 with PrimitiveObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector in project hive by apache.

the class GenericUDFToVarchar method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 1) {
        throw new UDFArgumentException("VARCHAR cast requires a value argument");
    }
    try {
        argumentOI = (PrimitiveObjectInspector) arguments[0];
    } catch (ClassCastException e) {
        throw new UDFArgumentException("The function VARCHAR takes only primitive types");
    }
    // Check if this UDF has been provided with type params for the output varchar type
    SettableHiveVarcharObjectInspector outputOI;
    outputOI = (SettableHiveVarcharObjectInspector) PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);
    converter = new HiveVarcharConverter(argumentOI, outputOI);
    return outputOI;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) SettableHiveVarcharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableHiveVarcharObjectInspector) HiveVarcharConverter(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.HiveVarcharConverter)

Example 10 with PrimitiveObjectInspector

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());
}
Also used : PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)

Aggregations

PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)232 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)172 Test (org.junit.Test)121 DeferredJavaObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject)110 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)108 LongWritable (org.apache.hadoop.io.LongWritable)34 UDFArgumentTypeException (org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException)33 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)33 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)30 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)28 UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)27 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)26 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)26 Text (org.apache.hadoop.io.Text)26 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)22 StringObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector)20 ByteWritable (org.apache.hadoop.hive.serde2.io.ByteWritable)19 LongObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector)19 IntWritable (org.apache.hadoop.io.IntWritable)18 ArrayList (java.util.ArrayList)17