Search in sources :

Example 11 with RowType

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

the class TestAvroDecoder method testRowWithNulls.

@Test
public void testRowWithNulls() {
    Schema schema = SchemaBuilder.record("record_field").fields().name("f1").type().optional().floatType().name("f2").type().optional().doubleType().name("f3").type().optional().intType().name("f4").type().optional().longType().name("f5").type().optional().stringType().name("f6").type().optional().enumeration("color").symbols("red", "blue", "green").name("f7").type().optional().fixed("fixed5").size(5).name("f8").type().optional().bytesType().name("f9").type().optional().booleanType().name("f10").type().optional().array().items().unionOf().nullType().and().record("sub_array_field").fields().name("sf1").type().optional().stringType().name("sf2").type().optional().longType().endRecord().endUnion().name("f11").type().optional().map().values().unionOf().nullType().and().record("sub_map_field").fields().name("sf1").type().optional().doubleType().name("sf2").type().optional().booleanType().endRecord().endUnion().name("f12").type().optional().record("sub_row_field").fields().name("sf1").type().optional().intType().name("sf2").type().optional().enumeration("state").symbols("initialized", "running", "finished", "failed").endRecord().endRecord();
    RowType rowType = RowType.from(ImmutableList.<RowType.Field>builder().add(RowType.field("f1", REAL)).add(RowType.field("f2", DOUBLE)).add(RowType.field("f3", INTEGER)).add(RowType.field("f4", BIGINT)).add(RowType.field("f5", VARCHAR)).add(RowType.field("f6", VARCHAR)).add(RowType.field("f7", VARBINARY)).add(RowType.field("f8", VARBINARY)).add(RowType.field("f9", BOOLEAN)).add(RowType.field("f10", new ArrayType(RowType.from(ImmutableList.<RowType.Field>builder().add(RowType.field("sf1", VARCHAR)).add(RowType.field("sf2", BIGINT)).build())))).add(RowType.field("f11", MAP_OF_RECORD)).add(RowType.field("f12", RowType.from(ImmutableList.<RowType.Field>builder().add(RowType.field("sf1", INTEGER)).add(RowType.field("sf2", VARCHAR)).build()))).build());
    GenericRecord data = new GenericRecordBuilder(schema).build();
    DecoderTestColumnHandle row = new DecoderTestColumnHandle(0, "record_field", rowType, "record_field", null, null, false, false, false);
    Map<DecoderColumnHandle, FieldValueProvider> decodedRow = buildAndDecodeColumn(row, "record_field", schema.toString(), data);
    checkRowValue(decodedRow, row, data);
    // Check nested fields with null values
    GenericData.Array<GenericRecord> array = new GenericData.Array<GenericRecord>(schema.getField("f10").schema().getTypes().get(1), Arrays.asList(new GenericRecordBuilder(schema.getField("f10").schema().getTypes().get(1).getElementType().getTypes().get(1)).build(), null));
    data = new GenericRecordBuilder(schema).set("f10", array).set("f11", ImmutableMap.builder().put("key1", new GenericRecordBuilder(schema.getField("f11").schema().getTypes().get(1).getValueType().getTypes().get(1)).build()).buildOrThrow()).set("f12", new GenericRecordBuilder(schema.getField("f12").schema().getTypes().get(1)).build()).build();
    decodedRow = buildAndDecodeColumn(row, "record_field", schema.toString(), data);
    checkRowValue(decodedRow, row, data);
}
Also used : DecoderTestColumnHandle(io.trino.decoder.DecoderTestColumnHandle) FieldValueProvider(io.trino.decoder.FieldValueProvider) Schema(org.apache.avro.Schema) RowType(io.trino.spi.type.RowType) DecoderColumnHandle(io.trino.decoder.DecoderColumnHandle) GenericData(org.apache.avro.generic.GenericData) ArrayType(io.trino.spi.type.ArrayType) GenericArray(org.apache.avro.generic.GenericArray) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.testng.annotations.Test)

Example 12 with RowType

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

the class CassandraType method createTypeForTuple.

private static Optional<CassandraType> createTypeForTuple(DataType dataType) {
    TupleType tupleType = (TupleType) dataType;
    List<Optional<CassandraType>> argumentTypesOptionals = tupleType.getComponentTypes().stream().map(CassandraType::toCassandraType).collect(toImmutableList());
    if (argumentTypesOptionals.stream().anyMatch(Optional::isEmpty)) {
        return Optional.empty();
    }
    List<CassandraType> argumentTypes = argumentTypesOptionals.stream().map(Optional::get).collect(toImmutableList());
    RowType trinoType = RowType.anonymous(argumentTypes.stream().map(CassandraType::getTrinoType).collect(toImmutableList()));
    return Optional.of(new CassandraType(Kind.TUPLE, trinoType, argumentTypes));
}
Also used : Optional(java.util.Optional) TupleType(com.datastax.driver.core.TupleType) RowType(io.trino.spi.type.RowType)

Example 13 with RowType

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

the class MapEntriesFunction method mapFromEntries.

@TypeParameter("K")
@TypeParameter("V")
@SqlType("array(row(K,V))")
public Block mapFromEntries(@TypeParameter("row(K,V)") RowType rowType, @SqlType("map(K,V)") Block block) {
    verify(rowType.getTypeParameters().size() == 2);
    verify(block.getPositionCount() % 2 == 0);
    Type keyType = rowType.getTypeParameters().get(0);
    Type valueType = rowType.getTypeParameters().get(1);
    ArrayType arrayType = new ArrayType(rowType);
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    int entryCount = block.getPositionCount() / 2;
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder entryBuilder = blockBuilder.beginBlockEntry();
    for (int i = 0; i < entryCount; i++) {
        BlockBuilder rowBuilder = entryBuilder.beginBlockEntry();
        keyType.appendTo(block, 2 * i, rowBuilder);
        valueType.appendTo(block, 2 * i + 1, rowBuilder);
        entryBuilder.closeEntry();
    }
    blockBuilder.closeEntry();
    pageBuilder.declarePosition();
    return arrayType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
}
Also used : ArrayType(io.trino.spi.type.ArrayType) RowType(io.trino.spi.type.RowType) SqlType(io.trino.spi.function.SqlType) Type(io.trino.spi.type.Type) ArrayType(io.trino.spi.type.ArrayType) BlockBuilder(io.trino.spi.block.BlockBuilder) TypeParameter(io.trino.spi.function.TypeParameter) SqlType(io.trino.spi.function.SqlType)

Example 14 with RowType

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

the class MapFromEntriesFunction method mapFromEntries.

@TypeParameter("K")
@TypeParameter("V")
@SqlType("map(K,V)")
@SqlNullable
public Block mapFromEntries(@OperatorDependency(operator = EQUAL, argumentTypes = { "K", "K" }, convention = @Convention(arguments = { BLOCK_POSITION, BLOCK_POSITION }, result = NULLABLE_RETURN)) BlockPositionEqual keyEqual, @OperatorDependency(operator = HASH_CODE, argumentTypes = "K", convention = @Convention(arguments = BLOCK_POSITION, result = FAIL_ON_NULL)) BlockPositionHashCode keyHashCode, @TypeParameter("map(K,V)") MapType mapType, ConnectorSession session, @SqlType("array(row(K,V))") Block mapEntries) {
    Type keyType = mapType.getKeyType();
    Type valueType = mapType.getValueType();
    RowType mapEntryType = RowType.anonymous(ImmutableList.of(keyType, valueType));
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    int entryCount = mapEntries.getPositionCount();
    BlockBuilder mapBlockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder resultBuilder = mapBlockBuilder.beginBlockEntry();
    TypedSet uniqueKeys = createEqualityTypedSet(keyType, keyEqual, keyHashCode, entryCount, "map_from_entries");
    for (int i = 0; i < entryCount; i++) {
        if (mapEntries.isNull(i)) {
            mapBlockBuilder.closeEntry();
            pageBuilder.declarePosition();
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map entry cannot be null");
        }
        Block mapEntryBlock = mapEntryType.getObject(mapEntries, i);
        if (mapEntryBlock.isNull(0)) {
            mapBlockBuilder.closeEntry();
            pageBuilder.declarePosition();
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
        }
        if (!uniqueKeys.add(mapEntryBlock, 0)) {
            mapBlockBuilder.closeEntry();
            pageBuilder.declarePosition();
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("Duplicate keys (%s) are not allowed", keyType.getObjectValue(session, mapEntryBlock, 0)));
        }
        keyType.appendTo(mapEntryBlock, 0, resultBuilder);
        valueType.appendTo(mapEntryBlock, 1, resultBuilder);
    }
    mapBlockBuilder.closeEntry();
    pageBuilder.declarePosition();
    return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
Also used : Type(io.trino.spi.type.Type) SqlType(io.trino.spi.function.SqlType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) RowType(io.trino.spi.type.RowType) TypedSet(io.trino.operator.aggregation.TypedSet) TypedSet.createEqualityTypedSet(io.trino.operator.aggregation.TypedSet.createEqualityTypedSet) TrinoException(io.trino.spi.TrinoException) Block(io.trino.spi.block.Block) BlockBuilder(io.trino.spi.block.BlockBuilder) SqlNullable(io.trino.spi.function.SqlNullable) TypeParameter(io.trino.spi.function.TypeParameter) SqlType(io.trino.spi.function.SqlType)

Example 15 with RowType

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

the class MultimapFromEntriesFunction method multimapFromEntries.

@TypeParameter("K")
@TypeParameter("V")
@SqlType("map(K,array(V))")
@SqlNullable
public Block multimapFromEntries(@TypeParameter("map(K,array(V))") MapType mapType, @OperatorDependency(operator = EQUAL, argumentTypes = { "K", "K" }, convention = @Convention(arguments = { BLOCK_POSITION, BLOCK_POSITION }, result = NULLABLE_RETURN)) BlockPositionEqual keyEqual, @OperatorDependency(operator = HASH_CODE, argumentTypes = "K", convention = @Convention(arguments = BLOCK_POSITION, result = FAIL_ON_NULL)) BlockPositionHashCode keyHashCode, @SqlType("array(row(K,V))") Block mapEntries) {
    Type keyType = mapType.getKeyType();
    Type valueType = ((ArrayType) mapType.getValueType()).getElementType();
    RowType mapEntryType = RowType.anonymous(ImmutableList.of(keyType, valueType));
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    int entryCount = mapEntries.getPositionCount();
    if (entryCount > entryIndicesList.length) {
        initializeEntryIndicesList(entryCount);
    }
    TypedSet keySet = createEqualityTypedSet(keyType, keyEqual, keyHashCode, entryCount, NAME);
    for (int i = 0; i < entryCount; i++) {
        if (mapEntries.isNull(i)) {
            clearEntryIndices(keySet.size());
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map entry cannot be null");
        }
        Block mapEntryBlock = mapEntryType.getObject(mapEntries, i);
        if (mapEntryBlock.isNull(0)) {
            clearEntryIndices(keySet.size());
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
        }
        if (keySet.add(mapEntryBlock, 0)) {
            entryIndicesList[keySet.size() - 1].add(i);
        } else {
            entryIndicesList[keySet.positionOf(mapEntryBlock, 0)].add(i);
        }
    }
    BlockBuilder multimapBlockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder mapWriter = multimapBlockBuilder.beginBlockEntry();
    for (int i = 0; i < keySet.size(); i++) {
        keyType.appendTo(mapEntryType.getObject(mapEntries, entryIndicesList[i].getInt(0)), 0, mapWriter);
        BlockBuilder valuesArray = mapWriter.beginBlockEntry();
        for (int entryIndex : entryIndicesList[i]) {
            valueType.appendTo(mapEntryType.getObject(mapEntries, entryIndex), 1, valuesArray);
        }
        mapWriter.closeEntry();
    }
    multimapBlockBuilder.closeEntry();
    pageBuilder.declarePosition();
    clearEntryIndices(keySet.size());
    return mapType.getObject(multimapBlockBuilder, multimapBlockBuilder.getPositionCount() - 1);
}
Also used : ArrayType(io.trino.spi.type.ArrayType) Type(io.trino.spi.type.Type) SqlType(io.trino.spi.function.SqlType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) RowType(io.trino.spi.type.RowType) TypedSet(io.trino.operator.aggregation.TypedSet) TypedSet.createEqualityTypedSet(io.trino.operator.aggregation.TypedSet.createEqualityTypedSet) TrinoException(io.trino.spi.TrinoException) Block(io.trino.spi.block.Block) BlockBuilder(io.trino.spi.block.BlockBuilder) SqlNullable(io.trino.spi.function.SqlNullable) TypeParameter(io.trino.spi.function.TypeParameter) SqlType(io.trino.spi.function.SqlType)

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