use of io.trino.spi.type.MapType in project trino by trinodb.
the class ParquetTester method writeValue.
private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
if (value == null) {
blockBuilder.appendNull();
} else {
if (BOOLEAN.equals(type)) {
type.writeBoolean(blockBuilder, (Boolean) value);
} else if (TINYINT.equals(type) || SMALLINT.equals(type) || INTEGER.equals(type) || BIGINT.equals(type)) {
type.writeLong(blockBuilder, ((Number) value).longValue());
} else if (Decimals.isShortDecimal(type)) {
type.writeLong(blockBuilder, ((SqlDecimal) value).getUnscaledValue().longValue());
} else if (Decimals.isLongDecimal(type)) {
if (Decimals.overflows(((SqlDecimal) value).getUnscaledValue(), MAX_PRECISION_INT64)) {
type.writeObject(blockBuilder, Int128.valueOf(((SqlDecimal) value).toBigDecimal().unscaledValue()));
} else {
type.writeObject(blockBuilder, Int128.valueOf(((SqlDecimal) value).getUnscaledValue().longValue()));
}
} else if (DOUBLE.equals(type)) {
type.writeDouble(blockBuilder, ((Number) value).doubleValue());
} else if (REAL.equals(type)) {
float floatValue = ((Number) value).floatValue();
type.writeLong(blockBuilder, Float.floatToIntBits(floatValue));
} else if (type instanceof VarcharType) {
Slice slice = truncateToLength(utf8Slice((String) value), type);
type.writeSlice(blockBuilder, slice);
} else if (type instanceof CharType) {
Slice slice = truncateToLengthAndTrimSpaces(utf8Slice((String) value), type);
type.writeSlice(blockBuilder, slice);
} else if (VARBINARY.equals(type)) {
type.writeSlice(blockBuilder, Slices.wrappedBuffer(((SqlVarbinary) value).getBytes()));
} else if (DATE.equals(type)) {
long days = ((SqlDate) value).getDays();
type.writeLong(blockBuilder, days);
} else if (TIMESTAMP_MILLIS.equals(type)) {
type.writeLong(blockBuilder, ((SqlTimestamp) value).getEpochMicros());
} else {
if (type instanceof ArrayType) {
List<?> array = (List<?>) value;
Type elementType = type.getTypeParameters().get(0);
BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
for (Object elementValue : array) {
writeValue(elementType, arrayBlockBuilder, elementValue);
}
blockBuilder.closeEntry();
} else if (type instanceof MapType) {
Map<?, ?> map = (Map<?, ?>) value;
Type keyType = type.getTypeParameters().get(0);
Type valueType = type.getTypeParameters().get(1);
BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
for (Map.Entry<?, ?> entry : map.entrySet()) {
writeValue(keyType, mapBlockBuilder, entry.getKey());
writeValue(valueType, mapBlockBuilder, entry.getValue());
}
blockBuilder.closeEntry();
} else if (type instanceof RowType) {
List<?> array = (List<?>) value;
List<Type> fieldTypes = type.getTypeParameters();
BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
for (int fieldId = 0; fieldId < fieldTypes.size(); fieldId++) {
Type fieldType = fieldTypes.get(fieldId);
writeValue(fieldType, rowBlockBuilder, array.get(fieldId));
}
blockBuilder.closeEntry();
} else {
throw new IllegalArgumentException("Unsupported type " + type);
}
}
}
}
use of io.trino.spi.type.MapType in project trino by trinodb.
the class TestTypedHistogram method testMassive.
@Test
public void testMassive() {
BlockBuilder inputBlockBuilder = BIGINT.createBlockBuilder(null, 5000);
BlockTypeOperators blockTypeOperators = new BlockTypeOperators(new TypeOperators());
TypedHistogram typedHistogram = new SingleTypedHistogram(BIGINT, blockTypeOperators.getEqualOperator(BIGINT), blockTypeOperators.getHashCodeOperator(BIGINT), 1000);
IntStream.range(1, 2000).flatMap(i -> IntStream.iterate(i, IntUnaryOperator.identity()).limit(i)).forEach(j -> BIGINT.writeLong(inputBlockBuilder, j));
Block inputBlock = inputBlockBuilder.build();
for (int i = 0; i < inputBlock.getPositionCount(); i++) {
typedHistogram.add(i, inputBlock, 1);
}
MapType mapType = mapType(BIGINT, BIGINT);
BlockBuilder out = mapType.createBlockBuilder(null, 1);
typedHistogram.serialize(out);
Block outputBlock = mapType.getObject(out, 0);
for (int i = 0; i < outputBlock.getPositionCount(); i += 2) {
assertEquals(BIGINT.getLong(outputBlock, i + 1), BIGINT.getLong(outputBlock, i));
}
}
use of io.trino.spi.type.MapType in project trino by trinodb.
the class TestHistogram method testMapHistograms.
@Test
public void testMapHistograms() {
MapType innerMapType = mapType(VARCHAR, VARCHAR);
BlockBuilder builder = innerMapType.createBlockBuilder(null, 3);
innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("a", "b")));
innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("c", "d")));
innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("e", "f")));
assertAggregation(FUNCTION_RESOLUTION, QualifiedName.of(Histogram.NAME), fromTypes(innerMapType), ImmutableMap.of(ImmutableMap.of("a", "b"), 1L, ImmutableMap.of("c", "d"), 1L, ImmutableMap.of("e", "f"), 1L), builder.build());
}
use of io.trino.spi.type.MapType in project trino by trinodb.
the class TestMapAggAggregation method testDoubleMapMap.
@Test
public void testDoubleMapMap() {
MapType innerMapType = mapType(VARCHAR, VARCHAR);
BlockBuilder builder = innerMapType.createBlockBuilder(null, 3);
innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("a", "b")));
innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("c", "d")));
innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("e", "f")));
assertAggregation(FUNCTION_RESOLUTION, QualifiedName.of(MapAggregationFunction.NAME), fromTypes(DOUBLE, innerMapType), ImmutableMap.of(1.0, ImmutableMap.of("a", "b"), 2.0, ImmutableMap.of("c", "d"), 3.0, ImmutableMap.of("e", "f")), createDoublesBlock(1.0, 2.0, 3.0), builder.build());
}
use of io.trino.spi.type.MapType in project trino by trinodb.
the class TestMapUnionAggregation method testSimpleWithDuplicates.
@Test
public void testSimpleWithDuplicates() {
MapType mapType = mapType(DOUBLE, VARCHAR);
assertAggregation(FUNCTION_RESOLUTION, QualifiedName.of(MapUnionAggregation.NAME), fromTypes(mapType), ImmutableMap.of(23.0, "aaa", 33.0, "bbb", 43.0, "ccc", 53.0, "ddd", 13.0, "eee"), arrayBlockOf(mapType, mapBlockOf(DOUBLE, VARCHAR, ImmutableMap.of(23.0, "aaa", 33.0, "bbb", 53.0, "ddd")), mapBlockOf(DOUBLE, VARCHAR, ImmutableMap.of(43.0, "ccc", 53.0, "ddd", 13.0, "eee"))));
mapType = mapType(DOUBLE, BIGINT);
assertAggregation(FUNCTION_RESOLUTION, QualifiedName.of(MapUnionAggregation.NAME), fromTypes(mapType), ImmutableMap.of(1.0, 99L, 2.0, 99L, 3.0, 99L, 4.0, 44L), arrayBlockOf(mapType, mapBlockOf(DOUBLE, BIGINT, ImmutableMap.of(1.0, 99L, 2.0, 99L, 3.0, 99L)), mapBlockOf(DOUBLE, BIGINT, ImmutableMap.of(1.0, 44L, 2.0, 44L, 4.0, 44L))));
mapType = mapType(BOOLEAN, BIGINT);
assertAggregation(FUNCTION_RESOLUTION, QualifiedName.of(MapUnionAggregation.NAME), fromTypes(mapType), ImmutableMap.of(false, 12L, true, 13L), arrayBlockOf(mapType, mapBlockOf(BOOLEAN, BIGINT, ImmutableMap.of(false, 12L)), mapBlockOf(BOOLEAN, BIGINT, ImmutableMap.of(true, 13L, false, 33L))));
}
Aggregations