Search in sources :

Example 1 with MapBlockBuilder

use of io.trino.spi.block.MapBlockBuilder in project trino by trinodb.

the class TestMapBlock method testCloseEntryStrict.

@Test
public void testCloseEntryStrict() throws Exception {
    MapType mapType = mapType(BIGINT, BIGINT);
    MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, 1);
    // Add 100 maps with only one entry but the same key
    for (int i = 0; i < 100; i++) {
        BlockBuilder entryBuilder = mapBlockBuilder.beginBlockEntry();
        BIGINT.writeLong(entryBuilder, 1);
        BIGINT.writeLong(entryBuilder, -1);
        mapBlockBuilder.closeEntry();
    }
    BlockBuilder entryBuilder = mapBlockBuilder.beginBlockEntry();
    // The purpose of this test is to make sure offset is calculated correctly in MapBlockBuilder.closeEntryStrict()
    for (int i = 0; i < 50; i++) {
        BIGINT.writeLong(entryBuilder, i);
        BIGINT.writeLong(entryBuilder, -1);
    }
    mapBlockBuilder.closeEntryStrict();
}
Also used : MapBlockBuilder(io.trino.spi.block.MapBlockBuilder) MapType(io.trino.spi.type.MapType) MapBlockBuilder(io.trino.spi.block.MapBlockBuilder) BlockBuilder(io.trino.spi.block.BlockBuilder) Test(org.testng.annotations.Test)

Example 2 with MapBlockBuilder

use of io.trino.spi.block.MapBlockBuilder in project trino by trinodb.

the class MapConstructor method createMap.

@UsedByGeneratedCode
public static Block createMap(MapType mapType, MethodHandle keyIndeterminate, State state, ConnectorSession session, Block keyBlock, Block valueBlock) {
    checkCondition(keyBlock.getPositionCount() == valueBlock.getPositionCount(), INVALID_FUNCTION_ARGUMENT, "Key and value arrays must be the same length");
    PageBuilder pageBuilder = state.getPageBuilder();
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) pageBuilder.getBlockBuilder(0);
    mapBlockBuilder.strict();
    BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
    for (int i = 0; i < keyBlock.getPositionCount(); i++) {
        if (keyBlock.isNull(i)) {
            // close block builder before throwing as we may be in a TRY() call
            // so that subsequent calls do not find it in an inconsistent state
            mapBlockBuilder.closeEntry();
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
        }
        Object keyObject = readNativeValue(mapType.getKeyType(), keyBlock, i);
        try {
            if ((boolean) keyIndeterminate.invoke(keyObject)) {
                throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be indeterminate: " + mapType.getKeyType().getObjectValue(session, keyBlock, i));
            }
        } catch (Throwable t) {
            mapBlockBuilder.closeEntry();
            throw internalError(t);
        }
        mapType.getKeyType().appendTo(keyBlock, i, blockBuilder);
        mapType.getValueType().appendTo(valueBlock, i, blockBuilder);
    }
    try {
        mapBlockBuilder.closeEntry();
    } catch (DuplicateMapKeyException e) {
        throw e.withDetailedMessage(mapType.getKeyType(), session);
    } finally {
        pageBuilder.declarePosition();
    }
    return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
Also used : MapBlockBuilder(io.trino.spi.block.MapBlockBuilder) TrinoException(io.trino.spi.TrinoException) DuplicateMapKeyException(io.trino.spi.block.DuplicateMapKeyException) PageBuilder(io.trino.spi.PageBuilder) MapBlockBuilder(io.trino.spi.block.MapBlockBuilder) BlockBuilder(io.trino.spi.block.BlockBuilder) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Example 3 with MapBlockBuilder

use of io.trino.spi.block.MapBlockBuilder in project trino by trinodb.

the class TestMapBlock method testStrict.

@Test
public void testStrict() {
    MapType mapType = mapType(BIGINT, BIGINT);
    MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, 1);
    mapBlockBuilder.strict();
    // Add 100 maps with only one entry but the same key
    for (int i = 0; i < 100; i++) {
        BlockBuilder entryBuilder = mapBlockBuilder.beginBlockEntry();
        BIGINT.writeLong(entryBuilder, 1);
        BIGINT.writeLong(entryBuilder, -1);
        mapBlockBuilder.closeEntry();
    }
    BlockBuilder entryBuilder = mapBlockBuilder.beginBlockEntry();
    // The purpose of this test is to make sure offset is calculated correctly in MapBlockBuilder.closeEntryStrict()
    for (int i = 0; i < 50; i++) {
        BIGINT.writeLong(entryBuilder, i);
        BIGINT.writeLong(entryBuilder, -1);
    }
    mapBlockBuilder.closeEntry();
    entryBuilder = mapBlockBuilder.beginBlockEntry();
    for (int i = 0; i < 2; i++) {
        BIGINT.writeLong(entryBuilder, 99);
        BIGINT.writeLong(entryBuilder, -1);
    }
    assertThatThrownBy(mapBlockBuilder::closeEntry).isInstanceOf(DuplicateMapKeyException.class).hasMessage("Duplicate map keys are not allowed");
}
Also used : MapBlockBuilder(io.trino.spi.block.MapBlockBuilder) DuplicateMapKeyException(io.trino.spi.block.DuplicateMapKeyException) MapType(io.trino.spi.type.MapType) MapBlockBuilder(io.trino.spi.block.MapBlockBuilder) BlockBuilder(io.trino.spi.block.BlockBuilder) Test(org.testng.annotations.Test)

Example 4 with MapBlockBuilder

use of io.trino.spi.block.MapBlockBuilder in project trino by trinodb.

the class TestMapBlock method createBlockBuilderWithValues.

private BlockBuilder createBlockBuilderWithValues(Map<String, Long>[] maps) {
    MapType mapType = mapType(VARCHAR, BIGINT);
    BlockBuilder mapBlockBuilder = mapType.createBlockBuilder(null, 1);
    for (Map<String, Long> map : maps) {
        createBlockBuilderWithValues(map, mapBlockBuilder);
    }
    return mapBlockBuilder;
}
Also used : MapType(io.trino.spi.type.MapType) MapBlockBuilder(io.trino.spi.block.MapBlockBuilder) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 5 with MapBlockBuilder

use of io.trino.spi.block.MapBlockBuilder in project trino by trinodb.

the class TestMapBlock method createBlockBuilderWithValues.

private void createBlockBuilderWithValues(Map<String, Long> map, BlockBuilder mapBlockBuilder) {
    if (map == null) {
        mapBlockBuilder.appendNull();
    } else {
        BlockBuilder elementBlockBuilder = mapBlockBuilder.beginBlockEntry();
        for (Map.Entry<String, Long> entry : map.entrySet()) {
            VARCHAR.writeSlice(elementBlockBuilder, utf8Slice(entry.getKey()));
            if (entry.getValue() == null) {
                elementBlockBuilder.appendNull();
            } else {
                BIGINT.writeLong(elementBlockBuilder, entry.getValue());
            }
        }
        mapBlockBuilder.closeEntry();
    }
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) MapBlockBuilder(io.trino.spi.block.MapBlockBuilder) BlockBuilder(io.trino.spi.block.BlockBuilder)

Aggregations

BlockBuilder (io.trino.spi.block.BlockBuilder)5 MapBlockBuilder (io.trino.spi.block.MapBlockBuilder)5 MapType (io.trino.spi.type.MapType)3 DuplicateMapKeyException (io.trino.spi.block.DuplicateMapKeyException)2 Test (org.testng.annotations.Test)2 UsedByGeneratedCode (io.trino.annotation.UsedByGeneratedCode)1 PageBuilder (io.trino.spi.PageBuilder)1 TrinoException (io.trino.spi.TrinoException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1