Search in sources :

Example 1 with MapBlock

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

the class TestMapUnionSumResult method testAddToKeySet.

@Test
public void testAddToKeySet() {
    MapType mapType = createMapType(BIGINT, BIGINT);
    Map<Long, Long> map = new HashMap<>();
    map.put(1L, 1L);
    MapBlock mapBlock = (MapBlock) createMapBlock(mapType, map);
    Block singleMapBlock = mapBlock.getBlock(0);
    MapUnionSumState mapUnionSumState = new MapUnionSumStateFactory(BIGINT, BIGINT).createSingleState();
    MapUnionSumResult mapUnionSumResult = MapUnionSumResult.create(BIGINT, BIGINT, mapUnionSumState.getAdder(), singleMapBlock);
    TypedSet typedSet = new TypedSet(BIGINT, 1, "TEST");
    Block block = createLongsBlock(-1);
    typedSet.add(block, 0);
    assertEquals(typedSet.size(), 1);
    assertEquals(mapUnionSumResult.size(), 1);
    mapUnionSumResult.unionSum(mapUnionSumResult).addKeyToSet(typedSet, 0);
    assertEquals(typedSet.size(), 2);
    assertEquals(mapUnionSumResult.size(), 1);
}
Also used : MapUnionSumStateFactory(com.facebook.presto.operator.aggregation.state.MapUnionSumStateFactory) HashMap(java.util.HashMap) MapUnionSumState(com.facebook.presto.operator.aggregation.state.MapUnionSumState) BlockAssertions.createLongsBlock(com.facebook.presto.block.BlockAssertions.createLongsBlock) MapBlock(com.facebook.presto.common.block.MapBlock) BlockAssertions.createMapBlock(com.facebook.presto.block.BlockAssertions.createMapBlock) Block(com.facebook.presto.common.block.Block) MapBlock(com.facebook.presto.common.block.MapBlock) BlockAssertions.createMapBlock(com.facebook.presto.block.BlockAssertions.createMapBlock) BlockAssertions.createMapType(com.facebook.presto.block.BlockAssertions.createMapType) MapType(com.facebook.presto.common.type.MapType) Test(org.testng.annotations.Test)

Example 2 with MapBlock

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

the class MapType method hash.

@Override
public long hash(Block block, int position) {
    Block mapBlock = getObject(block, position);
    long result = 0;
    for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
        result += hashPosition(keyType, mapBlock, i) ^ hashPosition(valueType, mapBlock, i + 1);
    }
    return result;
}
Also used : SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) MapBlock(com.facebook.presto.common.block.MapBlock) Block(com.facebook.presto.common.block.Block)

Example 3 with MapBlock

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

the class OptimizedPartitionedOutputOperator method decodeBlock.

/**
 * Flatten the block and convert the nested-typed block into ColumnarArray/Map/Row.
 * For performance considerations we decode the block only once for each block instead of for each batch.
 *
 * @return A tree structure that contains the decoded block
 */
@VisibleForTesting
static DecodedBlockNode decodeBlock(BlockFlattener flattener, Closer blockLeaseCloser, Block block) {
    BlockLease lease = flattener.flatten(block);
    blockLeaseCloser.register(lease::close);
    Block decodedBlock = lease.get();
    long estimatedSizeInBytes = decodedBlock.getLogicalSizeInBytes();
    if (decodedBlock instanceof ArrayBlock) {
        ColumnarArray columnarArray = ColumnarArray.toColumnarArray(decodedBlock);
        Block childBlock = columnarArray.getElementsBlock();
        return new DecodedBlockNode(columnarArray, ImmutableList.of(decodeBlock(flattener, blockLeaseCloser, childBlock)), columnarArray.getRetainedSizeInBytes(), estimatedSizeInBytes);
    }
    if (decodedBlock instanceof MapBlock) {
        ColumnarMap columnarMap = ColumnarMap.toColumnarMap(decodedBlock);
        Block keyBlock = columnarMap.getKeysBlock();
        Block valueBlock = columnarMap.getValuesBlock();
        return new DecodedBlockNode(columnarMap, ImmutableList.of(decodeBlock(flattener, blockLeaseCloser, keyBlock), decodeBlock(flattener, blockLeaseCloser, valueBlock)), columnarMap.getRetainedSizeInBytes(), estimatedSizeInBytes);
    }
    if (decodedBlock instanceof RowBlock) {
        ColumnarRow columnarRow = ColumnarRow.toColumnarRow(decodedBlock);
        ImmutableList.Builder<DecodedBlockNode> children = ImmutableList.builder();
        for (int i = 0; i < columnarRow.getFieldCount(); i++) {
            Block childBlock = columnarRow.getField(i);
            children.add(decodeBlock(flattener, blockLeaseCloser, childBlock));
        }
        return new DecodedBlockNode(columnarRow, children.build(), columnarRow.getRetainedSizeInBytes(), estimatedSizeInBytes);
    }
    if (decodedBlock instanceof DictionaryBlock) {
        Block dictionary = ((DictionaryBlock) decodedBlock).getDictionary();
        return new DecodedBlockNode(decodedBlock, ImmutableList.of(decodeBlock(flattener, blockLeaseCloser, dictionary)), decodedBlock.getRetainedSizeInBytes(), estimatedSizeInBytes);
    }
    if (decodedBlock instanceof RunLengthEncodedBlock) {
        Block childBlock = ((RunLengthEncodedBlock) decodedBlock).getValue();
        return new DecodedBlockNode(decodedBlock, ImmutableList.of(decodeBlock(flattener, blockLeaseCloser, childBlock)), decodedBlock.getRetainedSizeInBytes(), estimatedSizeInBytes);
    }
    return new DecodedBlockNode(decodedBlock, ImmutableList.of(), block.getRetainedSizeInBytes(), estimatedSizeInBytes);
}
Also used : ColumnarArray(com.facebook.presto.common.block.ColumnarArray) BlockLease(com.facebook.presto.common.block.BlockLease) ArrayBlock(com.facebook.presto.common.block.ArrayBlock) ImmutableList(com.google.common.collect.ImmutableList) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) RowBlock(com.facebook.presto.common.block.RowBlock) ColumnarRow(com.facebook.presto.common.block.ColumnarRow) ColumnarMap(com.facebook.presto.common.block.ColumnarMap) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) MapBlock(com.facebook.presto.common.block.MapBlock) RowBlock(com.facebook.presto.common.block.RowBlock) ArrayBlock(com.facebook.presto.common.block.ArrayBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) Block(com.facebook.presto.common.block.Block) MapBlock(com.facebook.presto.common.block.MapBlock) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with MapBlock

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

the class TestMapBlock method verifyMapRegion.

private void verifyMapRegion(Map<String, Long>[] testValues, AbstractMapBlock block, BlockBuilder blockBuilder, int offset, int length) {
    boolean isHashTablePresent = block.isHashTablesPresent();
    MapBlock region = (MapBlock) block.getRegion(offset, length);
    assertEquals(region.isHashTablesPresent(), isHashTablePresent);
    assertBlock(region, () -> blockBuilder.newBlockBuilderLike(null), Arrays.copyOfRange(testValues, offset, offset + length));
    assertTrue(region.isHashTablesPresent());
}
Also used : AbstractMapBlock(com.facebook.presto.common.block.AbstractMapBlock) MapBlock(com.facebook.presto.common.block.MapBlock) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock)

Example 5 with MapBlock

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

the class TestMapBlock method testSingleValueBlock.

@Test
public void testSingleValueBlock() {
    // 1 entry map.
    Map<String, Long>[] values = createTestMap(50);
    BlockBuilder mapBlockBuilder = createBlockBuilderWithValues(values);
    Block mapBlock = mapBlockBuilder.build();
    assertSame(mapBlock, mapBlock.getSingleValueBlock(0));
    assertNotSame(mapBlockBuilder, mapBlockBuilder.getSingleValueBlock(0));
    // 2 entries map.
    values = createTestMap(50, 50);
    mapBlockBuilder = createBlockBuilderWithValues(values);
    mapBlock = mapBlockBuilder.build();
    Block firstElement = mapBlock.getRegion(0, 1);
    assertNotSame(firstElement, firstElement.getSingleValueBlock(0));
    Block secondElementCopy = mapBlock.copyRegion(1, 1);
    assertSame(secondElementCopy, secondElementCopy.getSingleValueBlock(0));
    // Test with null elements.
    values = new Map[] { null };
    mapBlockBuilder = createBlockBuilderWithValues(values);
    mapBlock = mapBlockBuilder.build();
    assertSame(mapBlock, mapBlock.getSingleValueBlock(0));
    assertNotSame(mapBlock, mapBlockBuilder.getSingleValueBlock(0));
    // Test with 2 null elements.
    values = new Map[] { null, null };
    mapBlockBuilder = createBlockBuilderWithValues(values);
    mapBlock = mapBlockBuilder.build();
    assertNotSame(mapBlock, mapBlock.getSingleValueBlock(0));
}
Also used : BlockAssertions.createLongsBlock(com.facebook.presto.block.BlockAssertions.createLongsBlock) AbstractMapBlock(com.facebook.presto.common.block.AbstractMapBlock) BlockAssertions.createRandomLongsBlock(com.facebook.presto.block.BlockAssertions.createRandomLongsBlock) MapBlock(com.facebook.presto.common.block.MapBlock) BlockAssertions.createRLEBlock(com.facebook.presto.block.BlockAssertions.createRLEBlock) ByteArrayBlock(com.facebook.presto.common.block.ByteArrayBlock) BlockAssertions.createStringsBlock(com.facebook.presto.block.BlockAssertions.createStringsBlock) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) BlockAssertions.createLongDictionaryBlock(com.facebook.presto.block.BlockAssertions.createLongDictionaryBlock) Block(com.facebook.presto.common.block.Block) HashMap(java.util.HashMap) Map(java.util.Map) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) Test(org.testng.annotations.Test)

Aggregations

MapBlock (com.facebook.presto.common.block.MapBlock)12 SingleMapBlock (com.facebook.presto.common.block.SingleMapBlock)10 AbstractMapBlock (com.facebook.presto.common.block.AbstractMapBlock)9 Test (org.testng.annotations.Test)9 Block (com.facebook.presto.common.block.Block)7 MapBlockBuilder (com.facebook.presto.common.block.MapBlockBuilder)6 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)4 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)4 BlockAssertions.createLongDictionaryBlock (com.facebook.presto.block.BlockAssertions.createLongDictionaryBlock)3 BlockAssertions.createRLEBlock (com.facebook.presto.block.BlockAssertions.createRLEBlock)3 BlockAssertions.createRandomLongsBlock (com.facebook.presto.block.BlockAssertions.createRandomLongsBlock)3 BlockAssertions.createStringsBlock (com.facebook.presto.block.BlockAssertions.createStringsBlock)3 ByteArrayBlock (com.facebook.presto.common.block.ByteArrayBlock)3 ColumnarMap (com.facebook.presto.common.block.ColumnarMap)2 HashMap (java.util.HashMap)2 BlockAssertions.createMapBlock (com.facebook.presto.block.BlockAssertions.createMapBlock)1 BlockAssertions.createMapType (com.facebook.presto.block.BlockAssertions.createMapType)1 ArrayBlock (com.facebook.presto.common.block.ArrayBlock)1 BlockLease (com.facebook.presto.common.block.BlockLease)1 ColumnarArray (com.facebook.presto.common.block.ColumnarArray)1