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));
}
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));
}
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();
}
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);
}
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);
}
Aggregations