use of com.facebook.presto.common.type.MapType 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.type.MapType in project presto by prestodb.
the class TestRowBasedSerialization method testMap.
@Test
public void testMap() {
MapType mapType = createMapType(BIGINT, BIGINT);
assertRoundTrip(ImmutableList.of(mapType), ImmutableList.of(createMapBlock(mapType, ImmutableMap.of(312L, 123L))));
assertRoundTrip(ImmutableList.of(mapType), ImmutableList.of(createMapBlock(mapType, singletonMap(312L, null))));
Map<Long, Long> map = new HashMap<>();
map.put(1L, null);
map.put(2L, 2L);
map.put(3L, null);
map.put(4L, 4L);
assertRoundTrip(ImmutableList.of(mapType), ImmutableList.of(createMapBlock(mapType, map)));
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class BlockAssertions method createRandomBlockForType.
// Note: Make sure positionCount is sufficiently large if nestedNullRate or primitiveNullRate is greater than 0
public static Block createRandomBlockForType(Type type, int positionCount, float primitiveNullRate, float nestedNullRate, boolean createView, List<Encoding> wrappings) {
verifyNullRate(primitiveNullRate);
verifyNullRate(nestedNullRate);
Block block = null;
if (createView) {
positionCount *= 2;
}
if (type == BOOLEAN) {
block = createRandomBooleansBlock(positionCount, primitiveNullRate);
} else if (type == BIGINT) {
block = createRandomLongsBlock(positionCount, primitiveNullRate);
} else if (type == INTEGER || type == REAL) {
block = createRandomIntsBlock(positionCount, primitiveNullRate);
} else if (type == SMALLINT) {
block = createRandomSmallintsBlock(positionCount, primitiveNullRate);
} else if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
if (decimalType.isShort()) {
block = createRandomLongsBlock(positionCount, primitiveNullRate);
} else {
block = createRandomLongDecimalsBlock(positionCount, primitiveNullRate);
}
} else if (type == VARCHAR) {
block = createRandomStringBlock(positionCount, primitiveNullRate, MAX_STRING_SIZE);
} else {
// Nested types
// Build isNull and offsets of size positionCount
boolean[] isNull = null;
if (nestedNullRate > 0) {
isNull = new boolean[positionCount];
}
int[] offsets = new int[positionCount + 1];
for (int position = 0; position < positionCount; position++) {
if (nestedNullRate > 0 && ThreadLocalRandom.current().nextDouble(1) < nestedNullRate) {
isNull[position] = true;
offsets[position + 1] = offsets[position];
} else {
offsets[position + 1] = offsets[position] + (type instanceof RowType ? 1 : ThreadLocalRandom.current().nextInt(ENTRY_SIZE) + 1);
}
}
// Build the nested block of size offsets[positionCount].
if (type instanceof ArrayType) {
Block valuesBlock = createRandomBlockForType(((ArrayType) type).getElementType(), offsets[positionCount], primitiveNullRate, nestedNullRate, createView, wrappings);
block = fromElementBlock(positionCount, Optional.ofNullable(isNull), offsets, valuesBlock);
} else if (type instanceof MapType) {
MapType mapType = (MapType) type;
Block keyBlock = createRandomBlockForType(mapType.getKeyType(), offsets[positionCount], 0.0f, 0.0f, createView, wrappings);
Block valueBlock = createRandomBlockForType(mapType.getValueType(), offsets[positionCount], primitiveNullRate, nestedNullRate, createView, wrappings);
block = mapType.createBlockFromKeyValue(positionCount, Optional.ofNullable(isNull), offsets, keyBlock, valueBlock);
} else if (type instanceof RowType) {
List<Type> fieldTypes = type.getTypeParameters();
Block[] fieldBlocks = new Block[fieldTypes.size()];
for (int i = 0; i < fieldBlocks.length; i++) {
fieldBlocks[i] = createRandomBlockForType(fieldTypes.get(i), positionCount, primitiveNullRate, nestedNullRate, createView, wrappings);
}
block = fromFieldBlocks(positionCount, Optional.ofNullable(isNull), fieldBlocks);
} else {
throw new IllegalArgumentException(format("type %s is not supported.", type));
}
}
if (createView) {
positionCount /= 2;
int offset = positionCount / 2;
block = block.getRegion(offset, positionCount);
}
if (!wrappings.isEmpty()) {
block = wrapBlock(block, positionCount, wrappings);
}
return block;
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class BlockAssertions method createMapType.
public static MapType createMapType(Type keyType, Type valueType) {
MethodHandle keyNativeEquals = getOperatorMethodHandle(OperatorType.EQUAL, keyType, keyType);
MethodHandle keyBlockEquals = compose(keyNativeEquals, nativeValueGetter(keyType), nativeValueGetter(keyType));
MethodHandle keyNativeHashCode = getOperatorMethodHandle(OperatorType.HASH_CODE, keyType);
MethodHandle keyBlockHashCode = compose(keyNativeHashCode, nativeValueGetter(keyType));
return new MapType(keyType, valueType, keyBlockEquals, keyBlockHashCode);
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class BenchmarkMapCopy method benchmarkMapCopy.
@Benchmark
@OperationsPerInvocation(POSITIONS)
public BlockBuilder benchmarkMapCopy(BenchmarkData data) {
Block block = data.getDataBlock();
BlockBuilder blockBuilder = data.getBlockBuilder();
MapType mapType = mapType(VARCHAR, BIGINT);
for (int i = 0; i < POSITIONS; i++) {
mapType.appendTo(block, i, blockBuilder);
}
return blockBuilder;
}
Aggregations