Search in sources :

Example 66 with UDFArgumentException

use of org.apache.hadoop.hive.ql.exec.UDFArgumentException in project hive by apache.

the class GenericUDFBridge method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    try {
        udf = getUdfClassInternal().newInstance();
    } catch (Exception e) {
        throw new UDFArgumentException("Unable to instantiate UDF implementation class " + udfClassName + ": " + e);
    }
    // Resolve for the method based on argument types
    ArrayList<TypeInfo> argumentTypeInfos = new ArrayList<TypeInfo>(arguments.length);
    for (ObjectInspector argument : arguments) {
        argumentTypeInfos.add(TypeInfoUtils.getTypeInfoFromObjectInspector(argument));
    }
    udfMethod = udf.getResolver().getEvalMethod(argumentTypeInfos);
    udfMethod.setAccessible(true);
    // Create parameter converters
    conversionHelper = new ConversionHelper(udfMethod, arguments);
    // Create the non-deferred realArgument
    realArguments = new Object[arguments.length];
    // Get the return ObjectInspector.
    ObjectInspector returnOI = ObjectInspectorFactory.getReflectionObjectInspector(udfMethod.getGenericReturnType(), ObjectInspectorOptions.JAVA);
    return returnOI;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) ArrayList(java.util.ArrayList) ConversionHelper(org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils.ConversionHelper) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException)

Example 67 with UDFArgumentException

use of org.apache.hadoop.hive.ql.exec.UDFArgumentException in project hive by apache.

the class GenericUDFExtractUnion method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length == 1) {
        sourceOI = arguments[0];
        return objectInspectorConverter.convert(sourceOI);
    }
    if (arguments.length == 2 && (arguments[0] instanceof UnionObjectInspector) && (arguments[1] instanceof WritableConstantIntObjectInspector)) {
        tag = ((WritableConstantIntObjectInspector) arguments[1]).getWritableConstantValue().get();
        unionOI = (UnionObjectInspector) arguments[0];
        List<ObjectInspector> fieldOIs = ((UnionObjectInspector) arguments[0]).getObjectInspectors();
        if (tag < 0 || tag >= fieldOIs.size()) {
            throw new UDFArgumentException("int constant must be a valid union tag for " + unionOI.getTypeName() + ". Expected 0-" + (fieldOIs.size() - 1) + " got: " + tag);
        }
        return fieldOIs.get(tag);
    }
    String argumentTypes = "nothing";
    if (arguments.length > 0) {
        List<String> typeNames = new ArrayList<>();
        for (ObjectInspector oi : arguments) {
            typeNames.add(oi.getTypeName());
        }
        argumentTypes = typeNames.toString();
    }
    throw new UDFArgumentException("Unsupported arguments. Expected a type containing a union or a union and an int constant, got: " + argumentTypes);
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) SettableMapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.SettableMapObjectInspector) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) UnionObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.UnionObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) SettableStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.SettableStructObjectInspector) WritableConstantIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantIntObjectInspector) SettableListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.SettableListObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) WritableConstantIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantIntObjectInspector) ArrayList(java.util.ArrayList) UnionObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.UnionObjectInspector)

Example 68 with UDFArgumentException

use of org.apache.hadoop.hive.ql.exec.UDFArgumentException in project hive by apache.

the class GenericUDFFloorCeilBase method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 1) {
        throw new UDFArgumentException(opName + " requires one argument.");
    }
    Category category = arguments[0].getCategory();
    if (category != Category.PRIMITIVE) {
        throw new UDFArgumentTypeException(0, "The " + GenericUDFUtils.getOrdinal(1) + " argument of " + opName + "  is expected to a " + Category.PRIMITIVE.toString().toLowerCase() + " type, but " + category.toString().toLowerCase() + " is found");
    }
    inputOI = (PrimitiveObjectInspector) arguments[0];
    if (!FunctionRegistry.isNumericType(inputOI.getTypeInfo())) {
        throw new UDFArgumentTypeException(0, "The " + GenericUDFUtils.getOrdinal(1) + " argument of " + opName + "  is expected to a " + "numeric type, but " + inputOI.getTypeName() + " is found");
    }
    PrimitiveTypeInfo resultTypeInfo = null;
    PrimitiveTypeInfo inputTypeInfo = inputOI.getTypeInfo();
    if (inputTypeInfo instanceof DecimalTypeInfo) {
        DecimalTypeInfo decTypeInfo = (DecimalTypeInfo) inputTypeInfo;
        resultTypeInfo = TypeInfoFactory.getDecimalTypeInfo(decTypeInfo.precision() - decTypeInfo.scale() + 1, 0);
        ObjectInspector decimalOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(decTypeInfo);
        converter = ObjectInspectorConverters.getConverter(inputOI, decimalOI);
    } else {
        resultTypeInfo = TypeInfoFactory.longTypeInfo;
        ObjectInspector doubleObjectInspector = PrimitiveObjectInspectorFactory.writableDoubleObjectInspector;
        converter = ObjectInspectorConverters.getConverter(inputOI, doubleObjectInspector);
    }
    return resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(resultTypeInfo);
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) Category(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)

Example 69 with UDFArgumentException

use of org.apache.hadoop.hive.ql.exec.UDFArgumentException 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 70 with UDFArgumentException

use of org.apache.hadoop.hive.ql.exec.UDFArgumentException in project hive by apache.

the class GenericUDFLength method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 1) {
        throw new UDFArgumentLengthException("LENGTH requires 1 argument, got " + arguments.length);
    }
    if (arguments[0].getCategory() != ObjectInspector.Category.PRIMITIVE) {
        throw new UDFArgumentException("LENGTH only takes primitive types, got " + argumentOI.getTypeName());
    }
    argumentOI = (PrimitiveObjectInspector) arguments[0];
    PrimitiveObjectInspector.PrimitiveCategory inputType = argumentOI.getPrimitiveCategory();
    ObjectInspector outputOI = null;
    switch(inputType) {
        case CHAR:
        case VARCHAR:
        case STRING:
            isInputString = true;
            stringConverter = new PrimitiveObjectInspectorConverter.StringConverter(argumentOI);
            break;
        case BINARY:
            isInputString = false;
            binaryConverter = new PrimitiveObjectInspectorConverter.BinaryConverter(argumentOI, PrimitiveObjectInspectorFactory.writableBinaryObjectInspector);
            break;
        default:
            throw new UDFArgumentException(" LENGTH() only takes STRING/CHAR/VARCHAR/BINARY types as first argument, got " + inputType);
    }
    outputOI = PrimitiveObjectInspectorFactory.writableIntObjectInspector;
    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) UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) PrimitiveObjectInspectorConverter(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter)

Aggregations

UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)72 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)31 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)27 UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)24 UDFArgumentTypeException (org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException)18 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)11 ArrayList (java.util.ArrayList)9 Category (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category)7 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)7 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)6 ConstantObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)6 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)6 Test (org.junit.Test)6 PrimitiveObjectInspectorConverter (org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter)5 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)5 StringObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector)4 HiveConf (org.apache.hadoop.hive.conf.HiveConf)3 DateWritableV2 (org.apache.hadoop.hive.serde2.io.DateWritableV2)3 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)3 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)3