use of com.facebook.presto.common.block.SingleMapBlock 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));
}
}
use of com.facebook.presto.common.block.SingleMapBlock in project presto by prestodb.
the class TestMapBlock method testSeekKey.
@Test
public void testSeekKey() {
Block keyBlock = createStringsBlock("k");
Block valueBlock = createStringsBlock("v");
Block mapBlock = mapType(VARCHAR, VARCHAR).createBlockFromKeyValue(1, Optional.empty(), IntStream.range(0, 2).toArray(), keyBlock, valueBlock);
SingleMapBlock singleMapBlock = (SingleMapBlock) mapBlock.getBlock(0);
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));
// Testing not found case. The key to seek should be longer than AbstractVariableWidthType#EXPECTED_BYTES_PER_ENTRY(32) and has same hash code as the key in keyBlock.
// This is because the default capacity of the slice of the keyBlock for 1 single character key is EXPECTED_BYTES_PER_ENTRY. We want to make the seeked key out of boundary
// to make sure no exception is thrown.
assertEquals(singleMapBlock.seekKeyExact(Slices.utf8Slice(new String(new char[10]).replace("\0", "Doudou")), keyNativeHashCode, keyBlockNativeEquals, keyBlockHashCode), -1);
// Testing found case.
assertEquals(singleMapBlock.seekKeyExact(Slices.utf8Slice("k"), keyNativeHashCode, keyBlockNativeEquals, keyBlockHashCode), 1);
}
use of com.facebook.presto.common.block.SingleMapBlock in project presto by prestodb.
the class TestMapBlockBuilder method testAppendStructureWithHashPresent.
@Test
public void testAppendStructureWithHashPresent() {
MapBlockBuilder mapBlockBuilder = createMapBlockBuilder();
for (int i = 0; i < MAP_POSITIONS; i++) {
appendSingleEntryMap(mapBlockBuilder, i);
}
assertFalse(mapBlockBuilder.isHashTablesPresent());
MapBlockBuilder anotherBuilder = (MapBlockBuilder) mapBlockBuilder.newBlockBuilderLike(null);
MapBlock mapBlock = (MapBlock) mapBlockBuilder.build();
for (int i = 0; i < MAP_POSITIONS; i++) {
anotherBuilder.appendStructureInternal(mapBlock, i);
}
SingleMapBlock singleMapBlock = (SingleMapBlock) mapBlock.getBlock(1);
singleMapBlock.seekKeyExact(1, KEY_NATIVE_HASH_CODE, KEY_BLOCK_NATIVE_EQUALS, KEY_BLOCK_HASH_CODE);
assertFalse(anotherBuilder.isHashTablesPresent());
for (int i = 0; i < MAP_POSITIONS; i++) {
// Adding mapBlock with hash table present, forces the builder to build its hash table.
anotherBuilder.appendStructureInternal(mapBlock, i);
assertTrue(anotherBuilder.isHashTablesPresent());
// Verify the keys for them automatically.
verifyOnlyKeyInMap(anotherBuilder, MAP_POSITIONS + i, i);
}
for (int i = 0; i < MAP_POSITIONS; i++) {
verifyOnlyKeyInMap(anotherBuilder, i, i);
}
}
use of com.facebook.presto.common.block.SingleMapBlock in project presto by prestodb.
the class TestMapBlockBuilder method testMapBlockSeek.
@Test
public void testMapBlockSeek() {
MapBlockBuilder blockBuilder = createMapBlockBuilder();
for (int i = 0; i < MAP_POSITIONS; i++) {
BlockBuilder entryBuilder = blockBuilder.beginBlockEntry();
for (int j = 0; j < i; j++) {
// key
BIGINT.writeLong(entryBuilder, j);
// value
BIGINT.writeLong(entryBuilder, i);
}
blockBuilder.closeEntry();
}
assertFalse(blockBuilder.isHashTablesPresent());
MapBlock mapBlock = (MapBlock) blockBuilder.build();
assertFalse(mapBlock.isHashTablesPresent());
for (int i = 0; i < MAP_POSITIONS; i++) {
SingleMapBlock singleMapBlock = (SingleMapBlock) mapBlock.getBlock(i);
for (int j = 0; j < i; j++) {
assertEquals(singleMapBlock.seekKeyExact(j, KEY_NATIVE_HASH_CODE, KEY_BLOCK_NATIVE_EQUALS, KEY_BLOCK_HASH_CODE), j * 2 + 1);
}
assertEquals(mapBlock.isHashTablesPresent(), i > 0);
}
// Block copies the HashMap, so block builder's hash table is still not present.
assertFalse(blockBuilder.isHashTablesPresent());
}
use of com.facebook.presto.common.block.SingleMapBlock in project presto by prestodb.
the class TestObjectEncoders method testComplexObjectEncoders.
@Test
public void testComplexObjectEncoders() {
ObjectInspector inspector;
ObjectEncoder encoder;
inspector = ObjectInspectors.create(new ArrayType(BIGINT), typeManager);
encoder = createEncoder(new ArrayType(BIGINT), inspector);
assertTrue(encoder instanceof ObjectEncoders.ListObjectEncoder);
Object arrayObject = encoder.encode(new Long[] { 1L, 2L, 3L });
assertTrue(arrayObject instanceof LongArrayBlock);
assertEquals(((LongArrayBlock) arrayObject).getLong(0), 1L);
assertEquals(((LongArrayBlock) arrayObject).getLong(1), 2L);
assertEquals(((LongArrayBlock) arrayObject).getLong(2), 3L);
inspector = ObjectInspectors.create(new MapType(VARCHAR, BIGINT, methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation")), typeManager);
encoder = createEncoder(new MapType(VARCHAR, BIGINT, methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation")), inspector);
assertTrue(encoder instanceof ObjectEncoders.MapObjectEncoder);
assertTrue(encoder.encode(new HashMap<String, Long>() {
}) instanceof SingleMapBlock);
}
Aggregations