Search in sources :

Example 76 with MapType

use of io.trino.spi.type.MapType 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 77 with MapType

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

the class TestMapBlock method assertValue.

private void assertValue(Block mapBlock, int position, Map<String, Long> map) {
    MapType mapType = mapType(VARCHAR, BIGINT);
    // null maps are handled by assertPositionValue
    requireNonNull(map, "map is null");
    assertFalse(mapBlock.isNull(position));
    SingleMapBlock elementBlock = (SingleMapBlock) mapType.getObject(mapBlock, position);
    assertEquals(elementBlock.getPositionCount(), map.size() * 2);
    // Test new/hash-index access: assert inserted keys
    for (Map.Entry<String, Long> entry : map.entrySet()) {
        int pos = elementBlock.seekKey(utf8Slice(entry.getKey()));
        assertNotEquals(pos, -1);
        if (entry.getValue() == null) {
            assertTrue(elementBlock.isNull(pos));
        } else {
            assertFalse(elementBlock.isNull(pos));
            assertEquals(BIGINT.getLong(elementBlock, pos), (long) entry.getValue());
        }
    }
    // Test new/hash-index access: assert non-existent keys
    for (int i = 0; i < 10; i++) {
        assertEquals(elementBlock.seekKey(utf8Slice("not-inserted-" + i)), -1);
    }
    // Test legacy/iterative access
    for (int i = 0; i < elementBlock.getPositionCount(); i += 2) {
        String actualKey = VARCHAR.getSlice(elementBlock, i).toStringUtf8();
        Long actualValue;
        if (elementBlock.isNull(i + 1)) {
            actualValue = null;
        } else {
            actualValue = BIGINT.getLong(elementBlock, i + 1);
        }
        assertTrue(map.containsKey(actualKey));
        assertEquals(actualValue, map.get(actualKey));
    }
}
Also used : SingleMapBlock(io.trino.spi.block.SingleMapBlock) HashMap(java.util.HashMap) Map(java.util.Map) MapType(io.trino.spi.type.MapType)

Example 78 with MapType

use of io.trino.spi.type.MapType 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 79 with MapType

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

the class BenchmarkMapCopy method benchmarkMapCopy.

@Benchmark
@OperationsPerInvocation(POSITIONS)
public BlockBuilder benchmarkMapCopy(BenchmarkData data) {
    Block block = data.getDataBlock();
    BlockBuilder blockBuilder = data.getBlockBuilder();
    MapType mapType = mapType(VARCHAR, BIGINT);
    for (int i = 0; i < POSITIONS; i++) {
        mapType.appendTo(block, i, blockBuilder);
    }
    return blockBuilder;
}
Also used : Block(io.trino.spi.block.Block) MapType(io.trino.spi.type.MapType) BlockBuilder(io.trino.spi.block.BlockBuilder) Benchmark(org.openjdk.jmh.annotations.Benchmark) OperationsPerInvocation(org.openjdk.jmh.annotations.OperationsPerInvocation)

Example 80 with MapType

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

the class AvroDecoderTestUtil method checkMapValues.

public static void checkMapValues(Block block, Type type, Object value) {
    assertNotNull(type, "Type is null");
    assertTrue(type instanceof MapType, "Unexpected type");
    assertTrue(((MapType) type).getKeyType() instanceof VarcharType, "Unexpected key type");
    assertNotNull(block, "Block is null");
    assertNotNull(value, "Value is null");
    Map<String, ?> expected = (Map<String, ?>) value;
    assertEquals(block.getPositionCount(), expected.size() * 2);
    Type valueType = ((MapType) type).getValueType();
    if (valueType instanceof ArrayType) {
        for (int index = 0; index < block.getPositionCount(); index += 2) {
            String actualKey = VARCHAR.getSlice(block, index).toStringUtf8();
            assertTrue(expected.containsKey(actualKey));
            if (block.isNull(index + 1)) {
                assertNull(expected.get(actualKey));
                continue;
            }
            Block arrayBlock = block.getObject(index + 1, Block.class);
            checkArrayValues(arrayBlock, valueType, expected.get(actualKey));
        }
    } else if (valueType instanceof MapType) {
        for (int index = 0; index < block.getPositionCount(); index += 2) {
            String actualKey = VARCHAR.getSlice(block, index).toStringUtf8();
            assertTrue(expected.containsKey(actualKey));
            if (block.isNull(index + 1)) {
                assertNull(expected.get(actualKey));
                continue;
            }
            Block mapBlock = block.getObject(index + 1, Block.class);
            checkMapValues(mapBlock, valueType, expected.get(actualKey));
        }
    } else if (valueType instanceof RowType) {
        for (int index = 0; index < block.getPositionCount(); index += 2) {
            String actualKey = VARCHAR.getSlice(block, index).toStringUtf8();
            assertTrue(expected.containsKey(actualKey));
            if (block.isNull(index + 1)) {
                assertNull(expected.get(actualKey));
                continue;
            }
            Block rowBlock = block.getObject(index + 1, Block.class);
            checkRowValues(rowBlock, valueType, expected.get(actualKey));
        }
    } else {
        for (int index = 0; index < block.getPositionCount(); index += 2) {
            String actualKey = VARCHAR.getSlice(block, index).toStringUtf8();
            assertTrue(expected.containsKey(actualKey));
            checkPrimitiveValue(getObjectValue(valueType, block, index + 1), expected.get(actualKey));
        }
    }
}
Also used : ArrayType(io.trino.spi.type.ArrayType) RowType(io.trino.spi.type.RowType) MapType(io.trino.spi.type.MapType) Type(io.trino.spi.type.Type) ArrayType(io.trino.spi.type.ArrayType) VarcharType(io.trino.spi.type.VarcharType) VarcharType(io.trino.spi.type.VarcharType) Block(io.trino.spi.block.Block) RowType(io.trino.spi.type.RowType) Map(java.util.Map) MapType(io.trino.spi.type.MapType)

Aggregations

MapType (io.trino.spi.type.MapType)85 Type (io.trino.spi.type.Type)45 ArrayType (io.trino.spi.type.ArrayType)42 RowType (io.trino.spi.type.RowType)38 BlockBuilder (io.trino.spi.block.BlockBuilder)28 VarcharType (io.trino.spi.type.VarcharType)26 Block (io.trino.spi.block.Block)17 DecimalType (io.trino.spi.type.DecimalType)17 Map (java.util.Map)17 Test (org.testng.annotations.Test)17 ImmutableList (com.google.common.collect.ImmutableList)16 List (java.util.List)14 TypeSignature.mapType (io.trino.spi.type.TypeSignature.mapType)13 CharType (io.trino.spi.type.CharType)12 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)11 TypeOperators (io.trino.spi.type.TypeOperators)10 ImmutableMap (com.google.common.collect.ImmutableMap)9 VarbinaryType (io.trino.spi.type.VarbinaryType)8 Collectors.toList (java.util.stream.Collectors.toList)8