Search in sources :

Example 16 with MapBlockBuilder

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

the class TestMapBlockBuilder method testDirectBlockEntry.

@Test
public void testDirectBlockEntry() {
    MapType innerMapType = new MapType(BIGINT, BIGINT, KEY_BLOCK_EQUALS, KEY_BLOCK_HASH_CODE);
    MapType mapType = new MapType(BIGINT, innerMapType, KEY_BLOCK_EQUALS, KEY_BLOCK_HASH_CODE);
    MapBlockBuilder blockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, MAP_POSITIONS);
    int numberOfOuterElements = 10;
    int numberOfInnerElements = 500;
    int outerKeyBase = 100;
    int keyBase = 1_000;
    int valueBase = 1_000_000;
    // Each outer map's value has 500 entries of map {long, long} (This is called inner map).
    for (int element = 0; element < MAP_POSITIONS; element++) {
        blockBuilder.beginDirectEntry();
        BlockBuilder outerKeyBuilder = blockBuilder.getKeyBlockBuilder();
        for (int outer = 0; outer < numberOfOuterElements; outer++) {
            BIGINT.writeLong(outerKeyBuilder, element * outerKeyBase + outer);
        }
        MapBlockBuilder outerValueBuilder = (MapBlockBuilder) blockBuilder.getValueBlockBuilder();
        for (int outer = 0; outer < numberOfOuterElements; outer++) {
            outerValueBuilder.beginDirectEntry();
            BlockBuilder innerKeyBuilder = outerValueBuilder.getKeyBlockBuilder();
            for (int inner = 0; inner < numberOfInnerElements; inner++) {
                BIGINT.writeLong(innerKeyBuilder, inner + outer * keyBase);
            }
            BlockBuilder innerValueBuilder = outerValueBuilder.getValueBlockBuilder();
            for (int inner = 0; inner < numberOfInnerElements; inner++) {
                BIGINT.writeLong(innerValueBuilder, inner + outer * valueBase);
            }
            outerValueBuilder.closeEntry();
        }
        blockBuilder.closeEntry();
    }
    assertEquals(blockBuilder.getPositionCount(), MAP_POSITIONS);
    for (int element = 0; element < blockBuilder.getPositionCount(); element++) {
        SingleMapBlock outerBlock = (SingleMapBlock) blockBuilder.getBlock(element);
        assertEquals(outerBlock.getPositionCount(), numberOfOuterElements * 2);
        for (int outer = 0; outer < numberOfOuterElements; outer++) {
            assertEquals(outerBlock.getLong(outer * 2), (long) element * outerKeyBase + outer);
            SingleMapBlock innerValueBlock = (SingleMapBlock) outerBlock.getBlock(outer * 2 + 1);
            assertEquals(innerValueBlock.getPositionCount(), numberOfInnerElements * 2);
            for (int inner = 0; inner < numberOfInnerElements; inner++) {
                assertEquals(innerValueBlock.getLong(inner * 2), (long) outer * keyBase + inner);
                assertEquals(innerValueBlock.getLong(inner * 2 + 1), (long) outer * valueBase + inner);
            }
        }
    }
}
Also used : MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) MapType(com.facebook.presto.common.type.MapType) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) Test(org.testng.annotations.Test)

Example 17 with MapBlockBuilder

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

the class MapConstructor method createMap.

@UsedByGeneratedCode
public static Block createMap(MapType mapType, Type keyType, Type valueType, MethodHandle keyBlockEqual, MethodHandle keyBlockHashCode, MethodHandle keyIndeterminate, SqlFunctionProperties properties, Block keyBlock, Block valueBlock) {
    checkCondition(keyBlock.getPositionCount() == valueBlock.getPositionCount(), INVALID_FUNCTION_ARGUMENT, "Key and value arrays must be the same length");
    MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, keyBlock.getPositionCount() * 2);
    BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
    for (int i = 0; i < keyBlock.getPositionCount(); i++) {
        if (keyBlock.isNull(i)) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
        }
        if (keyType.getJavaType() == Block.class) {
            // If it's nto primitive or string, we need to look for nulls in the block.
            Object keyObject = readNativeValue(mapType.getKeyType(), keyBlock, i);
            try {
                if ((boolean) keyIndeterminate.invoke(keyObject, false)) {
                    throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be indeterminate: " + mapType.getKeyType().getObjectValue(properties, keyBlock, i));
                }
            } catch (Throwable t) {
                throw internalError(t);
            }
        }
        keyType.appendTo(keyBlock, i, blockBuilder);
        valueType.appendTo(valueBlock, i, blockBuilder);
    }
    try {
        mapBlockBuilder.closeEntryStrict(keyBlockEqual, keyBlockHashCode);
    } catch (DuplicateMapKeyException e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e.getDetailedMessage(mapType.getKeyType(), properties), e);
    } catch (NotSupportedException e) {
        throw new PrestoException(NOT_SUPPORTED, e.getMessage(), e);
    }
    return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
Also used : MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) DuplicateMapKeyException(com.facebook.presto.common.block.DuplicateMapKeyException) PrestoException(com.facebook.presto.spi.PrestoException) NotSupportedException(com.facebook.presto.common.NotSupportedException) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 18 with MapBlockBuilder

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

the class BenchmarkMapBlockBuilder method directBuilder.

@Benchmark
public void directBuilder(BenchmarkData data) {
    MapBlockBuilder mapBlockBuilder = data.getMapBlockBuilder();
    int positionCount = data.getPositionCount();
    int outerElementCount = data.getOuterElementCount();
    int innerElementCount = data.getInnerElementCount();
    for (int position = 0; position < positionCount; position++) {
        mapBlockBuilder.beginDirectEntry();
        BlockBuilder outerKeyBuilder = mapBlockBuilder.getKeyBlockBuilder();
        for (int outer = 0; outer < outerElementCount; outer++) {
            BIGINT.writeLong(outerKeyBuilder, outer);
        }
        MapBlockBuilder outerValueBuilder = (MapBlockBuilder) mapBlockBuilder.getValueBlockBuilder();
        for (int outer = 0; outer < outerElementCount; outer++) {
            outerValueBuilder.beginDirectEntry();
            BlockBuilder innerKeyBuilder = outerValueBuilder.getKeyBlockBuilder();
            for (int inner = 0; inner < innerElementCount; inner++) {
                BIGINT.writeLong(innerKeyBuilder, inner * 2L);
            }
            BlockBuilder innerValueBuilder = outerValueBuilder.getValueBlockBuilder();
            for (int inner = 0; inner < innerElementCount; inner++) {
                BIGINT.writeLong(innerValueBuilder, inner * 2L + 1);
            }
            outerValueBuilder.closeEntry();
        }
        mapBlockBuilder.closeEntry();
    }
}
Also used : MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 19 with MapBlockBuilder

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

the class BenchmarkMapBlockBuilder method blockBuilder.

@Benchmark
public void blockBuilder(BenchmarkData data) {
    MapBlockBuilder mapBlockBuilder = data.getMapBlockBuilder();
    int positionCount = data.getPositionCount();
    int outerElementCount = data.getOuterElementCount();
    int innerElementCount = data.getInnerElementCount();
    for (int position = 0; position < positionCount; position++) {
        BlockBuilder outerBlockBuilder = mapBlockBuilder.beginBlockEntry();
        for (int outer = 0; outer < outerElementCount; outer++) {
            BIGINT.writeLong(outerBlockBuilder, outer);
            BlockBuilder innerBlockBuilder = outerBlockBuilder.beginBlockEntry();
            for (int inner = 0; inner < innerElementCount; inner++) {
                // inner key
                BIGINT.writeLong(innerBlockBuilder, inner * 2L);
                // inner key
                BIGINT.writeLong(innerBlockBuilder, inner * 2L + 1);
            }
            outerBlockBuilder.closeEntry();
        }
        mapBlockBuilder.closeEntry();
    }
}
Also used : MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 20 with MapBlockBuilder

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

the class TestDirectEntryBlockBuilder method testNestedMap.

@Test
public void testNestedMap() {
    MapType innerMap = new MapType(BIGINT, VARCHAR, MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"));
    ArrayType arrayType = new ArrayType(innerMap);
    MapType mapType = new MapType(BIGINT, arrayType, MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"));
    MapBlockBuilder beginEntryBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, POSITION_COUNT);
    for (int i = 0; i < POSITION_COUNT; i++) {
        BlockBuilder mapWriter = beginEntryBlockBuilder.beginBlockEntry();
        for (int j = 0; j < 7; j++) {
            // Key
            BIGINT.writeLong(mapWriter, i * 7 + j);
            BlockBuilder arrayWriter = mapWriter.beginBlockEntry();
            // Map contains 4 elements.
            for (int k = 0; k < 10; k++) {
                BlockBuilder innerMapWriter = arrayWriter.beginBlockEntry();
                for (int l = 0; l < 3; l++) {
                    BIGINT.writeLong(innerMapWriter, k * 10 + l);
                    VARCHAR.writeSlice(innerMapWriter, utf8Slice("Value" + l));
                }
                arrayWriter.closeEntry();
                arrayWriter.appendNull();
            }
            mapWriter.closeEntry();
        }
        beginEntryBlockBuilder.closeEntry();
    }
    MapBlockBuilder directEntryBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, POSITION_COUNT);
    for (int i = 0; i < POSITION_COUNT; i++) {
        directEntryBlockBuilder.beginDirectEntry();
        BlockBuilder keyBuilder = directEntryBlockBuilder.getKeyBlockBuilder();
        ArrayBlockBuilder arrayBuilder = (ArrayBlockBuilder) directEntryBlockBuilder.getValueBlockBuilder();
        MapBlockBuilder innerMapBuilder = (MapBlockBuilder) arrayBuilder.getElementBlockBuilder();
        BlockBuilder innerMapKeyBuilder = innerMapBuilder.getKeyBlockBuilder();
        BlockBuilder innerMapValueBuilder = innerMapBuilder.getValueBlockBuilder();
        for (int j = 0; j < 7; j++) {
            // Key
            BIGINT.writeLong(keyBuilder, i * 7 + j);
            arrayBuilder.beginDirectEntry();
            // Map contains 4 elements.
            for (int k = 0; k < 10; k++) {
                innerMapBuilder.beginDirectEntry();
                for (int l = 0; l < 3; l++) {
                    BIGINT.writeLong(innerMapKeyBuilder, k * 10 + l);
                    VARCHAR.writeSlice(innerMapValueBuilder, utf8Slice("Value" + l));
                }
                innerMapBuilder.closeEntry();
                innerMapBuilder.appendNull();
            }
            arrayBuilder.closeEntry();
        }
        directEntryBlockBuilder.closeEntry();
    }
    Slice beginEntrySlice = getSlilce(beginEntryBlockBuilder);
    Slice directEntrySlice = getSlilce(directEntryBlockBuilder);
    assertEquals(beginEntrySlice.compareTo(directEntrySlice), 0);
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) MapType(com.facebook.presto.common.type.MapType) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder) Test(org.testng.annotations.Test)

Aggregations

MapBlockBuilder (com.facebook.presto.common.block.MapBlockBuilder)21 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)14 Test (org.testng.annotations.Test)13 SingleMapBlock (com.facebook.presto.common.block.SingleMapBlock)8 AbstractMapBlock (com.facebook.presto.common.block.AbstractMapBlock)6 MapBlock (com.facebook.presto.common.block.MapBlock)6 MapType (com.facebook.presto.common.type.MapType)5 Slice (io.airlift.slice.Slice)5 ArrayBlockBuilder (com.facebook.presto.common.block.ArrayBlockBuilder)3 Block (com.facebook.presto.common.block.Block)3 RowBlockBuilder (com.facebook.presto.common.block.RowBlockBuilder)3 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)3 ColumnarMap (com.facebook.presto.common.block.ColumnarMap)2 ArrayType (com.facebook.presto.common.type.ArrayType)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Benchmark (org.openjdk.jmh.annotations.Benchmark)2 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)1 BlockAssertions.createLongDictionaryBlock (com.facebook.presto.block.BlockAssertions.createLongDictionaryBlock)1 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)1