Search in sources :

Example 16 with SingleMapBlock

use of io.trino.spi.block.SingleMapBlock 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)

Aggregations

SingleMapBlock (io.trino.spi.block.SingleMapBlock)16 UsedByGeneratedCode (io.trino.annotation.UsedByGeneratedCode)8 Block (io.trino.spi.block.Block)4 MapBlock (io.trino.spi.block.MapBlock)3 MapType (io.trino.spi.type.MapType)3 HashMap (java.util.HashMap)3 BlockBuilder (io.trino.spi.block.BlockBuilder)2 ImplementAvgFloatingPoint (io.trino.plugin.jdbc.aggregation.ImplementAvgFloatingPoint)1 PageBuilder (io.trino.spi.PageBuilder)1 MapBlockBuilder (io.trino.spi.block.MapBlockBuilder)1 Type (io.trino.spi.type.Type)1 TypeOperators (io.trino.spi.type.TypeOperators)1 TypeSignature.functionType (io.trino.spi.type.TypeSignature.functionType)1 TypeSignature.mapType (io.trino.spi.type.TypeSignature.mapType)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Test (org.testng.annotations.Test)1