use of org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo in project hive by apache.
the class TestTypeInfoToSchema method createAvroUnionSchemaOfNull.
@Test
public void createAvroUnionSchemaOfNull() {
UnionTypeInfo unionTypeInfo = new UnionTypeInfo();
unionTypeInfo.setAllUnionObjectTypeInfos(Arrays.asList(VOID));
final String specificSchema = Schema.createUnion(Arrays.asList(Schema.create(Schema.Type.NULL))).toString();
String expectedSchema = genSchemaWithoutNull(specificSchema);
Assert.assertEquals("Test for union's avro schema failed", expectedSchema, getAvroSchemaString(unionTypeInfo));
}
use of org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo in project hive by apache.
the class TestAvroObjectInspectorGenerator method canHandleUnions.
@Test
public void canHandleUnions() throws SerDeException {
Schema s = AvroSerdeUtils.getSchemaFor(UNION_SCHEMA);
AvroObjectInspectorGenerator aoig = new AvroObjectInspectorGenerator(s);
// Column names
assertEquals(1, aoig.getColumnNames().size());
assertEquals("aUnion", aoig.getColumnNames().get(0));
// Column types
assertEquals(1, aoig.getColumnTypes().size());
TypeInfo typeInfo = aoig.getColumnTypes().get(0);
assertTrue(typeInfo instanceof UnionTypeInfo);
UnionTypeInfo uti = (UnionTypeInfo) typeInfo;
// Check that the union has come out unscathed. No scathing of unions allowed.
List<TypeInfo> typeInfos = uti.getAllUnionObjectTypeInfos();
assertEquals(2, typeInfos.size());
assertEquals(INT, typeInfos.get(0));
assertEquals(STRING, typeInfos.get(1));
assertEquals("uniontype<int,string>", uti.getTypeName());
}
use of org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo in project hive by apache.
the class LazyBinaryDeserializeRead method createField.
private Field createField(TypeInfo typeInfo) {
final Field field = new Field();
final Category category = typeInfo.getCategory();
field.category = category;
field.typeInfo = typeInfo;
switch(category) {
case PRIMITIVE:
field.primitiveCategory = ((PrimitiveTypeInfo) typeInfo).getPrimitiveCategory();
break;
case LIST:
field.children = new Field[1];
field.children[0] = createField(((ListTypeInfo) typeInfo).getListElementTypeInfo());
break;
case MAP:
field.children = new Field[2];
field.children[0] = createField(((MapTypeInfo) typeInfo).getMapKeyTypeInfo());
field.children[1] = createField(((MapTypeInfo) typeInfo).getMapValueTypeInfo());
break;
case STRUCT:
final StructTypeInfo structTypeInfo = (StructTypeInfo) typeInfo;
final List<TypeInfo> fieldTypeInfos = structTypeInfo.getAllStructFieldTypeInfos();
field.children = createFields(fieldTypeInfos.toArray(new TypeInfo[fieldTypeInfos.size()]));
break;
case UNION:
final UnionTypeInfo unionTypeInfo = (UnionTypeInfo) typeInfo;
final List<TypeInfo> objectTypeInfos = unionTypeInfo.getAllUnionObjectTypeInfos();
field.children = createFields(objectTypeInfos.toArray(new TypeInfo[objectTypeInfos.size()]));
break;
default:
throw new RuntimeException();
}
return field;
}
use of org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo in project incubator-gobblin by apache.
the class HiveAvroORCQueryGenerator method escapeHiveType.
/**
* Escape the Hive nested field names.
* @param type Primitive or nested Hive type.
* @return Escaped Hive nested field.
*/
public static String escapeHiveType(String type) {
TypeInfo typeInfo = TypeInfoUtils.getTypeInfoFromTypeString(type);
// Primitive
if (ObjectInspector.Category.PRIMITIVE.equals(typeInfo.getCategory())) {
return type;
} else // List
if (ObjectInspector.Category.LIST.equals(typeInfo.getCategory())) {
ListTypeInfo listTypeInfo = (ListTypeInfo) typeInfo;
return org.apache.hadoop.hive.serde.serdeConstants.LIST_TYPE_NAME + "<" + escapeHiveType(listTypeInfo.getListElementTypeInfo().getTypeName()) + ">";
} else // Map
if (ObjectInspector.Category.MAP.equals(typeInfo.getCategory())) {
MapTypeInfo mapTypeInfo = (MapTypeInfo) typeInfo;
return org.apache.hadoop.hive.serde.serdeConstants.MAP_TYPE_NAME + "<" + escapeHiveType(mapTypeInfo.getMapKeyTypeInfo().getTypeName()) + "," + escapeHiveType(mapTypeInfo.getMapValueTypeInfo().getTypeName()) + ">";
} else // Struct
if (ObjectInspector.Category.STRUCT.equals(typeInfo.getCategory())) {
StructTypeInfo structTypeInfo = (StructTypeInfo) typeInfo;
List<String> allStructFieldNames = structTypeInfo.getAllStructFieldNames();
List<TypeInfo> allStructFieldTypeInfos = structTypeInfo.getAllStructFieldTypeInfos();
StringBuilder sb = new StringBuilder();
sb.append(serdeConstants.STRUCT_TYPE_NAME + "<");
for (int i = 0; i < allStructFieldNames.size(); i++) {
if (i > 0) {
sb.append(",");
}
sb.append("`");
sb.append(allStructFieldNames.get(i));
sb.append("`");
sb.append(":");
sb.append(escapeHiveType(allStructFieldTypeInfos.get(i).getTypeName()));
}
sb.append(">");
return sb.toString();
} else // Union
if (ObjectInspector.Category.UNION.equals(typeInfo.getCategory())) {
UnionTypeInfo unionTypeInfo = (UnionTypeInfo) typeInfo;
List<TypeInfo> allUnionObjectTypeInfos = unionTypeInfo.getAllUnionObjectTypeInfos();
StringBuilder sb = new StringBuilder();
sb.append(serdeConstants.UNION_TYPE_NAME + "<");
for (int i = 0; i < allUnionObjectTypeInfos.size(); i++) {
if (i > 0) {
sb.append(",");
}
sb.append(escapeHiveType(allUnionObjectTypeInfos.get(i).getTypeName()));
}
sb.append(">");
return sb.toString();
} else {
throw new RuntimeException("Unknown type encountered: " + type);
}
}
use of org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo in project incubator-gobblin by apache.
the class TypeInfoToSchemaParser method parseSchemaFromTypeInfo.
Schema parseSchemaFromTypeInfo(TypeInfo typeInfo, String recordNamespace, String recordName) {
Category c = typeInfo.getCategory();
Schema schema;
switch(c) {
case STRUCT:
schema = this.parseSchemaFromStruct((StructTypeInfo) typeInfo, recordNamespace, recordName);
break;
case LIST:
schema = this.parseSchemaFromList((ListTypeInfo) typeInfo, recordNamespace, recordName);
break;
case MAP:
schema = this.parseSchemaFromMap((MapTypeInfo) typeInfo, recordNamespace, recordName);
break;
case PRIMITIVE:
schema = this.parseSchemaFromPrimitive((PrimitiveTypeInfo) typeInfo);
break;
case UNION:
schema = this.parseSchemaFromUnion((UnionTypeInfo) typeInfo, recordNamespace, recordName);
break;
default:
throw new UnsupportedOperationException("Conversion from " + c + " not supported");
}
return this._mkFieldsOptional ? wrapInNullableUnion(schema) : schema;
}
Aggregations