Search in sources :

Example 16 with MapType

use of com.facebook.presto.type.MapType in project presto by prestodb.

the class TestMultimapAggAggregation method testMultimapAgg.

private static <K, V> void testMultimapAgg(Type keyType, List<K> expectedKeys, Type valueType, List<V> expectedValues) {
    checkState(expectedKeys.size() == expectedValues.size(), "expectedKeys and expectedValues should have equal size");
    MapType mapType = new MapType(keyType, new ArrayType(valueType));
    Signature signature = new Signature(NAME, AGGREGATE, mapType.getTypeSignature(), keyType.getTypeSignature(), valueType.getTypeSignature());
    InternalAggregationFunction aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(signature);
    Map<K, List<V>> map = new HashMap<>();
    for (int i = 0; i < expectedKeys.size(); i++) {
        if (!map.containsKey(expectedKeys.get(i))) {
            map.put(expectedKeys.get(i), new ArrayList<>());
        }
        map.get(expectedKeys.get(i)).add(expectedValues.get(i));
    }
    RowPageBuilder builder = RowPageBuilder.rowPageBuilder(keyType, valueType);
    for (int i = 0; i < expectedKeys.size(); i++) {
        builder.row(expectedKeys.get(i), expectedValues.get(i));
    }
    assertAggregation(aggFunc, map.isEmpty() ? null : map, builder.build().getBlocks());
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) HashMap(java.util.HashMap) Signature(com.facebook.presto.metadata.Signature) RowPageBuilder(com.facebook.presto.RowPageBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) MapType(com.facebook.presto.type.MapType)

Example 17 with MapType

use of com.facebook.presto.type.MapType in project presto by prestodb.

the class TestMapAggAggregation method testDoubleMapMap.

@Test
public void testDoubleMapMap() throws Exception {
    MapType innerMapType = new MapType(VARCHAR, VARCHAR);
    MapType mapType = new MapType(DOUBLE, innerMapType);
    InternalAggregationFunction aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature(), parseTypeSignature(StandardTypes.DOUBLE), innerMapType.getTypeSignature()));
    BlockBuilder builder = innerMapType.createBlockBuilder(new BlockBuilderStatus(), 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(aggFunc, 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 : Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) MapType(com.facebook.presto.type.MapType) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) Test(org.testng.annotations.Test)

Example 18 with MapType

use of com.facebook.presto.type.MapType in project presto by prestodb.

the class TestMapAggAggregation method testArrayDoubleMap.

@Test
public void testArrayDoubleMap() throws Exception {
    ArrayType arrayType = new ArrayType(VARCHAR);
    MapType mapType = new MapType(arrayType, DOUBLE);
    InternalAggregationFunction aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature(), arrayType.getTypeSignature(), parseTypeSignature(StandardTypes.DOUBLE)));
    assertAggregation(aggFunc, ImmutableMap.of(ImmutableList.of("a", "b"), 1.0, ImmutableList.of("c", "d"), 2.0, ImmutableList.of("e", "f"), 3.0), createStringArraysBlock(ImmutableList.of(ImmutableList.of("a", "b"), ImmutableList.of("c", "d"), ImmutableList.of("e", "f"))), createDoublesBlock(1.0, 2.0, 3.0));
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) MapType(com.facebook.presto.type.MapType) Test(org.testng.annotations.Test)

Example 19 with MapType

use of com.facebook.presto.type.MapType in project presto by prestodb.

the class TestMapUnionAggregation method testSimpleWithDuplicates.

@Test
public void testSimpleWithDuplicates() throws Exception {
    MapType mapType = new MapType(DOUBLE, VARCHAR);
    InternalAggregationFunction aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature(), mapType.getTypeSignature()));
    assertAggregation(aggFunc, 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 = new MapType(DOUBLE, BIGINT);
    aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature(), mapType.getTypeSignature()));
    assertAggregation(aggFunc, 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 = new MapType(BOOLEAN, BIGINT);
    aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature(), mapType.getTypeSignature()));
    assertAggregation(aggFunc, 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 : Signature(com.facebook.presto.metadata.Signature) MapType(com.facebook.presto.type.MapType) Test(org.testng.annotations.Test)

Example 20 with MapType

use of com.facebook.presto.type.MapType in project presto by prestodb.

the class TestHistogram method testWithNulls.

@Test
public void testWithNulls() throws Exception {
    MapType mapType = new MapType(BIGINT, BIGINT);
    InternalAggregationFunction aggregationFunction = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature(), parseTypeSignature(StandardTypes.BIGINT)));
    assertAggregation(aggregationFunction, ImmutableMap.of(1L, 1L, 2L, 1L), createLongsBlock(2L, null, 1L));
    mapType = new MapType(BIGINT, BIGINT);
    aggregationFunction = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature(), parseTypeSignature(StandardTypes.BIGINT)));
    assertAggregation(aggregationFunction, null, createLongsBlock((Long) null));
}
Also used : Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) MapType(com.facebook.presto.type.MapType) Test(org.testng.annotations.Test)

Aggregations

MapType (com.facebook.presto.type.MapType)58 Test (org.testng.annotations.Test)42 ArrayType (com.facebook.presto.type.ArrayType)28 Type (com.facebook.presto.spi.type.Type)20 Signature (com.facebook.presto.metadata.Signature)18 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)14 RowType (com.facebook.presto.type.RowType)12 Block (com.facebook.presto.spi.block.Block)10 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)10 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)10 ImmutableList (com.google.common.collect.ImmutableList)7 HashMap (java.util.HashMap)7 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)6 ImmutableMap (com.google.common.collect.ImmutableMap)6 InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)5 List (java.util.List)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)2 KeyValuePairStateSerializer (com.facebook.presto.operator.aggregation.state.KeyValuePairStateSerializer)2