use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class StreamSummary method deserialize.
public static StreamSummary deserialize(Type type, Block block) {
int currentPosition = 0;
int maxBuckets = toIntExact(BIGINT.getLong(block, currentPosition++));
int heapCapacity = toIntExact(BIGINT.getLong(block, currentPosition++));
StreamSummary streamSummary = new StreamSummary(type, maxBuckets, heapCapacity);
Block keysBlock = new ArrayType(type).getObject(block, currentPosition++);
Block valuesBlock = new ArrayType(BIGINT).getObject(block, currentPosition);
for (int position = 0; position < keysBlock.getPositionCount(); position++) {
streamSummary.add(keysBlock, position, valuesBlock.getLong(position));
}
return streamSummary;
}
use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class ApproximateMostFrequent method specialize.
@Override
public InternalAggregationFunction specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
Type keyType = boundVariables.getTypeVariable("K");
checkArgument(keyType.isComparable(), "keyType must be comparable");
Type serializedType = functionAndTypeManager.getParameterizedType(ROW, ImmutableList.of(buildTypeSignatureParameter(MAX_BUCKETS, BigintType.BIGINT), buildTypeSignatureParameter(CAPACITY, BigintType.BIGINT), buildTypeSignatureParameter(KEYS, new ArrayType(keyType)), buildTypeSignatureParameter(VALUES, new ArrayType(BigintType.BIGINT))));
Type outputType = functionAndTypeManager.getParameterizedType(MAP, ImmutableList.of(TypeSignatureParameter.of(keyType.getTypeSignature()), TypeSignatureParameter.of(BigintType.BIGINT.getTypeSignature())));
DynamicClassLoader classLoader = new DynamicClassLoader(ApproximateMostFrequent.class.getClassLoader());
List<Type> inputTypes = ImmutableList.of(keyType);
ApproximateMostFrequentStateSerializer stateSerializer = new ApproximateMostFrequentStateSerializer(keyType, serializedType);
MethodHandle inputFunction = INPUT_FUNCTION.bindTo(keyType);
AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, outputType.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(keyType), inputFunction, COMBINE_FUNCTION, OUTPUT_FUNCTION, ImmutableList.of(new AggregationMetadata.AccumulatorStateDescriptor(ApproximateMostFrequentState.class, stateSerializer, new ApproximateMostFrequentStateFactory())), outputType);
GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(serializedType), outputType, true, false, factory);
}
use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class TestMapAggAggregation method testArrayDoubleMap.
@Test
public void testArrayDoubleMap() {
InternalAggregationFunction aggFunc = getAggregation(new ArrayType(VARCHAR), 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));
}
use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class TestStateCompiler method testComplexStateEstimatedSize.
@Test(invocationCount = 100, successPercentage = 90)
public void testComplexStateEstimatedSize() {
Map<String, Type> fieldMap = ImmutableMap.of("Block", new ArrayType(BIGINT), "AnotherBlock", mapType(BIGINT, VARCHAR));
AccumulatorStateFactory<TestComplexState> factory = StateCompiler.generateStateFactory(TestComplexState.class, fieldMap, new DynamicClassLoader(TestComplexState.class.getClassLoader()));
TestComplexState groupedState = factory.createGroupedState();
long initialRetainedSize = getComplexStateRetainedSize(groupedState);
assertEquals(groupedState.getEstimatedSize(), initialRetainedSize);
// BlockBigArray or SliceBigArray has an internal map that can grow in size when getting more blocks
// need to handle the map overhead separately
initialRetainedSize -= getReferenceCountMapOverhead(groupedState);
for (int i = 0; i < 1000; i++) {
long retainedSize = 0;
((GroupedAccumulatorState) groupedState).setGroupId(i);
groupedState.setBoolean(true);
groupedState.setLong(1);
groupedState.setDouble(2.0);
groupedState.setByte((byte) 3);
groupedState.setInt(4);
Slice slice = utf8Slice("test");
retainedSize += slice.getRetainedSize();
groupedState.setSlice(slice);
slice = wrappedDoubleArray(1.0, 2.0, 3.0);
retainedSize += slice.getRetainedSize();
groupedState.setAnotherSlice(slice);
groupedState.setYetAnotherSlice(null);
Block array = createLongsBlock(45);
retainedSize += array.getRetainedSizeInBytes();
groupedState.setBlock(array);
BlockBuilder mapBlockBuilder = mapType(BIGINT, VARCHAR).createBlockBuilder(null, 1);
BlockBuilder singleMapBlockWriter = mapBlockBuilder.beginBlockEntry();
BIGINT.writeLong(singleMapBlockWriter, 123L);
VARCHAR.writeSlice(singleMapBlockWriter, utf8Slice("testBlock"));
mapBlockBuilder.closeEntry();
Block map = mapBlockBuilder.build();
retainedSize += map.getRetainedSizeInBytes();
groupedState.setAnotherBlock(map);
assertEquals(groupedState.getEstimatedSize(), initialRetainedSize + retainedSize * (i + 1) + getReferenceCountMapOverhead(groupedState));
}
for (int i = 0; i < 1000; i++) {
long retainedSize = 0;
((GroupedAccumulatorState) groupedState).setGroupId(i);
groupedState.setBoolean(true);
groupedState.setLong(1);
groupedState.setDouble(2.0);
groupedState.setByte((byte) 3);
groupedState.setInt(4);
Slice slice = utf8Slice("test");
retainedSize += slice.getRetainedSize();
groupedState.setSlice(slice);
slice = wrappedDoubleArray(1.0, 2.0, 3.0);
retainedSize += slice.getRetainedSize();
groupedState.setAnotherSlice(slice);
groupedState.setYetAnotherSlice(null);
Block array = createLongsBlock(45);
retainedSize += array.getRetainedSizeInBytes();
groupedState.setBlock(array);
BlockBuilder mapBlockBuilder = mapType(BIGINT, VARCHAR).createBlockBuilder(null, 1);
BlockBuilder singleMapBlockWriter = mapBlockBuilder.beginBlockEntry();
BIGINT.writeLong(singleMapBlockWriter, 123L);
VARCHAR.writeSlice(singleMapBlockWriter, utf8Slice("testBlock"));
mapBlockBuilder.closeEntry();
Block map = mapBlockBuilder.build();
retainedSize += map.getRetainedSizeInBytes();
groupedState.setAnotherBlock(map);
assertEquals(groupedState.getEstimatedSize(), initialRetainedSize + retainedSize * 1000 + getReferenceCountMapOverhead(groupedState));
}
}
use of com.facebook.presto.common.type.ArrayType in project presto by prestodb.
the class TestStateCompiler method testComplexSerialization.
@Test
public void testComplexSerialization() {
Type arrayType = new ArrayType(BIGINT);
Type mapType = mapType(BIGINT, VARCHAR);
Map<String, Type> fieldMap = ImmutableMap.of("Block", arrayType, "AnotherBlock", mapType);
AccumulatorStateFactory<TestComplexState> factory = StateCompiler.generateStateFactory(TestComplexState.class, fieldMap, new DynamicClassLoader(TestComplexState.class.getClassLoader()));
AccumulatorStateSerializer<TestComplexState> serializer = StateCompiler.generateStateSerializer(TestComplexState.class, fieldMap, new DynamicClassLoader(TestComplexState.class.getClassLoader()));
TestComplexState singleState = factory.createSingleState();
TestComplexState deserializedState = factory.createSingleState();
singleState.setBoolean(true);
singleState.setLong(1);
singleState.setDouble(2.0);
singleState.setByte((byte) 3);
singleState.setInt(4);
singleState.setSlice(utf8Slice("test"));
singleState.setAnotherSlice(wrappedDoubleArray(1.0, 2.0, 3.0));
singleState.setYetAnotherSlice(null);
Block array = createLongsBlock(45);
singleState.setBlock(array);
singleState.setAnotherBlock(mapBlockOf(BIGINT, VARCHAR, ImmutableMap.of(123L, "testBlock")));
BlockBuilder builder = RowType.anonymous(ImmutableList.of(BOOLEAN, TINYINT, DOUBLE, INTEGER, BIGINT, mapType, VARBINARY, arrayType, VARBINARY, VARBINARY)).createBlockBuilder(null, 1);
serializer.serialize(singleState, builder);
Block block = builder.build();
serializer.deserialize(block, 0, deserializedState);
assertEquals(deserializedState.getBoolean(), singleState.getBoolean());
assertEquals(deserializedState.getLong(), singleState.getLong());
assertEquals(deserializedState.getDouble(), singleState.getDouble());
assertEquals(deserializedState.getByte(), singleState.getByte());
assertEquals(deserializedState.getInt(), singleState.getInt());
assertEquals(deserializedState.getSlice(), singleState.getSlice());
assertEquals(deserializedState.getAnotherSlice(), singleState.getAnotherSlice());
assertEquals(deserializedState.getYetAnotherSlice(), singleState.getYetAnotherSlice());
assertEquals(deserializedState.getBlock().getLong(0), singleState.getBlock().getLong(0));
assertEquals(deserializedState.getAnotherBlock().getLong(0), singleState.getAnotherBlock().getLong(0));
assertEquals(deserializedState.getAnotherBlock().getSlice(1, 0, 9), singleState.getAnotherBlock().getSlice(1, 0, 9));
}
Aggregations