Search in sources :

Example 26 with SingleMapBlock

use of com.facebook.presto.common.block.SingleMapBlock in project presto by prestodb.

the class TestMapBlock method assertValue.

private static void assertValue(Block mapBlock, int position, Map<String, Long> map) {
    MapType mapType = mapType(VARCHAR, BIGINT);
    MethodHandle keyNativeHashCode = getOperatorMethodHandle(OperatorType.HASH_CODE, VARCHAR);
    MethodHandle keyNativeEquals = getOperatorMethodHandle(OperatorType.EQUAL, VARCHAR, VARCHAR);
    MethodHandle keyBlockNativeEquals = compose(keyNativeEquals, nativeValueGetter(VARCHAR));
    MethodHandle keyBlockHashCode = compose(keyNativeHashCode, nativeValueGetter(VARCHAR));
    // 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()), keyNativeHashCode, keyBlockNativeEquals, keyBlockHashCode);
        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), keyNativeHashCode, keyBlockNativeEquals, keyBlockHashCode), -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(com.facebook.presto.common.block.SingleMapBlock) HashMap(java.util.HashMap) Map(java.util.Map) MapType(com.facebook.presto.common.type.MapType) MethodHandle(java.lang.invoke.MethodHandle) TestingEnvironment.getOperatorMethodHandle(com.facebook.presto.testing.TestingEnvironment.getOperatorMethodHandle)

Aggregations

SingleMapBlock (com.facebook.presto.common.block.SingleMapBlock)26 NotSupportedException (com.facebook.presto.common.NotSupportedException)12 PrestoException (com.facebook.presto.spi.PrestoException)12 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)11 Test (org.testng.annotations.Test)8 MapType (com.facebook.presto.common.type.MapType)7 Block (com.facebook.presto.common.block.Block)6 MapBlock (com.facebook.presto.common.block.MapBlock)6 MapBlockBuilder (com.facebook.presto.common.block.MapBlockBuilder)6 AbstractMapBlock (com.facebook.presto.common.block.AbstractMapBlock)5 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)5 HashMap (java.util.HashMap)4 Type (com.facebook.presto.common.type.Type)3 TestingEnvironment.getOperatorMethodHandle (com.facebook.presto.testing.TestingEnvironment.getOperatorMethodHandle)3 MethodHandle (java.lang.invoke.MethodHandle)3 ArrayType (com.facebook.presto.common.type.ArrayType)2 Map (java.util.Map)2 BlockAssertions.createLongDictionaryBlock (com.facebook.presto.block.BlockAssertions.createLongDictionaryBlock)1 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)1 BlockAssertions.createRLEBlock (com.facebook.presto.block.BlockAssertions.createRLEBlock)1