Search in sources :

Example 16 with UnionTypeInfo

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));
}
Also used : UnionTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo) Test(org.junit.Test)

Example 17 with 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());
}
Also used : Schema(org.apache.avro.Schema) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) UnionTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) UnionTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo) Test(org.junit.Test)

Example 18 with UnionTypeInfo

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;
}
Also used : PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory) Category(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) UnionTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo) UnionTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo)

Example 19 with UnionTypeInfo

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);
    }
}
Also used : ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) List(java.util.List) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) UnionTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo) UnionTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo)

Example 20 with UnionTypeInfo

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;
}
Also used : PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory) Category(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) Schema(org.apache.avro.Schema) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) UnionTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo)

Aggregations

UnionTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.UnionTypeInfo)31 ListTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo)26 MapTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo)26 StructTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo)25 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)24 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)23 ArrayList (java.util.ArrayList)16 List (java.util.List)13 DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)12 CharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo)9 BytesWritable (org.apache.hadoop.io.BytesWritable)9 Text (org.apache.hadoop.io.Text)9 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)8 HiveIntervalDayTimeWritable (org.apache.hadoop.hive.serde2.io.HiveIntervalDayTimeWritable)8 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)8 VarcharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo)8 Map (java.util.Map)7 HiveDecimal (org.apache.hadoop.hive.common.type.HiveDecimal)7 Timestamp (org.apache.hadoop.hive.common.type.Timestamp)7 ByteWritable (org.apache.hadoop.hive.serde2.io.ByteWritable)7