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);
}
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;
}
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);
}
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());
}
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));
}
Aggregations