Search in sources :

Example 1 with PrimitiveObjectInspector

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

the class GenericUDFTrunc method initializeDate.

private ObjectInspector initializeDate(ObjectInspector[] arguments) throws UDFArgumentLengthException, UDFArgumentTypeException {
    if (arguments.length != 2) {
        throw new UDFArgumentLengthException("trunc() requires 2 argument, got " + arguments.length);
    }
    if (arguments[0].getCategory() != ObjectInspector.Category.PRIMITIVE) {
        throw new UDFArgumentTypeException(0, "Only primitive type arguments are accepted but " + arguments[0].getTypeName() + " is passed. as first arguments");
    }
    if (arguments[1].getCategory() != ObjectInspector.Category.PRIMITIVE) {
        throw new UDFArgumentTypeException(1, "Only primitive type arguments are accepted but " + arguments[1].getTypeName() + " is passed. as second arguments");
    }
    ObjectInspector outputOI = PrimitiveObjectInspectorFactory.writableStringObjectInspector;
    inputType1 = ((PrimitiveObjectInspector) arguments[0]).getPrimitiveCategory();
    switch(inputType1) {
        case STRING:
        case VARCHAR:
        case CHAR:
        case VOID:
            inputType1 = PrimitiveCategory.STRING;
            textConverter1 = ObjectInspectorConverters.getConverter(arguments[0], PrimitiveObjectInspectorFactory.writableStringObjectInspector);
            break;
        case TIMESTAMP:
            timestampConverter = new TimestampConverter((PrimitiveObjectInspector) arguments[0], PrimitiveObjectInspectorFactory.writableTimestampObjectInspector);
            break;
        case DATE:
            dateWritableConverter = ObjectInspectorConverters.getConverter(arguments[0], PrimitiveObjectInspectorFactory.writableDateObjectInspector);
            break;
        default:
            throw new UDFArgumentTypeException(0, "TRUNC() only takes STRING/TIMESTAMP/DATEWRITABLE types as first argument, got " + inputType1);
    }
    inputType2 = ((PrimitiveObjectInspector) arguments[1]).getPrimitiveCategory();
    if (PrimitiveObjectInspectorUtils.getPrimitiveGrouping(inputType2) != PrimitiveGrouping.STRING_GROUP && PrimitiveObjectInspectorUtils.getPrimitiveGrouping(inputType2) != PrimitiveGrouping.VOID_GROUP) {
        throw new UDFArgumentTypeException(1, "trunk() only takes STRING/CHAR/VARCHAR types as second argument, got " + inputType2);
    }
    inputType2 = PrimitiveCategory.STRING;
    if (arguments[1] instanceof ConstantObjectInspector) {
        Object obj = ((ConstantObjectInspector) arguments[1]).getWritableConstantValue();
        fmtInput = obj != null ? obj.toString() : null;
    } else {
        textConverter2 = ObjectInspectorConverters.getConverter(arguments[1], PrimitiveObjectInspectorFactory.writableStringObjectInspector);
    }
    return outputOI;
}
Also used : PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) TimestampConverter(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.TimestampConverter) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)

Example 2 with PrimitiveObjectInspector

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

the class GenericUDFEncode method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 2) {
        throw new UDFArgumentLengthException("Encode() requires exactly two arguments");
    }
    if (arguments[0].getCategory() != Category.PRIMITIVE || PrimitiveGrouping.STRING_GROUP != PrimitiveObjectInspectorUtils.getPrimitiveGrouping(((PrimitiveObjectInspector) arguments[0]).getPrimitiveCategory())) {
        throw new UDFArgumentTypeException(0, "The first argument to Encode() must be a string/varchar");
    }
    stringOI = (PrimitiveObjectInspector) arguments[0];
    if (arguments[1].getCategory() != Category.PRIMITIVE || PrimitiveGrouping.STRING_GROUP != PrimitiveObjectInspectorUtils.getPrimitiveGrouping(((PrimitiveObjectInspector) arguments[1]).getPrimitiveCategory())) {
        throw new UDFArgumentTypeException(1, "The second argument to Encode() must be a string/varchar");
    }
    charsetOI = (PrimitiveObjectInspector) arguments[1];
    // If the character set for encoding is constant, we can optimize that
    if (charsetOI instanceof ConstantObjectInspector) {
        String charSetName = ((ConstantObjectInspector) arguments[1]).getWritableConstantValue().toString();
        encoder = Charset.forName(charSetName).newEncoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
    }
    result = new BytesWritable();
    return (ObjectInspector) PrimitiveObjectInspectorFactory.writableBinaryObjectInspector;
}
Also used : PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) 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) BytesWritable(org.apache.hadoop.io.BytesWritable) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)

Example 3 with PrimitiveObjectInspector

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

the class GenericUDFFormatNumber method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 2) {
        throw new UDFArgumentLengthException("The function FORMAT_NUMBER(X, D or F) needs two arguments.");
    }
    switch(arguments[0].getCategory()) {
        case PRIMITIVE:
            break;
        default:
            throw new UDFArgumentTypeException(0, "Argument 1" + " of function FORMAT_NUMBER must be \"" + serdeConstants.TINYINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.SMALLINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.INT_TYPE_NAME + "\"" + " or \"" + serdeConstants.BIGINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.DOUBLE_TYPE_NAME + "\"" + " or \"" + serdeConstants.FLOAT_TYPE_NAME + "\"" + " or \"" + serdeConstants.DECIMAL_TYPE_NAME + "\", but \"" + arguments[0].getTypeName() + "\" was found.");
    }
    switch(arguments[1].getCategory()) {
        case PRIMITIVE:
            break;
        default:
            throw new UDFArgumentTypeException(1, "Argument 2" + " of function FORMAT_NUMBER must be \"" + serdeConstants.TINYINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.SMALLINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.INT_TYPE_NAME + "\"" + " or \"" + serdeConstants.BIGINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.STRING_TYPE_NAME + "\", but \"" + arguments[1].getTypeName() + "\" was found.");
    }
    PrimitiveObjectInspector xObjectInspector = (PrimitiveObjectInspector) arguments[0];
    PrimitiveObjectInspector dObjectInspector = (PrimitiveObjectInspector) arguments[1];
    switch(xObjectInspector.getPrimitiveCategory()) {
        case VOID:
        case BYTE:
        case SHORT:
        case INT:
        case LONG:
        case DOUBLE:
        case FLOAT:
        case DECIMAL:
            break;
        default:
            throw new UDFArgumentTypeException(0, "Argument 1" + " of function FORMAT_NUMBER must be \"" + serdeConstants.TINYINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.SMALLINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.INT_TYPE_NAME + "\"" + " or \"" + serdeConstants.BIGINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.DOUBLE_TYPE_NAME + "\"" + " or \"" + serdeConstants.FLOAT_TYPE_NAME + "\"" + " or \"" + serdeConstants.DECIMAL_TYPE_NAME + "\", but \"" + arguments[0].getTypeName() + "\" was found.");
    }
    dType = dObjectInspector.getPrimitiveCategory();
    switch(dType) {
        case VOID:
        case BYTE:
        case SHORT:
        case INT:
        case LONG:
            break;
        case STRING:
            if (!(arguments[1] instanceof ConstantObjectInspector)) {
                throw new UDFArgumentTypeException(1, "Format string passed must be a constant STRING." + arguments[1].toString());
            }
            ConstantObjectInspector constantOI = (ConstantObjectInspector) arguments[1];
            String fValue = constantOI.getWritableConstantValue().toString();
            DecimalFormat dFormat = new DecimalFormat(fValue);
            numberFormat.applyPattern(dFormat.toPattern());
            break;
        default:
            throw new UDFArgumentTypeException(1, "Argument 2" + " of function FORMAT_NUMBER must be \"" + serdeConstants.TINYINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.SMALLINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.INT_TYPE_NAME + "\"" + " or \"" + serdeConstants.BIGINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.STRING_TYPE_NAME + "\", but \"" + arguments[1].getTypeName() + "\" was found.");
    }
    argumentOIs = arguments;
    return PrimitiveObjectInspectorFactory.writableStringObjectInspector;
}
Also used : UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) DecimalFormat(java.text.DecimalFormat) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)

Example 4 with PrimitiveObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector 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 5 with PrimitiveObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector 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)

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