Search in sources :

Example 11 with PrimitiveObjectInspector

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

the class GenericUDFBaseNumeric method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 2) {
        throw new UDFArgumentException(opName + " requires two arguments.");
    }
    for (int i = 0; i < 2; i++) {
        Category category = arguments[i].getCategory();
        if (category != Category.PRIMITIVE) {
            throw new UDFArgumentTypeException(i, "The " + GenericUDFUtils.getOrdinal(i + 1) + " argument of " + opName + "  is expected to a " + Category.PRIMITIVE.toString().toLowerCase() + " type, but " + category.toString().toLowerCase() + " is found");
        }
    }
    // we have access to these values in the map/reduce tasks.
    if (confLookupNeeded) {
        CompatLevel compatLevel = HiveCompat.getCompatLevel(SessionState.get().getConf());
        ansiSqlArithmetic = compatLevel.ordinal() > CompatLevel.HIVE_0_12.ordinal();
        confLookupNeeded = false;
    }
    leftOI = (PrimitiveObjectInspector) arguments[0];
    rightOI = (PrimitiveObjectInspector) arguments[1];
    resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(deriveResultTypeInfo());
    converterLeft = ObjectInspectorConverters.getConverter(leftOI, resultOI);
    converterRight = ObjectInspectorConverters.getConverter(rightOI, resultOI);
    return resultOI;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory) Category(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category) CompatLevel(org.apache.hive.common.HiveCompat.CompatLevel) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException)

Example 12 with PrimitiveObjectInspector

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

the class GenericUDAFLeadLag method getEvaluator.

@Override
public GenericUDAFEvaluator getEvaluator(GenericUDAFParameterInfo parameters) throws SemanticException {
    ObjectInspector[] paramOIs = parameters.getParameterObjectInspectors();
    String fNm = functionName();
    if (!(paramOIs.length >= 1 && paramOIs.length <= 3)) {
        throw new UDFArgumentTypeException(paramOIs.length - 1, "Incorrect invocation of " + fNm + ": _FUNC_(expr, amt, default)");
    }
    int amt = 1;
    if (paramOIs.length > 1) {
        ObjectInspector amtOI = paramOIs[1];
        if (!ObjectInspectorUtils.isConstantObjectInspector(amtOI) || (amtOI.getCategory() != ObjectInspector.Category.PRIMITIVE) || ((PrimitiveObjectInspector) amtOI).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.INT) {
            throw new UDFArgumentTypeException(1, fNm + " amount must be a integer value " + amtOI.getTypeName() + " was passed as parameter 1.");
        }
        Object o = ((ConstantObjectInspector) amtOI).getWritableConstantValue();
        amt = ((IntWritable) o).get();
        if (amt < 0) {
            throw new UDFArgumentTypeException(1, fNm + " amount can not be nagative. Specified: " + amt);
        }
    }
    if (paramOIs.length == 3) {
        ObjectInspectorConverters.getConverter(paramOIs[2], paramOIs[0]);
    }
    GenericUDAFLeadLagEvaluator eval = createLLEvaluator();
    eval.setAmt(amt);
    return eval;
}
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) 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 13 with PrimitiveObjectInspector

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

the class GenericUDF method obtainDoubleConverter.

protected void obtainDoubleConverter(ObjectInspector[] arguments, int i, PrimitiveCategory[] inputTypes, Converter[] converters) throws UDFArgumentTypeException {
    PrimitiveObjectInspector inOi = (PrimitiveObjectInspector) arguments[i];
    PrimitiveCategory inputType = inOi.getPrimitiveCategory();
    Converter converter = ObjectInspectorConverters.getConverter(arguments[i], PrimitiveObjectInspectorFactory.writableDoubleObjectInspector);
    converters[i] = converter;
    inputTypes[i] = inputType;
}
Also used : Converter(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)

Example 14 with PrimitiveObjectInspector

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

the class GenericUDFCharacterLength method initialize.

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

Example 15 with PrimitiveObjectInspector

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

the class HCatRecordSerDe method serializeList.

private static List<?> serializeList(Object f, ListObjectInspector loi) throws SerDeException {
    List l = loi.getList(f);
    if (l == null) {
        return null;
    }
    ObjectInspector eloi = loi.getListElementObjectInspector();
    if (eloi.getCategory() == Category.PRIMITIVE) {
        List<Object> list = new ArrayList<Object>(l.size());
        for (int i = 0; i < l.size(); i++) {
            list.add(((PrimitiveObjectInspector) eloi).getPrimitiveJavaObject(l.get(i)));
        }
        return list;
    } else if (eloi.getCategory() == Category.STRUCT) {
        List<List<?>> list = new ArrayList<List<?>>(l.size());
        for (int i = 0; i < l.size(); i++) {
            list.add(serializeStruct(l.get(i), (StructObjectInspector) eloi));
        }
        return list;
    } else if (eloi.getCategory() == Category.LIST) {
        List<List<?>> list = new ArrayList<List<?>>(l.size());
        for (int i = 0; i < l.size(); i++) {
            list.add(serializeList(l.get(i), (ListObjectInspector) eloi));
        }
        return list;
    } else if (eloi.getCategory() == Category.MAP) {
        List<Map<?, ?>> list = new ArrayList<Map<?, ?>>(l.size());
        for (int i = 0; i < l.size(); i++) {
            list.add(serializeMap(l.get(i), (MapObjectInspector) eloi));
        }
        return list;
    } else {
        throw new SerDeException(HCatRecordSerDe.class.toString() + " does not know what to do with fields of unknown category: " + eloi.getCategory() + " , type: " + eloi.getTypeName());
    }
}
Also used : ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) SerDeException(org.apache.hadoop.hive.serde2.SerDeException)

Aggregations

PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)252 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)177 Test (org.junit.Test)122 DeferredJavaObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject)111 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)109 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)31 UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)30 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)30 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)30 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)29 Text (org.apache.hadoop.io.Text)28 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)26 LongObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector)26 ArrayList (java.util.ArrayList)22 StringObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector)22 ConstantObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)21 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)20