Search in sources :

Example 21 with MapType

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);
            }
        }
    }
}
Also used : VarcharType(io.trino.spi.type.VarcharType) SqlVarbinary(io.trino.spi.type.SqlVarbinary) HiveUtil.isRowType(io.trino.plugin.hive.util.HiveUtil.isRowType) RowType(io.trino.spi.type.RowType) SqlDecimal(io.trino.spi.type.SqlDecimal) TypeInfoUtils.getTypeInfosFromTypeString(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils.getTypeInfosFromTypeString) HiveUtil.isMapType(io.trino.plugin.hive.util.HiveUtil.isMapType) MapType(io.trino.spi.type.MapType) ArrayType(io.trino.spi.type.ArrayType) HiveUtil.isArrayType(io.trino.plugin.hive.util.HiveUtil.isArrayType) HiveUtil.isMapType(io.trino.plugin.hive.util.HiveUtil.isMapType) HiveUtil.isRowType(io.trino.plugin.hive.util.HiveUtil.isRowType) MapType(io.trino.spi.type.MapType) CharType(io.trino.spi.type.CharType) RowType(io.trino.spi.type.RowType) ArrayType(io.trino.spi.type.ArrayType) MessageType(org.apache.parquet.schema.MessageType) DecimalType(io.trino.spi.type.DecimalType) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) HiveUtil.isArrayType(io.trino.plugin.hive.util.HiveUtil.isArrayType) HiveUtil.isStructuralType(io.trino.plugin.hive.util.HiveUtil.isStructuralType) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) CharType(io.trino.spi.type.CharType) Map(java.util.Map) HashMap(java.util.HashMap) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 22 with MapType

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));
    }
}
Also used : IntStream(java.util.stream.IntStream) BlockTypeOperators(io.trino.type.BlockTypeOperators) IntUnaryOperator(java.util.function.IntUnaryOperator) MapType(io.trino.spi.type.MapType) Assert.assertEquals(org.testng.Assert.assertEquals) SingleTypedHistogram(io.trino.operator.aggregation.histogram.SingleTypedHistogram) Test(org.testng.annotations.Test) TypeOperators(io.trino.spi.type.TypeOperators) StructuralTestUtil.mapType(io.trino.util.StructuralTestUtil.mapType) BIGINT(io.trino.spi.type.BigintType.BIGINT) Block(io.trino.spi.block.Block) TypedHistogram(io.trino.operator.aggregation.histogram.TypedHistogram) BlockBuilder(io.trino.spi.block.BlockBuilder) BlockTypeOperators(io.trino.type.BlockTypeOperators) SingleTypedHistogram(io.trino.operator.aggregation.histogram.SingleTypedHistogram) TypedHistogram(io.trino.operator.aggregation.histogram.TypedHistogram) SingleTypedHistogram(io.trino.operator.aggregation.histogram.SingleTypedHistogram) Block(io.trino.spi.block.Block) MapType(io.trino.spi.type.MapType) BlockBuilder(io.trino.spi.block.BlockBuilder) BlockTypeOperators(io.trino.type.BlockTypeOperators) TypeOperators(io.trino.spi.type.TypeOperators) Test(org.testng.annotations.Test)

Example 23 with MapType

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());
}
Also used : MapType(io.trino.spi.type.MapType) BlockBuilder(io.trino.spi.block.BlockBuilder) Test(org.testng.annotations.Test)

Example 24 with MapType

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());
}
Also used : MapType(io.trino.spi.type.MapType) BlockBuilder(io.trino.spi.block.BlockBuilder) Test(org.testng.annotations.Test)

Example 25 with MapType

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))));
}
Also used : MapType(io.trino.spi.type.MapType) Test(org.testng.annotations.Test)

Aggregations

MapType (io.trino.spi.type.MapType)85 Type (io.trino.spi.type.Type)45 ArrayType (io.trino.spi.type.ArrayType)42 RowType (io.trino.spi.type.RowType)38 BlockBuilder (io.trino.spi.block.BlockBuilder)28 VarcharType (io.trino.spi.type.VarcharType)26 Block (io.trino.spi.block.Block)17 DecimalType (io.trino.spi.type.DecimalType)17 Map (java.util.Map)17 Test (org.testng.annotations.Test)17 ImmutableList (com.google.common.collect.ImmutableList)16 List (java.util.List)14 TypeSignature.mapType (io.trino.spi.type.TypeSignature.mapType)13 CharType (io.trino.spi.type.CharType)12 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)11 TypeOperators (io.trino.spi.type.TypeOperators)10 ImmutableMap (com.google.common.collect.ImmutableMap)9 VarbinaryType (io.trino.spi.type.VarbinaryType)8 Collectors.toList (java.util.stream.Collectors.toList)8