Search in sources :

Example 36 with MapType

use of com.facebook.presto.common.type.MapType in project presto by prestodb.

the class TestMapBlock method assertValueUnchecked.

private static void assertValueUnchecked(Block mapBlock, int internalPosition, Map<String, Long> map) {
    MapType mapType = mapType(VARCHAR, BIGINT);
    MethodHandle keyNativeHashCode = getOperatorMethodHandle(OperatorType.HASH_CODE, VARCHAR);
    MethodHandle keyBlockHashCode = compose(keyNativeHashCode, nativeValueGetter(VARCHAR));
    MethodHandle keyNativeEquals = getOperatorMethodHandle(OperatorType.EQUAL, VARCHAR, VARCHAR);
    MethodHandle keyBlockNativeEquals = compose(keyNativeEquals, nativeValueGetter(VARCHAR));
    // null maps are handled by assertPositionValue
    requireNonNull(map, "map is null");
    assertFalse(mapBlock.isNullUnchecked((internalPosition)));
    SingleMapBlock elementBlock = (SingleMapBlock) mapType.getBlockUnchecked(mapBlock, (internalPosition));
    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.isNullUnchecked(pos + elementBlock.getOffsetBase()));
        } else {
            assertFalse(elementBlock.isNullUnchecked(pos + elementBlock.getOffsetBase()));
            assertEquals(BIGINT.getLongUnchecked(elementBlock, pos + elementBlock.getOffsetBase()), (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.getSliceUnchecked(elementBlock, i + elementBlock.getOffset()).toStringUtf8();
        Long actualValue;
        if (elementBlock.isNullUnchecked(i + 1 + elementBlock.getOffset())) {
            actualValue = null;
        } else {
            actualValue = BIGINT.getLongUnchecked(elementBlock, i + 1 + elementBlock.getOffsetBase());
        }
        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)

Example 37 with MapType

use of com.facebook.presto.common.type.MapType in project presto by prestodb.

the class TestRowBasedSerialization method testMap.

@Test
public void testMap() {
    MapType mapType = createMapType(BIGINT, BIGINT);
    assertRoundTrip(ImmutableList.of(mapType), ImmutableList.of(createMapBlock(mapType, ImmutableMap.of(312L, 123L))));
    assertRoundTrip(ImmutableList.of(mapType), ImmutableList.of(createMapBlock(mapType, singletonMap(312L, null))));
    Map<Long, Long> map = new HashMap<>();
    map.put(1L, null);
    map.put(2L, 2L);
    map.put(3L, null);
    map.put(4L, 4L);
    assertRoundTrip(ImmutableList.of(mapType), ImmutableList.of(createMapBlock(mapType, map)));
}
Also used : HashMap(java.util.HashMap) MapType(com.facebook.presto.common.type.MapType) BlockAssertions.createMapType(com.facebook.presto.block.BlockAssertions.createMapType) Test(org.testng.annotations.Test)

Example 38 with MapType

use of com.facebook.presto.common.type.MapType in project presto by prestodb.

the class BlockAssertions method createRandomBlockForType.

// Note: Make sure positionCount is sufficiently large if nestedNullRate or primitiveNullRate is greater than 0
public static Block createRandomBlockForType(Type type, int positionCount, float primitiveNullRate, float nestedNullRate, boolean createView, List<Encoding> wrappings) {
    verifyNullRate(primitiveNullRate);
    verifyNullRate(nestedNullRate);
    Block block = null;
    if (createView) {
        positionCount *= 2;
    }
    if (type == BOOLEAN) {
        block = createRandomBooleansBlock(positionCount, primitiveNullRate);
    } else if (type == BIGINT) {
        block = createRandomLongsBlock(positionCount, primitiveNullRate);
    } else if (type == INTEGER || type == REAL) {
        block = createRandomIntsBlock(positionCount, primitiveNullRate);
    } else if (type == SMALLINT) {
        block = createRandomSmallintsBlock(positionCount, primitiveNullRate);
    } else if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.isShort()) {
            block = createRandomLongsBlock(positionCount, primitiveNullRate);
        } else {
            block = createRandomLongDecimalsBlock(positionCount, primitiveNullRate);
        }
    } else if (type == VARCHAR) {
        block = createRandomStringBlock(positionCount, primitiveNullRate, MAX_STRING_SIZE);
    } else {
        // Nested types
        // Build isNull and offsets of size positionCount
        boolean[] isNull = null;
        if (nestedNullRate > 0) {
            isNull = new boolean[positionCount];
        }
        int[] offsets = new int[positionCount + 1];
        for (int position = 0; position < positionCount; position++) {
            if (nestedNullRate > 0 && ThreadLocalRandom.current().nextDouble(1) < nestedNullRate) {
                isNull[position] = true;
                offsets[position + 1] = offsets[position];
            } else {
                offsets[position + 1] = offsets[position] + (type instanceof RowType ? 1 : ThreadLocalRandom.current().nextInt(ENTRY_SIZE) + 1);
            }
        }
        // Build the nested block of size offsets[positionCount].
        if (type instanceof ArrayType) {
            Block valuesBlock = createRandomBlockForType(((ArrayType) type).getElementType(), offsets[positionCount], primitiveNullRate, nestedNullRate, createView, wrappings);
            block = fromElementBlock(positionCount, Optional.ofNullable(isNull), offsets, valuesBlock);
        } else if (type instanceof MapType) {
            MapType mapType = (MapType) type;
            Block keyBlock = createRandomBlockForType(mapType.getKeyType(), offsets[positionCount], 0.0f, 0.0f, createView, wrappings);
            Block valueBlock = createRandomBlockForType(mapType.getValueType(), offsets[positionCount], primitiveNullRate, nestedNullRate, createView, wrappings);
            block = mapType.createBlockFromKeyValue(positionCount, Optional.ofNullable(isNull), offsets, keyBlock, valueBlock);
        } else if (type instanceof RowType) {
            List<Type> fieldTypes = type.getTypeParameters();
            Block[] fieldBlocks = new Block[fieldTypes.size()];
            for (int i = 0; i < fieldBlocks.length; i++) {
                fieldBlocks[i] = createRandomBlockForType(fieldTypes.get(i), positionCount, primitiveNullRate, nestedNullRate, createView, wrappings);
            }
            block = fromFieldBlocks(positionCount, Optional.ofNullable(isNull), fieldBlocks);
        } else {
            throw new IllegalArgumentException(format("type %s is not supported.", type));
        }
    }
    if (createView) {
        positionCount /= 2;
        int offset = positionCount / 2;
        block = block.getRegion(offset, positionCount);
    }
    if (!wrappings.isEmpty()) {
        block = wrapBlock(block, positionCount, wrappings);
    }
    return block;
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) ArrayBlock.fromElementBlock(com.facebook.presto.common.block.ArrayBlock.fromElementBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) Block(com.facebook.presto.common.block.Block) DecimalType(com.facebook.presto.common.type.DecimalType) RowType(com.facebook.presto.common.type.RowType) List(java.util.List) ArrayList(java.util.ArrayList) MapType(com.facebook.presto.common.type.MapType)

Example 39 with MapType

use of com.facebook.presto.common.type.MapType in project presto by prestodb.

the class BlockAssertions method createMapType.

public static MapType createMapType(Type keyType, Type valueType) {
    MethodHandle keyNativeEquals = getOperatorMethodHandle(OperatorType.EQUAL, keyType, keyType);
    MethodHandle keyBlockEquals = compose(keyNativeEquals, nativeValueGetter(keyType), nativeValueGetter(keyType));
    MethodHandle keyNativeHashCode = getOperatorMethodHandle(OperatorType.HASH_CODE, keyType);
    MethodHandle keyBlockHashCode = compose(keyNativeHashCode, nativeValueGetter(keyType));
    return new MapType(keyType, valueType, keyBlockEquals, keyBlockHashCode);
}
Also used : MapType(com.facebook.presto.common.type.MapType) MethodHandle(java.lang.invoke.MethodHandle) TestingEnvironment.getOperatorMethodHandle(com.facebook.presto.testing.TestingEnvironment.getOperatorMethodHandle)

Example 40 with MapType

use of com.facebook.presto.common.type.MapType in project presto by prestodb.

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(com.facebook.presto.common.block.Block) MapType(com.facebook.presto.common.type.MapType) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) Benchmark(org.openjdk.jmh.annotations.Benchmark) OperationsPerInvocation(org.openjdk.jmh.annotations.OperationsPerInvocation)

Aggregations

MapType (com.facebook.presto.common.type.MapType)92 Type (com.facebook.presto.common.type.Type)49 ArrayType (com.facebook.presto.common.type.ArrayType)40 Test (org.testng.annotations.Test)32 RowType (com.facebook.presto.common.type.RowType)30 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)24 Block (com.facebook.presto.common.block.Block)21 HashMap (java.util.HashMap)12 DecimalType (com.facebook.presto.common.type.DecimalType)11 ImmutableList (com.google.common.collect.ImmutableList)11 List (java.util.List)11 Map (java.util.Map)11 VarcharType (com.facebook.presto.common.type.VarcharType)9 MethodHandle (java.lang.invoke.MethodHandle)9 ArrayList (java.util.ArrayList)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 SingleMapBlock (com.facebook.presto.common.block.SingleMapBlock)7 PrestoException (com.facebook.presto.spi.PrestoException)7 OperatorType (com.facebook.presto.common.function.OperatorType)6 MapBlockBuilder (com.facebook.presto.common.block.MapBlockBuilder)5