Search in sources :

Example 46 with MapType

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

the class TestMapAggAggregation method testNull.

@Test
public void testNull() throws Exception {
    InternalAggregationFunction doubleDouble = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, new MapType(DOUBLE, DOUBLE).getTypeSignature(), parseTypeSignature(StandardTypes.DOUBLE), parseTypeSignature(StandardTypes.DOUBLE)));
    assertAggregation(doubleDouble, ImmutableMap.of(1.0, 2.0), createDoublesBlock(1.0, null, null), createDoublesBlock(2.0, 3.0, 4.0));
    assertAggregation(doubleDouble, null, createDoublesBlock(null, null, null), createDoublesBlock(2.0, 3.0, 4.0));
    Map<Double, Double> expected = new LinkedHashMap<>();
    expected.put(1.0, null);
    expected.put(2.0, null);
    expected.put(3.0, null);
    assertAggregation(doubleDouble, expected, createDoublesBlock(1.0, 2.0, 3.0), createDoublesBlock(null, null, null));
}
Also used : Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) MapType(com.facebook.presto.type.MapType) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

Example 47 with MapType

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

the class TestMapUnionAggregation method testSimpleWithNulls.

@Test
public void testSimpleWithNulls() throws Exception {
    MapType mapType = new MapType(DOUBLE, VARCHAR);
    InternalAggregationFunction aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature(), mapType.getTypeSignature()));
    Map<Object, Object> expected = mapOf(23.0, "aaa", 33.0, null, 43.0, "ccc", 53.0, "ddd", null, "eee");
    assertAggregation(aggFunc, expected, arrayBlockOf(mapType, mapBlockOf(DOUBLE, VARCHAR, mapOf(23.0, "aaa", 33.0, null, 53.0, "ddd")), null, mapBlockOf(DOUBLE, VARCHAR, mapOf(43.0, "ccc", 53.0, "ddd", null, "eee"))));
}
Also used : Signature(com.facebook.presto.metadata.Signature) MapType(com.facebook.presto.type.MapType) Test(org.testng.annotations.Test)

Example 48 with MapType

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

the class TestMapUnionAggregation method testStructural.

@Test
public void testStructural() throws Exception {
    MapType mapType = new MapType(DOUBLE, new ArrayType(VARCHAR));
    InternalAggregationFunction aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature(), mapType.getTypeSignature()));
    assertAggregation(aggFunc, ImmutableMap.of(1.0, ImmutableList.of("a", "b"), 2.0, ImmutableList.of("c", "d"), 3.0, ImmutableList.of("e", "f"), 4.0, ImmutableList.of("r", "s")), arrayBlockOf(mapType, mapBlockOf(DOUBLE, new ArrayType(VARCHAR), ImmutableMap.of(1.0, ImmutableList.of("a", "b"), 2.0, ImmutableList.of("c", "d"), 3.0, ImmutableList.of("e", "f"))), mapBlockOf(DOUBLE, new ArrayType(VARCHAR), ImmutableMap.of(1.0, ImmutableList.of("x", "y"), 4.0, ImmutableList.of("r", "s"), 3.0, ImmutableList.of("w", "z")))));
    mapType = new MapType(DOUBLE, new MapType(VARCHAR, VARCHAR));
    aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature(), mapType.getTypeSignature()));
    assertAggregation(aggFunc, ImmutableMap.of(1.0, ImmutableMap.of("a", "b"), 2.0, ImmutableMap.of("c", "d"), 3.0, ImmutableMap.of("e", "f")), arrayBlockOf(mapType, mapBlockOf(DOUBLE, new MapType(VARCHAR, VARCHAR), ImmutableMap.of(1.0, ImmutableMap.of("a", "b"), 2.0, ImmutableMap.of("c", "d"))), mapBlockOf(DOUBLE, new MapType(VARCHAR, VARCHAR), ImmutableMap.of(3.0, ImmutableMap.of("e", "f")))));
    mapType = new MapType(new ArrayType(VARCHAR), DOUBLE);
    aggFunc = metadata.getFunctionRegistry().getAggregateFunctionImplementation(new Signature(NAME, AGGREGATE, mapType.getTypeSignature(), mapType.getTypeSignature()));
    assertAggregation(aggFunc, ImmutableMap.of(ImmutableList.of("a", "b"), 1.0, ImmutableList.of("c", "d"), 2.0, ImmutableList.of("e", "f"), 3.0), arrayBlockOf(mapType, mapBlockOf(new ArrayType(VARCHAR), DOUBLE, ImmutableMap.of(ImmutableList.of("a", "b"), 1.0, ImmutableList.of("e", "f"), 3.0)), mapBlockOf(new ArrayType(VARCHAR), DOUBLE, ImmutableMap.of(ImmutableList.of("c", "d"), 2.0))));
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) Signature(com.facebook.presto.metadata.Signature) MapType(com.facebook.presto.type.MapType) Test(org.testng.annotations.Test)

Example 49 with MapType

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

the class MapAggregationFunction method generateAggregation.

private static InternalAggregationFunction generateAggregation(Type keyType, Type valueType) {
    DynamicClassLoader classLoader = new DynamicClassLoader(MapAggregationFunction.class.getClassLoader());
    List<Type> inputTypes = ImmutableList.of(keyType, valueType);
    Type outputType = new MapType(keyType, valueType);
    KeyValuePairStateSerializer stateSerializer = new KeyValuePairStateSerializer(keyType, valueType);
    Type intermediateType = stateSerializer.getSerializedType();
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, outputType.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(keyType, valueType), INPUT_FUNCTION.bindTo(keyType).bindTo(valueType), COMBINE_FUNCTION, OUTPUT_FUNCTION, KeyValuePairsState.class, stateSerializer, new KeyValuePairsStateFactory(keyType, valueType), outputType);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(NAME, inputTypes, intermediateType, outputType, true, factory);
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) KeyValuePairStateSerializer(com.facebook.presto.operator.aggregation.state.KeyValuePairStateSerializer) KeyValuePairsStateFactory(com.facebook.presto.operator.aggregation.state.KeyValuePairsStateFactory) MapType(com.facebook.presto.type.MapType)

Example 50 with MapType

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

the class TestField method testMap.

@Test
public void testMap() throws Exception {
    Type type = new MapType(VARCHAR, BIGINT);
    Block expected = AccumuloRowSerializer.getBlockFromMap(type, ImmutableMap.of("a", 1L, "b", 2L, "c", 3L));
    Field f1 = new Field(expected, type);
    assertEquals(f1.getMap(), expected);
    assertEquals(f1.getObject(), expected);
    assertEquals(f1.getType(), type);
    assertEquals(f1.toString(), "MAP(ARRAY ['a','b','c'], ARRAY [1,2,3])");
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) Block(com.facebook.presto.spi.block.Block) 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