Search in sources :

Example 21 with UnionObjectInspector

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

the class TestAvroDeserializer method canDeserializeUnions.

@Test
public void canDeserializeUnions() throws SerDeException, IOException {
    Schema s = AvroSerdeUtils.getSchemaFor(TestAvroObjectInspectorGenerator.UNION_SCHEMA);
    GenericData.Record record = new GenericData.Record(s);
    record.put("aUnion", "this is a string");
    ResultPair result = unionTester(s, record);
    assertTrue(result.value instanceof String);
    assertEquals("this is a string", result.value);
    UnionObjectInspector uoi = (UnionObjectInspector) result.oi;
    assertEquals(1, uoi.getTag(result.unionObject));
    // Now the other enum possibility
    record = new GenericData.Record(s);
    record.put("aUnion", 99);
    result = unionTester(s, record);
    assertTrue(result.value instanceof Integer);
    assertEquals(99, result.value);
    uoi = (UnionObjectInspector) result.oi;
    assertEquals(0, uoi.getTag(result.unionObject));
}
Also used : Schema(org.apache.avro.Schema) GenericData(org.apache.avro.generic.GenericData) UnionObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.UnionObjectInspector) Test(org.junit.Test)

Example 22 with UnionObjectInspector

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

the class TestAvroDeserializer method canDeserializeEvolvedUnions2.

@Test
public void canDeserializeEvolvedUnions2() throws SerDeException, IOException {
    Schema ws = AvroSerdeUtils.getSchemaFor(TestAvroObjectInspectorGenerator.UNION_SCHEMA_3);
    Schema rs = AvroSerdeUtils.getSchemaFor(TestAvroObjectInspectorGenerator.UNION_SCHEMA_4);
    GenericData.Record record = new GenericData.Record(ws);
    record.put("aUnion", 90);
    ResultPair result = unionTester(ws, rs, record);
    assertTrue(result.value instanceof Integer);
    assertEquals(90, result.value);
    UnionObjectInspector uoi = (UnionObjectInspector) result.oi;
    assertEquals(0, uoi.getTag(result.unionObject));
    // Now the other enum possibility
    record = new GenericData.Record(ws);
    record.put("aUnion", 99.9f);
    result = unionTester(ws, rs, record);
    assertTrue(result.value instanceof Float);
    assertEquals(99.9f, result.value);
    uoi = (UnionObjectInspector) result.oi;
    assertEquals(1, uoi.getTag(result.unionObject));
}
Also used : Schema(org.apache.avro.Schema) GenericData(org.apache.avro.generic.GenericData) UnionObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.UnionObjectInspector) Test(org.junit.Test)

Example 23 with UnionObjectInspector

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

the class VectorSerializeRow method serializeUnionWrite.

private void serializeUnionWrite(UnionColumnVector colVector, Field field, int adjustedBatchIndex) throws IOException {
    UnionTypeInfo typeInfo = (UnionTypeInfo) field.typeInfo;
    UnionObjectInspector objectInspector = (UnionObjectInspector) field.objectInspector;
    final byte tag = (byte) colVector.tags[adjustedBatchIndex];
    final ColumnVector fieldColumnVector = colVector.fields[tag];
    final Field childField = field.children[tag];
    serializeWrite.beginUnion(tag);
    serializeWrite(fieldColumnVector, childField, adjustedBatchIndex);
    serializeWrite.finishUnion();
}
Also used : StructField(org.apache.hadoop.hive.serde2.objectinspector.StructField) UnionObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.UnionObjectInspector) UnionTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo)

Example 24 with UnionObjectInspector

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

the class GenericUDFExtractUnion method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length == 1) {
        sourceOI = arguments[0];
        return objectInspectorConverter.convert(sourceOI);
    }
    if (arguments.length == 2 && (arguments[0] instanceof UnionObjectInspector) && (arguments[1] instanceof WritableConstantIntObjectInspector)) {
        tag = ((WritableConstantIntObjectInspector) arguments[1]).getWritableConstantValue().get();
        unionOI = (UnionObjectInspector) arguments[0];
        List<ObjectInspector> fieldOIs = ((UnionObjectInspector) arguments[0]).getObjectInspectors();
        if (tag < 0 || tag >= fieldOIs.size()) {
            throw new UDFArgumentException("int constant must be a valid union tag for " + unionOI.getTypeName() + ". Expected 0-" + (fieldOIs.size() - 1) + " got: " + tag);
        }
        return fieldOIs.get(tag);
    }
    String argumentTypes = "nothing";
    if (arguments.length > 0) {
        List<String> typeNames = new ArrayList<>();
        for (ObjectInspector oi : arguments) {
            typeNames.add(oi.getTypeName());
        }
        argumentTypes = typeNames.toString();
    }
    throw new UDFArgumentException("Unsupported arguments. Expected a type containing a union or a union and an int constant, got: " + argumentTypes);
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) SettableMapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.SettableMapObjectInspector) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) UnionObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.UnionObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) SettableStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.SettableStructObjectInspector) WritableConstantIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantIntObjectInspector) SettableListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.SettableListObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) WritableConstantIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantIntObjectInspector) ArrayList(java.util.ArrayList) UnionObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.UnionObjectInspector)

Example 25 with UnionObjectInspector

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

the class ReduceSinkOperator method initEvaluatorsAndReturnStruct.

/**
 * Initializes array of ExprNodeEvaluator. Adds Union field for distinct
 * column indices for group by.
 * Puts the return values into a StructObjectInspector with output column
 * names.
 *
 * If distinctColIndices is empty, the object inspector is same as
 * {@link Operator#initEvaluatorsAndReturnStruct(ExprNodeEvaluator[], List, ObjectInspector)}
 */
protected static StructObjectInspector initEvaluatorsAndReturnStruct(ExprNodeEvaluator[] evals, List<List<Integer>> distinctColIndices, List<String> outputColNames, int length, ObjectInspector rowInspector) throws HiveException {
    int inspectorLen = evals.length > length ? length + 1 : evals.length;
    List<ObjectInspector> sois = new ArrayList<ObjectInspector>(inspectorLen);
    // keys
    ObjectInspector[] fieldObjectInspectors = initEvaluators(evals, 0, length, rowInspector);
    sois.addAll(Arrays.asList(fieldObjectInspectors));
    if (outputColNames.size() > length) {
        // union keys
        assert distinctColIndices != null;
        List<ObjectInspector> uois = new ArrayList<ObjectInspector>();
        for (List<Integer> distinctCols : distinctColIndices) {
            List<String> names = new ArrayList<String>();
            List<ObjectInspector> eois = new ArrayList<ObjectInspector>();
            int numExprs = 0;
            for (int i : distinctCols) {
                names.add(HiveConf.getColumnInternalName(numExprs));
                eois.add(evals[i].initialize(rowInspector));
                numExprs++;
            }
            uois.add(ObjectInspectorFactory.getStandardStructObjectInspector(names, eois));
        }
        UnionObjectInspector uoi = ObjectInspectorFactory.getStandardUnionObjectInspector(uois);
        sois.add(uoi);
    }
    return ObjectInspectorFactory.getStandardStructObjectInspector(outputColNames, sois);
}
Also used : UnionObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.UnionObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) ArrayList(java.util.ArrayList) UnionObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.UnionObjectInspector)

Aggregations

ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)21 UnionObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.UnionObjectInspector)19 ArrayList (java.util.ArrayList)13 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)13 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)12 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)12 HiveCharObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveCharObjectInspector)12 HiveVarcharObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveVarcharObjectInspector)12 List (java.util.List)11 BinaryObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector)11 ByteObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector)11 DateObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector)11 DoubleObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.DoubleObjectInspector)11 FloatObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector)11 HiveDecimalObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector)11 IntObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector)11 LongObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector)11 ShortObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.ShortObjectInspector)11 StringObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector)11 TimestampObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector)11