Search in sources :

Example 51 with MapType

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

the class MapAggregationFunction method specialize.

@Override
public InternalAggregationFunction specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
    Type keyType = boundVariables.getTypeVariable("K");
    Type valueType = boundVariables.getTypeVariable("V");
    MapType outputType = (MapType) functionAndTypeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.of(keyType.getTypeSignature()), TypeSignatureParameter.of(valueType.getTypeSignature())));
    return generateAggregation(keyType, valueType, outputType);
}
Also used : MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) MapType(com.facebook.presto.common.type.MapType)

Example 52 with MapType

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

the class MapAggregationFunction method generateAggregation.

private static InternalAggregationFunction generateAggregation(Type keyType, Type valueType, MapType outputType) {
    DynamicClassLoader classLoader = new DynamicClassLoader(MapAggregationFunction.class.getClassLoader());
    List<Type> inputTypes = ImmutableList.of(keyType, valueType);
    KeyValuePairStateSerializer stateSerializer = new KeyValuePairStateSerializer(outputType);
    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, ImmutableList.of(new AccumulatorStateDescriptor(KeyValuePairsState.class, stateSerializer, new KeyValuePairsStateFactory(keyType, valueType))), outputType);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(intermediateType), outputType, true, true, factory);
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) KeyValuePairStateSerializer(com.facebook.presto.operator.aggregation.state.KeyValuePairStateSerializer) AccumulatorStateDescriptor(com.facebook.presto.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) KeyValuePairsStateFactory(com.facebook.presto.operator.aggregation.state.KeyValuePairsStateFactory)

Example 53 with MapType

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

the class MapUnionSumAggregation method generateAggregation.

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

Example 54 with MapType

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

the class TestMapBlockBuilder method testDirectBlockEntry.

@Test
public void testDirectBlockEntry() {
    MapType innerMapType = new MapType(BIGINT, BIGINT, KEY_BLOCK_EQUALS, KEY_BLOCK_HASH_CODE);
    MapType mapType = new MapType(BIGINT, innerMapType, KEY_BLOCK_EQUALS, KEY_BLOCK_HASH_CODE);
    MapBlockBuilder blockBuilder = (MapBlockBuilder) mapType.createBlockBuilder(null, MAP_POSITIONS);
    int numberOfOuterElements = 10;
    int numberOfInnerElements = 500;
    int outerKeyBase = 100;
    int keyBase = 1_000;
    int valueBase = 1_000_000;
    // Each outer map's value has 500 entries of map {long, long} (This is called inner map).
    for (int element = 0; element < MAP_POSITIONS; element++) {
        blockBuilder.beginDirectEntry();
        BlockBuilder outerKeyBuilder = blockBuilder.getKeyBlockBuilder();
        for (int outer = 0; outer < numberOfOuterElements; outer++) {
            BIGINT.writeLong(outerKeyBuilder, element * outerKeyBase + outer);
        }
        MapBlockBuilder outerValueBuilder = (MapBlockBuilder) blockBuilder.getValueBlockBuilder();
        for (int outer = 0; outer < numberOfOuterElements; outer++) {
            outerValueBuilder.beginDirectEntry();
            BlockBuilder innerKeyBuilder = outerValueBuilder.getKeyBlockBuilder();
            for (int inner = 0; inner < numberOfInnerElements; inner++) {
                BIGINT.writeLong(innerKeyBuilder, inner + outer * keyBase);
            }
            BlockBuilder innerValueBuilder = outerValueBuilder.getValueBlockBuilder();
            for (int inner = 0; inner < numberOfInnerElements; inner++) {
                BIGINT.writeLong(innerValueBuilder, inner + outer * valueBase);
            }
            outerValueBuilder.closeEntry();
        }
        blockBuilder.closeEntry();
    }
    assertEquals(blockBuilder.getPositionCount(), MAP_POSITIONS);
    for (int element = 0; element < blockBuilder.getPositionCount(); element++) {
        SingleMapBlock outerBlock = (SingleMapBlock) blockBuilder.getBlock(element);
        assertEquals(outerBlock.getPositionCount(), numberOfOuterElements * 2);
        for (int outer = 0; outer < numberOfOuterElements; outer++) {
            assertEquals(outerBlock.getLong(outer * 2), (long) element * outerKeyBase + outer);
            SingleMapBlock innerValueBlock = (SingleMapBlock) outerBlock.getBlock(outer * 2 + 1);
            assertEquals(innerValueBlock.getPositionCount(), numberOfInnerElements * 2);
            for (int inner = 0; inner < numberOfInnerElements; inner++) {
                assertEquals(innerValueBlock.getLong(inner * 2), (long) outer * keyBase + inner);
                assertEquals(innerValueBlock.getLong(inner * 2 + 1), (long) outer * valueBase + inner);
            }
        }
    }
}
Also used : MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) MapType(com.facebook.presto.common.type.MapType) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) Test(org.testng.annotations.Test)

Example 55 with MapType

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

the class TestStringFunctions method testSplitToMultimap.

@Test
public void testSplitToMultimap() {
    MapType expectedType = mapType(VARCHAR, new ArrayType(VARCHAR));
    assertFunction("SPLIT_TO_MULTIMAP('', ',', '=')", expectedType, ImmutableMap.of());
    assertFunction("SPLIT_TO_MULTIMAP('a=123,b=.4,c=,=d', ',', '=')", expectedType, ImmutableMap.of("a", ImmutableList.of("123"), "b", ImmutableList.of(".4"), "c", ImmutableList.of(""), "", ImmutableList.of("d")));
    assertFunction("SPLIT_TO_MULTIMAP('=', ',', '=')", expectedType, ImmutableMap.of("", ImmutableList.of("")));
    // Test multiple values of the same key preserve the order as they appear in input
    assertFunction("SPLIT_TO_MULTIMAP('a=123,a=.4,a=5.67', ',', '=')", expectedType, ImmutableMap.of("a", ImmutableList.of("123", ".4", "5.67")));
    // Test multi-character delimiters
    assertFunction("SPLIT_TO_MULTIMAP('key=>value,key=>value', ',', '=>')", expectedType, ImmutableMap.of("key", ImmutableList.of("value", "value")));
    assertFunction("SPLIT_TO_MULTIMAP('key => value, key => value', ',', '=>')", expectedType, ImmutableMap.of("key ", ImmutableList.of(" value"), " key ", ImmutableList.of(" value")));
    assertFunction("SPLIT_TO_MULTIMAP('key => value, key => value', ', ', '=>')", expectedType, ImmutableMap.of("key ", ImmutableList.of(" value", " value")));
    // Test non-ASCII
    assertFunction("SPLIT_TO_MULTIMAP('\u4EA0\u4EFF\u4EA1', '\u4E00', '\u4EFF')", expectedType, ImmutableMap.of("\u4EA0", ImmutableList.of("\u4EA1")));
    assertFunction("SPLIT_TO_MULTIMAP('\u4EA0\u4EFF\u4EA1\u4E00\u4EA0\u4EFF\u4EB1', '\u4E00', '\u4EFF')", expectedType, ImmutableMap.of("\u4EA0", ImmutableList.of("\u4EA1", "\u4EB1")));
    // Entry delimiter and key-value delimiter must not be the same.
    assertInvalidFunction("SPLIT_TO_MULTIMAP('', '\u4EFF', '\u4EFF')", "entryDelimiter and keyValueDelimiter must not be the same");
    assertInvalidFunction("SPLIT_TO_MULTIMAP('a=123,b=.4,c=', '=', '=')", "entryDelimiter and keyValueDelimiter must not be the same");
    // Key-value delimiter must appear exactly once in each entry.
    assertInvalidFunction("SPLIT_TO_MULTIMAP('key', ',', '=')", "Key-value delimiter must appear exactly once in each entry. Bad input: key");
    assertInvalidFunction("SPLIT_TO_MULTIMAP('key==value', ',', '=')", "Key-value delimiter must appear exactly once in each entry. Bad input: key==value");
    assertInvalidFunction("SPLIT_TO_MULTIMAP('key=va=lue', ',', '=')", "Key-value delimiter must appear exactly once in each entry. Bad input: key=va=lue");
    assertCachedInstanceHasBoundedRetainedSize("SPLIT_TO_MULTIMAP('a=123,b=.4,c=,=d', ',', '=')");
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) MapType(com.facebook.presto.common.type.MapType) Test(org.testng.annotations.Test)

Aggregations

MapType (com.facebook.presto.common.type.MapType)92 Type (com.facebook.presto.common.type.Type)49 ArrayType (com.facebook.presto.common.type.ArrayType)40 Test (org.testng.annotations.Test)32 RowType (com.facebook.presto.common.type.RowType)30 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)24 Block (com.facebook.presto.common.block.Block)21 HashMap (java.util.HashMap)12 DecimalType (com.facebook.presto.common.type.DecimalType)11 ImmutableList (com.google.common.collect.ImmutableList)11 List (java.util.List)11 Map (java.util.Map)11 VarcharType (com.facebook.presto.common.type.VarcharType)9 MethodHandle (java.lang.invoke.MethodHandle)9 ArrayList (java.util.ArrayList)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 SingleMapBlock (com.facebook.presto.common.block.SingleMapBlock)7 PrestoException (com.facebook.presto.spi.PrestoException)7 OperatorType (com.facebook.presto.common.function.OperatorType)6 MapBlockBuilder (com.facebook.presto.common.block.MapBlockBuilder)5