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);
}
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);
}
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);
}
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);
}
}
}
}
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', ',', '=')");
}
Aggregations