use of com.facebook.presto.common.block.SingleMapBlock in project presto by prestodb.
the class TestMapBlock method assertValue.
private static void assertValue(Block mapBlock, int position, Map<String, Long> map) {
MapType mapType = mapType(VARCHAR, BIGINT);
MethodHandle keyNativeHashCode = getOperatorMethodHandle(OperatorType.HASH_CODE, VARCHAR);
MethodHandle keyNativeEquals = getOperatorMethodHandle(OperatorType.EQUAL, VARCHAR, VARCHAR);
MethodHandle keyBlockNativeEquals = compose(keyNativeEquals, nativeValueGetter(VARCHAR));
MethodHandle keyBlockHashCode = compose(keyNativeHashCode, nativeValueGetter(VARCHAR));
// null maps are handled by assertPositionValue
requireNonNull(map, "map is null");
assertFalse(mapBlock.isNull(position));
SingleMapBlock elementBlock = (SingleMapBlock) mapType.getObject(mapBlock, position);
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.isNull(pos));
} else {
assertFalse(elementBlock.isNull(pos));
assertEquals(BIGINT.getLong(elementBlock, pos), (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.getSlice(elementBlock, i).toStringUtf8();
Long actualValue;
if (elementBlock.isNull(i + 1)) {
actualValue = null;
} else {
actualValue = BIGINT.getLong(elementBlock, i + 1);
}
assertTrue(map.containsKey(actualKey));
assertEquals(actualValue, map.get(actualKey));
}
}
Aggregations