Search in sources :

Example 66 with RowType

use of io.trino.spi.type.RowType in project trino by trinodb.

the class CassandraType method createTypeForUserType.

private static Optional<CassandraType> createTypeForUserType(DataType dataType) {
    UserType userType = (UserType) dataType;
    // Using ImmutableMap is important as we exploit the fact that entries iteration order matches the order of putting values via builder
    ImmutableMap.Builder<String, CassandraType> argumentTypes = ImmutableMap.builder();
    for (UserType.Field field : userType) {
        Optional<CassandraType> cassandraType = CassandraType.toCassandraType(field.getType());
        if (cassandraType.isEmpty()) {
            return Optional.empty();
        }
        argumentTypes.put(field.getName(), cassandraType.get());
    }
    RowType trinoType = RowType.from(argumentTypes.buildOrThrow().entrySet().stream().map(field -> new RowType.Field(Optional.of(field.getKey()), field.getValue().getTrinoType())).collect(toImmutableList()));
    return Optional.of(new CassandraType(Kind.UDT, trinoType, argumentTypes.buildOrThrow().values().stream().collect(toImmutableList())));
}
Also used : RowType(io.trino.spi.type.RowType) InetAddresses.toAddrString(com.google.common.net.InetAddresses.toAddrString) UserType(com.datastax.driver.core.UserType) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 67 with RowType

use of io.trino.spi.type.RowType in project trino by trinodb.

the class TestParquetPageSourceFactory method testGetNestedMixedRepetitionColumnType.

@Test
public void testGetNestedMixedRepetitionColumnType() {
    RowType rowType = rowType(RowType.field("optional_level2", rowType(RowType.field("required_level3", IntegerType.INTEGER))));
    HiveColumnHandle columnHandle = new HiveColumnHandle("optional_level1", 0, HiveType.valueOf("struct<optional_level2:struct<required_level3:int>>"), rowType, Optional.of(new HiveColumnProjectionInfo(ImmutableList.of(1, 1), ImmutableList.of("optional_level2", "required_level3"), toHiveType(IntegerType.INTEGER), IntegerType.INTEGER)), REGULAR, Optional.empty());
    MessageType fileSchema = new MessageType("hive_schema", new GroupType(OPTIONAL, "optional_level1", new GroupType(OPTIONAL, "optional_level2", new PrimitiveType(REQUIRED, INT32, "required_level3"))));
    assertEquals(ParquetPageSourceFactory.getColumnType(columnHandle, fileSchema, true).get(), fileSchema.getType("optional_level1"));
}
Also used : GroupType(org.apache.parquet.schema.GroupType) HiveColumnProjectionInfo(io.trino.plugin.hive.HiveColumnProjectionInfo) RowType(io.trino.spi.type.RowType) PrimitiveType(org.apache.parquet.schema.PrimitiveType) HiveColumnHandle(io.trino.plugin.hive.HiveColumnHandle) MessageType(org.apache.parquet.schema.MessageType) Test(org.testng.annotations.Test)

Example 68 with RowType

use of io.trino.spi.type.RowType in project trino by trinodb.

the class TestParquetPredicateUtils method testParquetTupleDomainStruct.

@Test
public void testParquetTupleDomainStruct() {
    RowType rowType = rowType(RowType.field("a", INTEGER), RowType.field("b", INTEGER));
    HiveColumnHandle columnHandle = createBaseColumn("my_struct", 0, HiveType.valueOf("struct<a:int,b:int>"), rowType, REGULAR, Optional.empty());
    TupleDomain<HiveColumnHandle> domain = withColumnDomains(ImmutableMap.of(columnHandle, Domain.notNull(rowType)));
    MessageType fileSchema = new MessageType("hive_schema", new GroupType(OPTIONAL, "my_struct", new PrimitiveType(OPTIONAL, INT32, "a"), new PrimitiveType(OPTIONAL, INT32, "b")));
    Map<List<String>, RichColumnDescriptor> descriptorsByPath = getDescriptors(fileSchema, fileSchema);
    TupleDomain<ColumnDescriptor> tupleDomain = getParquetTupleDomain(descriptorsByPath, domain, fileSchema, true);
    assertTrue(tupleDomain.isAll());
}
Also used : GroupType(org.apache.parquet.schema.GroupType) RichColumnDescriptor(io.trino.parquet.RichColumnDescriptor) RichColumnDescriptor(io.trino.parquet.RichColumnDescriptor) ColumnDescriptor(org.apache.parquet.column.ColumnDescriptor) RowType(io.trino.spi.type.RowType) PrimitiveType(org.apache.parquet.schema.PrimitiveType) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) HiveColumnHandle(io.trino.plugin.hive.HiveColumnHandle) MessageType(org.apache.parquet.schema.MessageType) Test(org.testng.annotations.Test)

Example 69 with RowType

use of io.trino.spi.type.RowType in project trino by trinodb.

the class RcFileTester method assertColumnValueEquals.

private static void assertColumnValueEquals(Type type, Object actual, Object expected) {
    if (actual == null) {
        assertNull(expected);
        return;
    }
    if (type instanceof ArrayType) {
        List<?> actualArray = (List<?>) actual;
        List<?> expectedArray = (List<?>) expected;
        assertEquals(actualArray.size(), expectedArray.size());
        Type elementType = type.getTypeParameters().get(0);
        for (int i = 0; i < actualArray.size(); i++) {
            Object actualElement = actualArray.get(i);
            Object expectedElement = expectedArray.get(i);
            assertColumnValueEquals(elementType, actualElement, expectedElement);
        }
    } else if (type instanceof MapType) {
        Map<?, ?> actualMap = (Map<?, ?>) actual;
        Map<?, ?> expectedMap = (Map<?, ?>) expected;
        assertEquals(actualMap.size(), expectedMap.size());
        Type keyType = type.getTypeParameters().get(0);
        Type valueType = type.getTypeParameters().get(1);
        List<Entry<?, ?>> expectedEntries = new ArrayList<>(expectedMap.entrySet());
        for (Entry<?, ?> actualEntry : actualMap.entrySet()) {
            Iterator<Entry<?, ?>> iterator = expectedEntries.iterator();
            while (iterator.hasNext()) {
                Entry<?, ?> expectedEntry = iterator.next();
                try {
                    assertColumnValueEquals(keyType, actualEntry.getKey(), expectedEntry.getKey());
                    assertColumnValueEquals(valueType, actualEntry.getValue(), expectedEntry.getValue());
                    iterator.remove();
                } catch (AssertionError ignored) {
                }
            }
        }
        assertTrue(expectedEntries.isEmpty(), "Unmatched entries " + expectedEntries);
    } else if (type instanceof RowType) {
        List<Type> fieldTypes = type.getTypeParameters();
        List<?> actualRow = (List<?>) actual;
        List<?> expectedRow = (List<?>) expected;
        assertEquals(actualRow.size(), fieldTypes.size());
        assertEquals(actualRow.size(), expectedRow.size());
        for (int fieldId = 0; fieldId < actualRow.size(); fieldId++) {
            Type fieldType = fieldTypes.get(fieldId);
            Object actualElement = actualRow.get(fieldId);
            Object expectedElement = expectedRow.get(fieldId);
            assertColumnValueEquals(fieldType, actualElement, expectedElement);
        }
    } else if (type.equals(DOUBLE)) {
        Double actualDouble = (Double) actual;
        Double expectedDouble = (Double) expected;
        if (actualDouble.isNaN()) {
            assertTrue(expectedDouble.isNaN(), "expected double to be NaN");
        } else {
            assertEquals(actualDouble, expectedDouble, 0.001);
        }
    } else if (!Objects.equals(actual, expected)) {
        assertEquals(actual, expected);
    }
}
Also used : RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) MapType(io.trino.spi.type.MapType) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) DecimalType(io.trino.spi.type.DecimalType) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) Entry(java.util.Map.Entry) AbstractIterator(com.google.common.collect.AbstractIterator) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) StructObject(org.apache.hadoop.hive.serde2.StructObject) Map(java.util.Map) LazyMap(org.apache.hadoop.hive.serde2.lazy.LazyMap) ImmutableMap(com.google.common.collect.ImmutableMap) LazyBinaryMap(org.apache.hadoop.hive.serde2.lazybinary.LazyBinaryMap) HashMap(java.util.HashMap)

Example 70 with RowType

use of io.trino.spi.type.RowType in project trino by trinodb.

the class RcFileTester method getJavaObjectInspector.

private static ObjectInspector getJavaObjectInspector(Type type) {
    if (type.equals(BOOLEAN)) {
        return javaBooleanObjectInspector;
    }
    if (type.equals(BIGINT)) {
        return javaLongObjectInspector;
    }
    if (type.equals(INTEGER)) {
        return javaIntObjectInspector;
    }
    if (type.equals(SMALLINT)) {
        return javaShortObjectInspector;
    }
    if (type.equals(TINYINT)) {
        return javaByteObjectInspector;
    }
    if (type.equals(REAL)) {
        return javaFloatObjectInspector;
    }
    if (type.equals(DOUBLE)) {
        return javaDoubleObjectInspector;
    }
    if (type instanceof VarcharType) {
        return javaStringObjectInspector;
    }
    if (type.equals(VARBINARY)) {
        return javaByteArrayObjectInspector;
    }
    if (type.equals(DATE)) {
        return javaDateObjectInspector;
    }
    if (type.equals(TIMESTAMP_MILLIS)) {
        return javaTimestampObjectInspector;
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return getPrimitiveJavaObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
    }
    if (type instanceof ArrayType) {
        return ObjectInspectorFactory.getStandardListObjectInspector(getJavaObjectInspector(type.getTypeParameters().get(0)));
    }
    if (type instanceof MapType) {
        ObjectInspector keyObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(0));
        ObjectInspector valueObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(1));
        return ObjectInspectorFactory.getStandardMapObjectInspector(keyObjectInspector, valueObjectInspector);
    }
    if (type instanceof RowType) {
        return getStandardStructObjectInspector(type.getTypeSignature().getParameters().stream().map(parameter -> parameter.getNamedTypeSignature().getName().get()).collect(toList()), type.getTypeParameters().stream().map(RcFileTester::getJavaObjectInspector).collect(toList()));
    }
    throw new IllegalArgumentException("unsupported type: " + type);
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) ArrayType(io.trino.spi.type.ArrayType) PrimitiveObjectInspectorFactory.javaByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaByteObjectInspector) PrimitiveObjectInspectorFactory.javaLongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaLongObjectInspector) PrimitiveObjectInspectorFactory.javaTimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaTimestampObjectInspector) PrimitiveObjectInspectorFactory.javaDateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaDateObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) PrimitiveObjectInspectorFactory.javaByteArrayObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaByteArrayObjectInspector) PrimitiveObjectInspectorFactory.javaFloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaFloatObjectInspector) PrimitiveObjectInspectorFactory.javaDoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaDoubleObjectInspector) PrimitiveObjectInspectorFactory.javaIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaIntObjectInspector) PrimitiveObjectInspectorFactory.javaShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaShortObjectInspector) ObjectInspectorFactory.getStandardStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.getStandardStructObjectInspector) SettableStructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.SettableStructObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) PrimitiveObjectInspectorFactory.javaBooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaBooleanObjectInspector) PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector) PrimitiveObjectInspectorFactory.javaStringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector) VarcharType(io.trino.spi.type.VarcharType) DecimalType(io.trino.spi.type.DecimalType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType)

Aggregations

RowType (io.trino.spi.type.RowType)90 ArrayType (io.trino.spi.type.ArrayType)51 MapType (io.trino.spi.type.MapType)42 Type (io.trino.spi.type.Type)42 ImmutableList (com.google.common.collect.ImmutableList)30 VarcharType (io.trino.spi.type.VarcharType)28 BlockBuilder (io.trino.spi.block.BlockBuilder)26 DecimalType (io.trino.spi.type.DecimalType)21 Test (org.testng.annotations.Test)21 ImmutableMap (com.google.common.collect.ImmutableMap)20 Block (io.trino.spi.block.Block)20 List (java.util.List)20 Map (java.util.Map)18 ArrayList (java.util.ArrayList)17 Optional (java.util.Optional)17 CharType (io.trino.spi.type.CharType)16 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)15 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)12 TimestampWithTimeZoneType (io.trino.spi.type.TimestampWithTimeZoneType)12 HashMap (java.util.HashMap)12