Search in sources :

Example 6 with Type

use of org.apache.iceberg.types.Type in project hive by apache.

the class TestHiveIcebergSelects method testSelectDistinctFromTable.

@Test
public void testSelectDistinctFromTable() throws IOException {
    for (int i = 0; i < SUPPORTED_TYPES.size(); i++) {
        Type type = SUPPORTED_TYPES.get(i);
        if ((type == Types.TimestampType.withZone() || type == Types.TimeType.get()) && isVectorized && fileFormat == FileFormat.ORC) {
            // ORC/TIMESTAMP_INSTANT and time are not supported vectorized types for Hive
            continue;
        }
        // TODO: remove this filter when issue #1881 is resolved
        if (type == Types.UUIDType.get() && fileFormat == FileFormat.PARQUET) {
            continue;
        }
        String tableName = type.typeId().toString().toLowerCase() + "_table_" + i;
        String columnName = type.typeId().toString().toLowerCase() + "_column";
        Schema schema = new Schema(required(1, columnName, type));
        List<Record> records = TestHelper.generateRandomRecords(schema, 4, 0L);
        int size = records.stream().map(r -> r.getField(columnName)).collect(Collectors.toSet()).size();
        testTables.createTable(shell, tableName, schema, fileFormat, records);
        List<Object[]> queryResult = shell.executeStatement("select count(distinct(" + columnName + ")) from default." + tableName);
        int distinctIds = ((Long) queryResult.get(0)[0]).intValue();
        Assert.assertEquals(tableName, size, distinctIds);
    }
}
Also used : Type(org.apache.iceberg.types.Type) Schema(org.apache.iceberg.Schema) Record(org.apache.iceberg.data.Record) Test(org.junit.Test)

Example 7 with Type

use of org.apache.iceberg.types.Type in project hive by apache.

the class TestHiveIcebergSelects method testJoinTablesSupportedTypes.

@Test
public void testJoinTablesSupportedTypes() throws IOException {
    for (int i = 0; i < SUPPORTED_TYPES.size(); i++) {
        Type type = SUPPORTED_TYPES.get(i);
        if ((type == Types.TimestampType.withZone() || type == Types.TimeType.get()) && isVectorized && fileFormat == FileFormat.ORC) {
            // ORC/TIMESTAMP_INSTANT and time are not supported vectorized types for Hive
            continue;
        }
        // TODO: remove this filter when issue #1881 is resolved
        if (type == Types.UUIDType.get() && fileFormat == FileFormat.PARQUET) {
            continue;
        }
        String tableName = type.typeId().toString().toLowerCase() + "_table_" + i;
        String columnName = type.typeId().toString().toLowerCase() + "_column";
        Schema schema = new Schema(required(1, columnName, type));
        List<Record> records = TestHelper.generateRandomRecords(schema, 1, 0L);
        testTables.createTable(shell, tableName, schema, fileFormat, records);
        List<Object[]> queryResult = shell.executeStatement("select s." + columnName + ", h." + columnName + " from default." + tableName + " s join default." + tableName + " h on h." + columnName + "=s." + columnName);
        Assert.assertEquals("Non matching record count for table " + tableName + " with type " + type, 1, queryResult.size());
    }
}
Also used : Type(org.apache.iceberg.types.Type) Schema(org.apache.iceberg.Schema) Record(org.apache.iceberg.data.Record) Test(org.junit.Test)

Example 8 with Type

use of org.apache.iceberg.types.Type in project hive by apache.

the class IcebergInternalRecordWrapper method converter.

private static Function<Object, Object> converter(Type type) {
    switch(type.typeId()) {
        case TIMESTAMP:
            return timestamp -> DateTimeUtil.timestamptzFromMicros((Long) timestamp);
        case STRUCT:
            IcebergInternalRecordWrapper wrapper = new IcebergInternalRecordWrapper(type.asStructType(), type.asStructType());
            return struct -> wrapper.wrap((StructLike) struct);
        case LIST:
            if (Type.TypeID.STRUCT.equals(type.asListType().elementType().typeId())) {
                StructType listElementSchema = type.asListType().elementType().asStructType();
                Function<Type, IcebergInternalRecordWrapper> createWrapper = t -> new IcebergInternalRecordWrapper(listElementSchema, listElementSchema);
                return list -> {
                    return ((List<?>) list).stream().map(item -> createWrapper.apply(type).wrap((StructLike) item)).collect(Collectors.toList());
                };
            }
            break;
        default:
    }
    return null;
}
Also used : Arrays(java.util.Arrays) Array(java.lang.reflect.Array) Types(org.apache.iceberg.types.Types) HashMap(java.util.HashMap) StructLike(org.apache.iceberg.StructLike) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Type(org.apache.iceberg.types.Type) DateTimeUtil(org.apache.iceberg.util.DateTimeUtil) List(java.util.List) Record(org.apache.iceberg.data.Record) Map(java.util.Map) StructType(org.apache.iceberg.types.Types.StructType) Type(org.apache.iceberg.types.Type) StructType(org.apache.iceberg.types.Types.StructType) StructType(org.apache.iceberg.types.Types.StructType) StructLike(org.apache.iceberg.StructLike)

Example 9 with Type

use of org.apache.iceberg.types.Type in project hive by apache.

the class TestHiveIcebergStorageHandlerNoScan method testCreateTableWithNotSupportedTypesWithAutoConversion.

@Test
public void testCreateTableWithNotSupportedTypesWithAutoConversion() {
    TableIdentifier identifier = TableIdentifier.of("default", "not_supported_types");
    // Can not create INTERVAL types from normal create table, so leave them out from this test
    Map<String, Type> notSupportedTypes = ImmutableMap.of("TINYINT", Types.IntegerType.get(), "SMALLINT", Types.IntegerType.get(), "VARCHAR(1)", Types.StringType.get(), "CHAR(1)", Types.StringType.get());
    shell.setHiveSessionValue(InputFormatConfig.SCHEMA_AUTO_CONVERSION, "true");
    for (String notSupportedType : notSupportedTypes.keySet()) {
        shell.executeStatement("CREATE EXTERNAL TABLE not_supported_types (not_supported " + notSupportedType + ") " + "STORED BY ICEBERG " + testTables.locationForCreateTableSQL(identifier) + testTables.propertiesForCreateTableSQL(ImmutableMap.of()));
        org.apache.iceberg.Table icebergTable = testTables.loadTable(identifier);
        Assert.assertEquals(notSupportedTypes.get(notSupportedType), icebergTable.schema().columns().get(0).type());
        shell.executeStatement("DROP TABLE not_supported_types");
    }
}
Also used : TableIdentifier(org.apache.iceberg.catalog.TableIdentifier) Type(org.apache.iceberg.types.Type) Table(org.apache.iceberg.Table) Test(org.junit.Test)

Example 10 with Type

use of org.apache.iceberg.types.Type in project hive by apache.

the class TestHiveIcebergComplexTypeWrites method buildComplexTypeInnerQuery.

private StringBuilder buildComplexTypeInnerQuery(Object field, Type type) {
    StringBuilder query = new StringBuilder();
    if (type instanceof Types.ListType) {
        query.append("array(");
        List<Object> elements = (List<Object>) field;
        Assert.assertFalse("Hive can not handle empty array() inserts", elements.isEmpty());
        Type innerType = ((Types.ListType) type).fields().get(0).type();
        if (!elements.isEmpty()) {
            elements.forEach(e -> query.append(buildComplexTypeInnerQuery(e, innerType)));
            query.setLength(query.length() - 1);
        }
        query.append("),");
    } else if (type instanceof Types.MapType) {
        query.append("map(");
        Map<Object, Object> entries = (Map<Object, Object>) field;
        Type keyType = ((Types.MapType) type).fields().get(0).type();
        Type valueType = ((Types.MapType) type).fields().get(1).type();
        if (!entries.isEmpty()) {
            entries.entrySet().forEach(e -> query.append(buildComplexTypeInnerQuery(e.getKey(), keyType).append(buildComplexTypeInnerQuery(e.getValue(), valueType))));
            query.setLength(query.length() - 1);
        }
        query.append("),");
    } else if (type instanceof Types.StructType) {
        query.append("named_struct(");
        ((GenericRecord) field).struct().fields().stream().forEach(f -> query.append(buildComplexTypeInnerQuery(f.name(), Types.StringType.get())).append(buildComplexTypeInnerQuery(((GenericRecord) field).getField(f.name()), f.type())));
        query.setLength(query.length() - 1);
        query.append("),");
    } else if (type instanceof Types.StringType) {
        if (field != null) {
            query.append("'").append(field).append("',");
        }
    } else {
        throw new RuntimeException("Unsupported type in complex query build.");
    }
    return query;
}
Also used : Types(org.apache.iceberg.types.Types) Table(org.apache.iceberg.Table) IOException(java.io.IOException) Test(org.junit.Test) TestHelper(org.apache.iceberg.mr.TestHelper) ImmutableList(org.apache.iceberg.relocated.com.google.common.collect.ImmutableList) Schema(org.apache.iceberg.Schema) Type(org.apache.iceberg.types.Type) List(java.util.List) Record(org.apache.iceberg.data.Record) NestedField.required(org.apache.iceberg.types.Types.NestedField.required) Map(java.util.Map) GenericRecord(org.apache.iceberg.data.GenericRecord) Assert(org.junit.Assert) Types(org.apache.iceberg.types.Types) Type(org.apache.iceberg.types.Type) ImmutableList(org.apache.iceberg.relocated.com.google.common.collect.ImmutableList) List(java.util.List) Map(java.util.Map)

Aggregations

Type (org.apache.iceberg.types.Type)20 Schema (org.apache.iceberg.Schema)7 TypeConverter.toPrestoType (com.facebook.presto.iceberg.TypeConverter.toPrestoType)6 PartitionField (org.apache.iceberg.PartitionField)6 Types (org.apache.iceberg.types.Types)6 Test (org.junit.Test)6 ImmutableList (com.google.common.collect.ImmutableList)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)5 List (java.util.List)5 Map (java.util.Map)5 Table (org.apache.iceberg.Table)5 ArrayType (com.facebook.presto.common.type.ArrayType)3 RowType (com.facebook.presto.common.type.RowType)3 TypeManager (com.facebook.presto.common.type.TypeManager)3 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)3 Constraint (com.facebook.presto.spi.Constraint)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Objects.requireNonNull (java.util.Objects.requireNonNull)3