use of io.trino.spi.block.MapBlockBuilder in project trino by trinodb.
the class TestMapBlock method testCloseEntryStrict.
@Test
public void testCloseEntryStrict() throws Exception {
MapType mapType = mapType(BIGINT, BIGINT);
MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, 1);
// Add 100 maps with only one entry but the same key
for (int i = 0; i < 100; i++) {
BlockBuilder entryBuilder = mapBlockBuilder.beginBlockEntry();
BIGINT.writeLong(entryBuilder, 1);
BIGINT.writeLong(entryBuilder, -1);
mapBlockBuilder.closeEntry();
}
BlockBuilder entryBuilder = mapBlockBuilder.beginBlockEntry();
// The purpose of this test is to make sure offset is calculated correctly in MapBlockBuilder.closeEntryStrict()
for (int i = 0; i < 50; i++) {
BIGINT.writeLong(entryBuilder, i);
BIGINT.writeLong(entryBuilder, -1);
}
mapBlockBuilder.closeEntryStrict();
}
use of io.trino.spi.block.MapBlockBuilder in project trino by trinodb.
the class MapConstructor method createMap.
@UsedByGeneratedCode
public static Block createMap(MapType mapType, MethodHandle keyIndeterminate, State state, ConnectorSession session, Block keyBlock, Block valueBlock) {
checkCondition(keyBlock.getPositionCount() == valueBlock.getPositionCount(), INVALID_FUNCTION_ARGUMENT, "Key and value arrays must be the same length");
PageBuilder pageBuilder = state.getPageBuilder();
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) pageBuilder.getBlockBuilder(0);
mapBlockBuilder.strict();
BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
for (int i = 0; i < keyBlock.getPositionCount(); i++) {
if (keyBlock.isNull(i)) {
// close block builder before throwing as we may be in a TRY() call
// so that subsequent calls do not find it in an inconsistent state
mapBlockBuilder.closeEntry();
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
}
Object keyObject = readNativeValue(mapType.getKeyType(), keyBlock, i);
try {
if ((boolean) keyIndeterminate.invoke(keyObject)) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be indeterminate: " + mapType.getKeyType().getObjectValue(session, keyBlock, i));
}
} catch (Throwable t) {
mapBlockBuilder.closeEntry();
throw internalError(t);
}
mapType.getKeyType().appendTo(keyBlock, i, blockBuilder);
mapType.getValueType().appendTo(valueBlock, i, blockBuilder);
}
try {
mapBlockBuilder.closeEntry();
} catch (DuplicateMapKeyException e) {
throw e.withDetailedMessage(mapType.getKeyType(), session);
} finally {
pageBuilder.declarePosition();
}
return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
use of io.trino.spi.block.MapBlockBuilder in project trino by trinodb.
the class TestMapBlock method testStrict.
@Test
public void testStrict() {
MapType mapType = mapType(BIGINT, BIGINT);
MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, 1);
mapBlockBuilder.strict();
// Add 100 maps with only one entry but the same key
for (int i = 0; i < 100; i++) {
BlockBuilder entryBuilder = mapBlockBuilder.beginBlockEntry();
BIGINT.writeLong(entryBuilder, 1);
BIGINT.writeLong(entryBuilder, -1);
mapBlockBuilder.closeEntry();
}
BlockBuilder entryBuilder = mapBlockBuilder.beginBlockEntry();
// The purpose of this test is to make sure offset is calculated correctly in MapBlockBuilder.closeEntryStrict()
for (int i = 0; i < 50; i++) {
BIGINT.writeLong(entryBuilder, i);
BIGINT.writeLong(entryBuilder, -1);
}
mapBlockBuilder.closeEntry();
entryBuilder = mapBlockBuilder.beginBlockEntry();
for (int i = 0; i < 2; i++) {
BIGINT.writeLong(entryBuilder, 99);
BIGINT.writeLong(entryBuilder, -1);
}
assertThatThrownBy(mapBlockBuilder::closeEntry).isInstanceOf(DuplicateMapKeyException.class).hasMessage("Duplicate map keys are not allowed");
}
use of io.trino.spi.block.MapBlockBuilder in project trino by trinodb.
the class TestMapBlock method createBlockBuilderWithValues.
private BlockBuilder createBlockBuilderWithValues(Map<String, Long>[] maps) {
MapType mapType = mapType(VARCHAR, BIGINT);
BlockBuilder mapBlockBuilder = mapType.createBlockBuilder(null, 1);
for (Map<String, Long> map : maps) {
createBlockBuilderWithValues(map, mapBlockBuilder);
}
return mapBlockBuilder;
}
use of io.trino.spi.block.MapBlockBuilder in project trino by trinodb.
the class TestMapBlock method createBlockBuilderWithValues.
private void createBlockBuilderWithValues(Map<String, Long> map, BlockBuilder mapBlockBuilder) {
if (map == null) {
mapBlockBuilder.appendNull();
} else {
BlockBuilder elementBlockBuilder = mapBlockBuilder.beginBlockEntry();
for (Map.Entry<String, Long> entry : map.entrySet()) {
VARCHAR.writeSlice(elementBlockBuilder, utf8Slice(entry.getKey()));
if (entry.getValue() == null) {
elementBlockBuilder.appendNull();
} else {
BIGINT.writeLong(elementBlockBuilder, entry.getValue());
}
}
mapBlockBuilder.closeEntry();
}
}
Aggregations