Search in sources :

Example 1 with ObjectInspector

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

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

the class GenericUDTFGetSplits method initialize.

@Override
public StructObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    LOG.debug("initializing GenericUDFGetSplits");
    if (SessionState.get() == null || SessionState.get().getConf() == null) {
        throw new IllegalStateException("Cannot run get splits outside HS2");
    }
    LOG.debug("Initialized conf, jc and metastore connection");
    if (arguments.length != 2) {
        throw new UDFArgumentLengthException("The function GET_SPLITS accepts 2 arguments.");
    } else if (!(arguments[0] instanceof StringObjectInspector)) {
        LOG.error("Got " + arguments[0].getTypeName() + " instead of string.");
        throw new UDFArgumentTypeException(0, "\"" + "string\" is expected at function GET_SPLITS, " + "but \"" + arguments[0].getTypeName() + "\" is found");
    } else if (!(arguments[1] instanceof IntObjectInspector)) {
        LOG.error("Got " + arguments[1].getTypeName() + " instead of int.");
        throw new UDFArgumentTypeException(1, "\"" + "int\" is expected at function GET_SPLITS, " + "but \"" + arguments[1].getTypeName() + "\" is found");
    }
    stringOI = (StringObjectInspector) arguments[0];
    intOI = (IntObjectInspector) arguments[1];
    List<String> names = Arrays.asList("split");
    List<ObjectInspector> fieldOIs = Arrays.<ObjectInspector>asList(PrimitiveObjectInspectorFactory.javaByteArrayObjectInspector);
    StructObjectInspector outputOI = ObjectInspectorFactory.getStandardStructObjectInspector(names, fieldOIs);
    LOG.debug("done initializing GenericUDFGetSplits");
    return outputOI;
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) IntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector) IntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector) UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)

Example 3 with ObjectInspector

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

the class GenericUDTFStack method initialize.

@Override
public StructObjectInspector initialize(ObjectInspector[] args) throws UDFArgumentException {
    if (args.length < 2) {
        throw new UDFArgumentException("STACK() expects at least two arguments.");
    }
    if (!(args[0] instanceof ConstantObjectInspector)) {
        throw new UDFArgumentException("The first argument to STACK() must be a constant integer (got " + args[0].getTypeName() + " instead).");
    }
    numRows = (IntWritable) ((ConstantObjectInspector) args[0]).getWritableConstantValue();
    if (numRows == null || numRows.get() < 1) {
        throw new UDFArgumentException("STACK() expects its first argument to be >= 1.");
    }
    // Divide and round up.
    numCols = (args.length - 1 + numRows.get() - 1) / numRows.get();
    for (int jj = 0; jj < numCols; ++jj) {
        returnOIResolvers.add(new ReturnObjectInspectorResolver());
        for (int ii = 0; ii < numRows.get(); ++ii) {
            int index = ii * numCols + jj + 1;
            if (index < args.length && !returnOIResolvers.get(jj).update(args[index])) {
                throw new UDFArgumentException("Argument " + (jj + 1) + "'s type (" + args[jj + 1].getTypeName() + ") should be equal to argument " + index + "'s type (" + args[index].getTypeName() + ")");
            }
        }
    }
    forwardObj = new Object[numCols];
    for (int ii = 0; ii < args.length; ++ii) {
        argOIs.add(args[ii]);
    }
    ArrayList<String> fieldNames = new ArrayList<String>();
    ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();
    for (int ii = 0; ii < numCols; ++ii) {
        fieldNames.add("col" + ii);
        fieldOIs.add(returnOIResolvers.get(ii).get());
    }
    return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs);
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) WritableConstantIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantIntObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) ReturnObjectInspectorResolver(org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils.ReturnObjectInspectorResolver) ArrayList(java.util.ArrayList) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)

Example 4 with ObjectInspector

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

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

Aggregations

ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)700 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)299 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)261 Test (org.junit.Test)241 DeferredJavaObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject)163 ArrayList (java.util.ArrayList)159 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)159 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)107 Text (org.apache.hadoop.io.Text)103 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)89 StringObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector)75 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)73 StructField (org.apache.hadoop.hive.serde2.objectinspector.StructField)66 IntWritable (org.apache.hadoop.io.IntWritable)65 LongObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector)58 LongWritable (org.apache.hadoop.io.LongWritable)54 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)53 IntObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector)53 DoubleObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.DoubleObjectInspector)51 BytesWritable (org.apache.hadoop.io.BytesWritable)51