use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class TestMapBlockBuilder method testDirectBlockEntry.
@Test
public void testDirectBlockEntry() {
MapType innerMapType = new MapType(BIGINT, BIGINT, KEY_BLOCK_EQUALS, KEY_BLOCK_HASH_CODE);
MapType mapType = new MapType(BIGINT, innerMapType, KEY_BLOCK_EQUALS, KEY_BLOCK_HASH_CODE);
MapBlockBuilder blockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, MAP_POSITIONS);
int numberOfOuterElements = 10;
int numberOfInnerElements = 500;
int outerKeyBase = 100;
int keyBase = 1_000;
int valueBase = 1_000_000;
// Each outer map's value has 500 entries of map {long, long} (This is called inner map).
for (int element = 0; element < MAP_POSITIONS; element++) {
blockBuilder.beginDirectEntry();
BlockBuilder outerKeyBuilder = blockBuilder.getKeyBlockBuilder();
for (int outer = 0; outer < numberOfOuterElements; outer++) {
BIGINT.writeLong(outerKeyBuilder, element * outerKeyBase + outer);
}
MapBlockBuilder outerValueBuilder = (MapBlockBuilder) blockBuilder.getValueBlockBuilder();
for (int outer = 0; outer < numberOfOuterElements; outer++) {
outerValueBuilder.beginDirectEntry();
BlockBuilder innerKeyBuilder = outerValueBuilder.getKeyBlockBuilder();
for (int inner = 0; inner < numberOfInnerElements; inner++) {
BIGINT.writeLong(innerKeyBuilder, inner + outer * keyBase);
}
BlockBuilder innerValueBuilder = outerValueBuilder.getValueBlockBuilder();
for (int inner = 0; inner < numberOfInnerElements; inner++) {
BIGINT.writeLong(innerValueBuilder, inner + outer * valueBase);
}
outerValueBuilder.closeEntry();
}
blockBuilder.closeEntry();
}
assertEquals(blockBuilder.getPositionCount(), MAP_POSITIONS);
for (int element = 0; element < blockBuilder.getPositionCount(); element++) {
SingleMapBlock outerBlock = (SingleMapBlock) blockBuilder.getBlock(element);
assertEquals(outerBlock.getPositionCount(), numberOfOuterElements * 2);
for (int outer = 0; outer < numberOfOuterElements; outer++) {
assertEquals(outerBlock.getLong(outer * 2), (long) element * outerKeyBase + outer);
SingleMapBlock innerValueBlock = (SingleMapBlock) outerBlock.getBlock(outer * 2 + 1);
assertEquals(innerValueBlock.getPositionCount(), numberOfInnerElements * 2);
for (int inner = 0; inner < numberOfInnerElements; inner++) {
assertEquals(innerValueBlock.getLong(inner * 2), (long) outer * keyBase + inner);
assertEquals(innerValueBlock.getLong(inner * 2 + 1), (long) outer * valueBase + inner);
}
}
}
}
use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class MapConstructor method createMap.
@UsedByGeneratedCode
public static Block createMap(MapType mapType, Type keyType, Type valueType, MethodHandle keyBlockEqual, MethodHandle keyBlockHashCode, MethodHandle keyIndeterminate, SqlFunctionProperties properties, Block keyBlock, Block valueBlock) {
checkCondition(keyBlock.getPositionCount() == valueBlock.getPositionCount(), INVALID_FUNCTION_ARGUMENT, "Key and value arrays must be the same length");
MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, keyBlock.getPositionCount() * 2);
BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
for (int i = 0; i < keyBlock.getPositionCount(); i++) {
if (keyBlock.isNull(i)) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
}
if (keyType.getJavaType() == Block.class) {
// If it's nto primitive or string, we need to look for nulls in the block.
Object keyObject = readNativeValue(mapType.getKeyType(), keyBlock, i);
try {
if ((boolean) keyIndeterminate.invoke(keyObject, false)) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be indeterminate: " + mapType.getKeyType().getObjectValue(properties, keyBlock, i));
}
} catch (Throwable t) {
throw internalError(t);
}
}
keyType.appendTo(keyBlock, i, blockBuilder);
valueType.appendTo(valueBlock, i, blockBuilder);
}
try {
mapBlockBuilder.closeEntryStrict(keyBlockEqual, keyBlockHashCode);
} catch (DuplicateMapKeyException e) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e.getDetailedMessage(mapType.getKeyType(), properties), e);
} catch (NotSupportedException e) {
throw new PrestoException(NOT_SUPPORTED, e.getMessage(), e);
}
return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class BenchmarkMapBlockBuilder method directBuilder.
@Benchmark
public void directBuilder(BenchmarkData data) {
MapBlockBuilder mapBlockBuilder = data.getMapBlockBuilder();
int positionCount = data.getPositionCount();
int outerElementCount = data.getOuterElementCount();
int innerElementCount = data.getInnerElementCount();
for (int position = 0; position < positionCount; position++) {
mapBlockBuilder.beginDirectEntry();
BlockBuilder outerKeyBuilder = mapBlockBuilder.getKeyBlockBuilder();
for (int outer = 0; outer < outerElementCount; outer++) {
BIGINT.writeLong(outerKeyBuilder, outer);
}
MapBlockBuilder outerValueBuilder = (MapBlockBuilder) mapBlockBuilder.getValueBlockBuilder();
for (int outer = 0; outer < outerElementCount; outer++) {
outerValueBuilder.beginDirectEntry();
BlockBuilder innerKeyBuilder = outerValueBuilder.getKeyBlockBuilder();
for (int inner = 0; inner < innerElementCount; inner++) {
BIGINT.writeLong(innerKeyBuilder, inner * 2L);
}
BlockBuilder innerValueBuilder = outerValueBuilder.getValueBlockBuilder();
for (int inner = 0; inner < innerElementCount; inner++) {
BIGINT.writeLong(innerValueBuilder, inner * 2L + 1);
}
outerValueBuilder.closeEntry();
}
mapBlockBuilder.closeEntry();
}
}
use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class BenchmarkMapBlockBuilder method blockBuilder.
@Benchmark
public void blockBuilder(BenchmarkData data) {
MapBlockBuilder mapBlockBuilder = data.getMapBlockBuilder();
int positionCount = data.getPositionCount();
int outerElementCount = data.getOuterElementCount();
int innerElementCount = data.getInnerElementCount();
for (int position = 0; position < positionCount; position++) {
BlockBuilder outerBlockBuilder = mapBlockBuilder.beginBlockEntry();
for (int outer = 0; outer < outerElementCount; outer++) {
BIGINT.writeLong(outerBlockBuilder, outer);
BlockBuilder innerBlockBuilder = outerBlockBuilder.beginBlockEntry();
for (int inner = 0; inner < innerElementCount; inner++) {
// inner key
BIGINT.writeLong(innerBlockBuilder, inner * 2L);
// inner key
BIGINT.writeLong(innerBlockBuilder, inner * 2L + 1);
}
outerBlockBuilder.closeEntry();
}
mapBlockBuilder.closeEntry();
}
}
use of com.facebook.presto.common.block.MapBlockBuilder in project presto by prestodb.
the class TestDirectEntryBlockBuilder method testNestedMap.
@Test
public void testNestedMap() {
MapType innerMap = new MapType(BIGINT, VARCHAR, MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"));
ArrayType arrayType = new ArrayType(innerMap);
MapType mapType = new MapType(BIGINT, arrayType, MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"));
MapBlockBuilder beginEntryBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, POSITION_COUNT);
for (int i = 0; i < POSITION_COUNT; i++) {
BlockBuilder mapWriter = beginEntryBlockBuilder.beginBlockEntry();
for (int j = 0; j < 7; j++) {
// Key
BIGINT.writeLong(mapWriter, i * 7 + j);
BlockBuilder arrayWriter = mapWriter.beginBlockEntry();
// Map contains 4 elements.
for (int k = 0; k < 10; k++) {
BlockBuilder innerMapWriter = arrayWriter.beginBlockEntry();
for (int l = 0; l < 3; l++) {
BIGINT.writeLong(innerMapWriter, k * 10 + l);
VARCHAR.writeSlice(innerMapWriter, utf8Slice("Value" + l));
}
arrayWriter.closeEntry();
arrayWriter.appendNull();
}
mapWriter.closeEntry();
}
beginEntryBlockBuilder.closeEntry();
}
MapBlockBuilder directEntryBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, POSITION_COUNT);
for (int i = 0; i < POSITION_COUNT; i++) {
directEntryBlockBuilder.beginDirectEntry();
BlockBuilder keyBuilder = directEntryBlockBuilder.getKeyBlockBuilder();
ArrayBlockBuilder arrayBuilder = (ArrayBlockBuilder) directEntryBlockBuilder.getValueBlockBuilder();
MapBlockBuilder innerMapBuilder = (MapBlockBuilder) arrayBuilder.getElementBlockBuilder();
BlockBuilder innerMapKeyBuilder = innerMapBuilder.getKeyBlockBuilder();
BlockBuilder innerMapValueBuilder = innerMapBuilder.getValueBlockBuilder();
for (int j = 0; j < 7; j++) {
// Key
BIGINT.writeLong(keyBuilder, i * 7 + j);
arrayBuilder.beginDirectEntry();
// Map contains 4 elements.
for (int k = 0; k < 10; k++) {
innerMapBuilder.beginDirectEntry();
for (int l = 0; l < 3; l++) {
BIGINT.writeLong(innerMapKeyBuilder, k * 10 + l);
VARCHAR.writeSlice(innerMapValueBuilder, utf8Slice("Value" + l));
}
innerMapBuilder.closeEntry();
innerMapBuilder.appendNull();
}
arrayBuilder.closeEntry();
}
directEntryBlockBuilder.closeEntry();
}
Slice beginEntrySlice = getSlilce(beginEntryBlockBuilder);
Slice directEntrySlice = getSlilce(directEntryBlockBuilder);
assertEquals(beginEntrySlice.compareTo(directEntrySlice), 0);
}
Aggregations