use of io.trino.spi.type.MapType 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.type.MapType in project trino by trinodb.
the class TestMapBlock method assertValue.
private void assertValue(Block mapBlock, int position, Map<String, Long> map) {
MapType mapType = mapType(VARCHAR, BIGINT);
// 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()));
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)), -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));
}
}
use of io.trino.spi.type.MapType 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.type.MapType in project trino by trinodb.
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;
}
use of io.trino.spi.type.MapType in project trino by trinodb.
the class AvroDecoderTestUtil method checkMapValues.
public static void checkMapValues(Block block, Type type, Object value) {
assertNotNull(type, "Type is null");
assertTrue(type instanceof MapType, "Unexpected type");
assertTrue(((MapType) type).getKeyType() instanceof VarcharType, "Unexpected key type");
assertNotNull(block, "Block is null");
assertNotNull(value, "Value is null");
Map<String, ?> expected = (Map<String, ?>) value;
assertEquals(block.getPositionCount(), expected.size() * 2);
Type valueType = ((MapType) type).getValueType();
if (valueType instanceof ArrayType) {
for (int index = 0; index < block.getPositionCount(); index += 2) {
String actualKey = VARCHAR.getSlice(block, index).toStringUtf8();
assertTrue(expected.containsKey(actualKey));
if (block.isNull(index + 1)) {
assertNull(expected.get(actualKey));
continue;
}
Block arrayBlock = block.getObject(index + 1, Block.class);
checkArrayValues(arrayBlock, valueType, expected.get(actualKey));
}
} else if (valueType instanceof MapType) {
for (int index = 0; index < block.getPositionCount(); index += 2) {
String actualKey = VARCHAR.getSlice(block, index).toStringUtf8();
assertTrue(expected.containsKey(actualKey));
if (block.isNull(index + 1)) {
assertNull(expected.get(actualKey));
continue;
}
Block mapBlock = block.getObject(index + 1, Block.class);
checkMapValues(mapBlock, valueType, expected.get(actualKey));
}
} else if (valueType instanceof RowType) {
for (int index = 0; index < block.getPositionCount(); index += 2) {
String actualKey = VARCHAR.getSlice(block, index).toStringUtf8();
assertTrue(expected.containsKey(actualKey));
if (block.isNull(index + 1)) {
assertNull(expected.get(actualKey));
continue;
}
Block rowBlock = block.getObject(index + 1, Block.class);
checkRowValues(rowBlock, valueType, expected.get(actualKey));
}
} else {
for (int index = 0; index < block.getPositionCount(); index += 2) {
String actualKey = VARCHAR.getSlice(block, index).toStringUtf8();
assertTrue(expected.containsKey(actualKey));
checkPrimitiveValue(getObjectValue(valueType, block, index + 1), expected.get(actualKey));
}
}
}
Aggregations