Search in sources :

Example 86 with PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector in project hive by apache.

the class TestGenericUDFRound method testDecimalRoundingMetaData.

@Test
public void testDecimalRoundingMetaData() throws UDFArgumentException {
    GenericUDFRound udf = new GenericUDFRound();
    ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.getDecimalTypeInfo(7, 3)), PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(TypeInfoFactory.intTypeInfo, new IntWritable(2)) };
    PrimitiveObjectInspector outputOI = (PrimitiveObjectInspector) udf.initialize(inputOIs);
    DecimalTypeInfo outputTypeInfo = (DecimalTypeInfo) outputOI.getTypeInfo();
    Assert.assertEquals(TypeInfoFactory.getDecimalTypeInfo(7, 2), outputTypeInfo);
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) IntWritable(org.apache.hadoop.io.IntWritable) Test(org.junit.Test) BaseScalarUdfTest(org.apache.hadoop.hive.ql.testutil.BaseScalarUdfTest)

Example 87 with PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector in project cdap by caskdata.

the class ObjectInspectorFactory method getReflectionObjectInspectorNoCache.

private static ObjectInspector getReflectionObjectInspectorNoCache(Type t) {
    if (t instanceof GenericArrayType) {
        GenericArrayType at = (GenericArrayType) t;
        return getStandardListObjectInspector(getReflectionObjectInspector(at.getGenericComponentType()));
    }
    Map<TypeVariable, Type> genericTypes = null;
    if (t instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) t;
        Type rawType = pt.getRawType();
        // Collection?
        if (Collection.class.isAssignableFrom((Class<?>) rawType)) {
            return getStandardListObjectInspector(getReflectionObjectInspector(pt.getActualTypeArguments()[0]));
        }
        // Map?
        if (Map.class.isAssignableFrom((Class<?>) rawType)) {
            return getStandardMapObjectInspector(getReflectionObjectInspector(pt.getActualTypeArguments()[0]), getReflectionObjectInspector(pt.getActualTypeArguments()[1]));
        }
        // Otherwise convert t to RawType so we will fall into the following if block.
        t = rawType;
        ImmutableMap.Builder<TypeVariable, Type> builder = ImmutableMap.builder();
        for (int i = 0; i < pt.getActualTypeArguments().length; i++) {
            builder.put(((Class<?>) t).getTypeParameters()[i], pt.getActualTypeArguments()[i]);
        }
        genericTypes = builder.build();
    }
    // Must be a class.
    if (!(t instanceof Class)) {
        throw new RuntimeException(ObjectInspectorFactory.class.getName() + " internal error:" + t);
    }
    Class<?> c = (Class<?>) t;
    // Java Primitive Type?
    if (PrimitiveObjectInspectorUtils.isPrimitiveJavaType(c)) {
        return PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspectorUtils.getTypeEntryFromPrimitiveJavaType(c).primitiveCategory);
    }
    // Java Primitive Class?
    if (PrimitiveObjectInspectorUtils.isPrimitiveJavaClass(c)) {
        return PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspectorUtils.getTypeEntryFromPrimitiveJavaClass(c).primitiveCategory);
    }
    // Primitive Writable class?
    if (PrimitiveObjectInspectorUtils.isPrimitiveWritableClass(c)) {
        return PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(PrimitiveObjectInspectorUtils.getTypeEntryFromPrimitiveWritableClass(c).primitiveCategory);
    }
    // Enum class?
    if (Enum.class.isAssignableFrom(c)) {
        return PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(PrimitiveObjectInspector.PrimitiveCategory.STRING);
    }
    // Array
    if (c.isArray()) {
        return getStandardListObjectInspector(getReflectionObjectInspector(c.getComponentType()));
    }
    // Must be struct because List and Map need to be ParameterizedType
    Preconditions.checkState(!List.class.isAssignableFrom(c));
    Preconditions.checkState(!Map.class.isAssignableFrom(c));
    Preconditions.checkState(!c.isInterface(), "Cannot inspect an interface.");
    ReflectionStructObjectInspector oi = new ReflectionStructObjectInspector();
    // put it into the cache BEFORE it is initialized to make sure we can catch
    // recursive types.
    objectInspectorCache.put(t, oi);
    Field[] fields = ObjectInspectorUtils.getDeclaredNonStaticFields(c);
    List<ObjectInspector> structFieldObjectInspectors = new ArrayList<>(fields.length);
    for (Field field : fields) {
        // "this" pointer present in nested classes and that references the parent.
        if (Modifier.isTransient(field.getModifiers()) || field.isSynthetic()) {
            continue;
        }
        if (!oi.shouldIgnoreField(field.getName())) {
            Type newType = field.getGenericType();
            if (newType instanceof TypeVariable) {
                Preconditions.checkNotNull(genericTypes, "Type was not recognized as a parameterized type.");
                Preconditions.checkNotNull(genericTypes.get(newType), "Generic type " + newType + " not a parameter of class " + c);
                newType = genericTypes.get(newType);
            }
            structFieldObjectInspectors.add(getReflectionObjectInspector(newType));
        }
    }
    oi.init(c, structFieldObjectInspectors);
    return oi;
}
Also used : StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) ArrayList(java.util.ArrayList) GenericArrayType(java.lang.reflect.GenericArrayType) ImmutableMap(com.google.common.collect.ImmutableMap) ParameterizedType(java.lang.reflect.ParameterizedType) Field(java.lang.reflect.Field) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) PrimitiveObjectInspectorFactory(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory)

Example 88 with PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector in project hive by apache.

the class TestDruidSerDe method serializeObject.

private static void serializeObject(Properties properties, DruidSerDe serDe, Object[] rowObject, DruidWritable druidWritable) throws SerDeException {
    // Build OI with timestamp granularity column
    final List<String> columnNames = new ArrayList<>();
    final List<PrimitiveTypeInfo> columnTypes = new ArrayList<>();
    List<ObjectInspector> inspectors = new ArrayList<>();
    columnNames.addAll(Utilities.getColumnNames(properties));
    columnNames.add(Constants.DRUID_TIMESTAMP_GRANULARITY_COL_NAME);
    columnTypes.addAll(Lists.transform(Utilities.getColumnTypes(properties), new Function<String, PrimitiveTypeInfo>() {

        @Override
        public PrimitiveTypeInfo apply(String type) {
            return TypeInfoFactory.getPrimitiveTypeInfo(type);
        }
    }));
    columnTypes.add(TypeInfoFactory.getPrimitiveTypeInfo("timestamp"));
    inspectors.addAll(Lists.transform(columnTypes, new Function<PrimitiveTypeInfo, ObjectInspector>() {

        @Override
        public ObjectInspector apply(PrimitiveTypeInfo type) {
            return PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(type);
        }
    }));
    ObjectInspector inspector = ObjectInspectorFactory.getStandardStructObjectInspector(columnNames, inspectors);
    // Serialize
    DruidWritable writable = (DruidWritable) serDe.serialize(rowObject, inspector);
    // Check result
    assertEquals(druidWritable.getValue().size(), writable.getValue().size());
    for (Entry<String, Object> e : druidWritable.getValue().entrySet()) {
        assertEquals(e.getValue(), writable.getValue().get(e.getKey()));
    }
}
Also used : Function(com.google.common.base.Function) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) ArrayList(java.util.ArrayList) Matchers.anyObject(org.mockito.Matchers.anyObject) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)

Example 89 with PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector in project hive by apache.

the class VectorRandomRowSource method randomWritablePrimitiveRow.

public static Object[] randomWritablePrimitiveRow(int columnCount, Random r, PrimitiveTypeInfo[] primitiveTypeInfos) {
    final Object[] row = new Object[columnCount];
    for (int c = 0; c < columnCount; c++) {
        final PrimitiveTypeInfo primitiveTypeInfo = primitiveTypeInfos[c];
        final ObjectInspector objectInspector = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(primitiveTypeInfo);
        final Object object = randomPrimitiveObject(r, primitiveTypeInfo);
        row[c] = getWritablePrimitiveObject(primitiveTypeInfo, objectInspector, object);
    }
    return row;
}
Also used : WritableIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableIntObjectInspector) WritableByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableByteObjectInspector) UnionObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.UnionObjectInspector) StandardStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StandardStructObjectInspector) WritableDoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableDoubleObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) StandardListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StandardListObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) WritableStringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableStringObjectInspector) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) WritableHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableHiveCharObjectInspector) WritableHiveVarcharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableHiveVarcharObjectInspector) WritableBooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableBooleanObjectInspector) WritableTimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableTimestampObjectInspector) WritableHiveIntervalDayTimeObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableHiveIntervalDayTimeObjectInspector) WritableShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableShortObjectInspector) StandardMapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StandardMapObjectInspector) WritableHiveIntervalYearMonthObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableHiveIntervalYearMonthObjectInspector) WritableFloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableFloatObjectInspector) WritableLongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableLongObjectInspector) StandardUnionObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StandardUnionObjectInspector) WritableDateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableDateObjectInspector) WritableHiveDecimalObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableHiveDecimalObjectInspector) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)

Example 90 with PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector

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

Aggregations

ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)77 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)63 Test (org.junit.Test)45 DeferredJavaObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject)35 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)35 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)24 ArrayList (java.util.ArrayList)20 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)20 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)18 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)17 DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)14 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)13 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)12 WritableHiveCharObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableHiveCharObjectInspector)9 WritableHiveDecimalObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableHiveDecimalObjectInspector)9 WritableHiveVarcharObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableHiveVarcharObjectInspector)9 WritableStringObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableStringObjectInspector)9 VarcharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo)9 StandardStructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StandardStructObjectInspector)8 WritableBooleanObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableBooleanObjectInspector)8