Search in sources :

Example 21 with UDFArgumentLengthException

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

the class GenericUDFFromUtcTimestamp method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 2) {
        throw new UDFArgumentLengthException("The function " + getName() + " requires two " + "argument, got " + arguments.length);
    }
    try {
        argumentOIs = new PrimitiveObjectInspector[2];
        argumentOIs[0] = (PrimitiveObjectInspector) arguments[0];
        argumentOIs[1] = (PrimitiveObjectInspector) arguments[1];
    } catch (ClassCastException e) {
        throw new UDFArgumentException("The function " + getName() + " takes only primitive types");
    }
    timestampConverter = new TimestampConverter(argumentOIs[0], PrimitiveObjectInspectorFactory.writableTimestampObjectInspector);
    textConverter = new TextConverter(argumentOIs[1]);
    return PrimitiveObjectInspectorFactory.javaTimestampObjectInspector;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) TimestampConverter(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.TimestampConverter) TextConverter(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.TextConverter)

Example 22 with UDFArgumentLengthException

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

the class GenericUDFIf method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    argumentOIs = arguments;
    returnOIResolver = new GenericUDFUtils.ReturnObjectInspectorResolver(true);
    if (arguments.length != 3) {
        throw new UDFArgumentLengthException("The function IF(expr1,expr2,expr3) accepts exactly 3 arguments.");
    }
    boolean conditionTypeIsOk = (arguments[0].getCategory() == ObjectInspector.Category.PRIMITIVE);
    if (conditionTypeIsOk) {
        PrimitiveObjectInspector poi = ((PrimitiveObjectInspector) arguments[0]);
        conditionTypeIsOk = (poi.getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.BOOLEAN || poi.getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.VOID);
    }
    if (!conditionTypeIsOk) {
        throw new UDFArgumentTypeException(0, "The first argument of function IF should be \"" + serdeConstants.BOOLEAN_TYPE_NAME + "\", but \"" + arguments[0].getTypeName() + "\" is found");
    }
    if (!(returnOIResolver.update(arguments[1]) && returnOIResolver.update(arguments[2]))) {
        throw new UDFArgumentTypeException(2, "The second and the third arguments of function IF should have the same type, " + "but they are different: \"" + arguments[1].getTypeName() + "\" and \"" + arguments[2].getTypeName() + "\"");
    }
    return returnOIResolver.get();
}
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)

Example 23 with UDFArgumentLengthException

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

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

the class GenericUDFSortArrayByField method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    GenericUDFUtils.ReturnObjectInspectorResolver returnOIResolver;
    returnOIResolver = new GenericUDFUtils.ReturnObjectInspectorResolver(true);
    /**
     *This UDF requires minimum 2 arguments array_name,field name
     */
    if (arguments.length < 2) {
        throw new UDFArgumentLengthException("SORT_ARRAY_BY requires minimum 2 arguments, got " + arguments.length);
    }
    /**
     *First argument must be array
     */
    switch(arguments[0].getCategory()) {
        case LIST:
            listObjectInspector = (ListObjectInspector) arguments[0];
            break;
        default:
            throw new UDFArgumentTypeException(0, "Argument 1 of function SORT_ARRAY_BY must be " + serdeConstants.LIST_TYPE_NAME + ", but " + arguments[0].getTypeName() + " was found.");
    }
    /**
     *Elements inside first argument(array) must be tuple(s)
     */
    switch(listObjectInspector.getListElementObjectInspector().getCategory()) {
        case STRUCT:
            structObjectInspector = (StructObjectInspector) listObjectInspector.getListElementObjectInspector();
            break;
        default:
            throw new UDFArgumentTypeException(0, "Element[s] of first argument array in function SORT_ARRAY_BY must be " + serdeConstants.STRUCT_TYPE_NAME + ", but " + listObjectInspector.getTypeName() + " was found.");
    }
    /**
     *All sort fields argument name and sort order name must be in String type
     */
    converters = new Converter[arguments.length];
    inputTypes = new PrimitiveCategory[arguments.length];
    fields = new StructField[arguments.length - 1];
    noOfInputFields = arguments.length - 1;
    for (int i = 1; i < arguments.length; i++) {
        checkArgPrimitive(arguments, i);
        checkArgGroups(arguments, i, inputTypes, PrimitiveGrouping.STRING_GROUP);
        if (arguments[i] instanceof ConstantObjectInspector) {
            String fieldName = getConstantStringValue(arguments, i);
            /**
             *checking whether any sorting order (ASC,DESC) has specified in last argument
             */
            if (i != 1 && (i == arguments.length - 1) && (fieldName.trim().toUpperCase().equals(SORT_ORDER_TYPE.ASC.name()) || fieldName.trim().toUpperCase().equals(SORT_ORDER_TYPE.DESC.name()))) {
                sortOrder = SORT_ORDER_TYPE.valueOf(fieldName.trim().toUpperCase());
                noOfInputFields -= 1;
                continue;
            }
            fields[i - 1] = structObjectInspector.getStructFieldRef(getConstantStringValue(arguments, i));
        }
        obtainStringConverter(arguments, i, inputTypes, converters);
    }
    ObjectInspector returnOI = returnOIResolver.get(structObjectInspector);
    converters[0] = ObjectInspectorConverters.getConverter(structObjectInspector, returnOI);
    return ObjectInspectorFactory.getStandardListObjectInspector(structObjectInspector);
}
Also used : ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)

Example 25 with UDFArgumentLengthException

use of org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException 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;
        case TIMESTAMPLOCALTZ:
            inputTimestampLocalTzOI = (TimestampLocalTZObjectInspector) arguments[0];
            break;
        default:
            throw new UDFArgumentException("The function " + getName().toUpperCase() + " takes only string/date/timestamp/timestampwltz types. Got Type:" + arg1OI.getPrimitiveCategory().name());
    }
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) TimestampLocalTZObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampLocalTZObjectInspector) 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)

Aggregations

UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)47 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)29 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)26 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)21 UDFArgumentTypeException (org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException)21 ConstantObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)13 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)6 PrimitiveObjectInspectorConverter (org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter)5 TimestampConverter (org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.TimestampConverter)5 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)4 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)4 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)4 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)3 StringObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector)3 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 DateWritable (org.apache.hadoop.hive.serde2.io.DateWritable)2 ObjectInspectorConverters (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters)2 HiveDecimalObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector)2 StringConverter (org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.StringConverter)2