Search in sources :

Example 86 with ArrayType

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;
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) Block(com.facebook.presto.common.block.Block)

Example 87 with ArrayType

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);
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) BigintType(com.facebook.presto.common.type.BigintType) GenericAccumulatorFactoryBinder(com.facebook.presto.operator.aggregation.GenericAccumulatorFactoryBinder) AggregationMetadata(com.facebook.presto.operator.aggregation.AggregationMetadata) InternalAggregationFunction(com.facebook.presto.operator.aggregation.InternalAggregationFunction) MethodHandle(java.lang.invoke.MethodHandle)

Example 88 with ArrayType

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));
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) Test(org.testng.annotations.Test)

Example 89 with ArrayType

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));
    }
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) ArrayType(com.facebook.presto.common.type.ArrayType) ArrayType(com.facebook.presto.common.type.ArrayType) StructuralTestUtil.mapType(com.facebook.presto.util.StructuralTestUtil.mapType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) BlockAssertions.createLongsBlock(com.facebook.presto.block.BlockAssertions.createLongsBlock) Block(com.facebook.presto.common.block.Block) GroupedAccumulatorState(com.facebook.presto.spi.function.GroupedAccumulatorState) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) Test(org.testng.annotations.Test)

Example 90 with ArrayType

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));
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) ArrayType(com.facebook.presto.common.type.ArrayType) StructuralTestUtil.mapType(com.facebook.presto.util.StructuralTestUtil.mapType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) BlockAssertions.createLongsBlock(com.facebook.presto.block.BlockAssertions.createLongsBlock) Block(com.facebook.presto.common.block.Block) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) Test(org.testng.annotations.Test)

Aggregations

ArrayType (com.facebook.presto.common.type.ArrayType)287 Test (org.testng.annotations.Test)219 Type (com.facebook.presto.common.type.Type)99 RowType (com.facebook.presto.common.type.RowType)79 ArrayList (java.util.ArrayList)58 ImmutableList (com.google.common.collect.ImmutableList)54 List (java.util.List)51 MapType (com.facebook.presto.common.type.MapType)39 DecimalType.createDecimalType (com.facebook.presto.common.type.DecimalType.createDecimalType)36 Arrays.asList (java.util.Arrays.asList)34 Collections.singletonList (java.util.Collections.singletonList)33 MessageType (org.apache.parquet.schema.MessageType)30 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)28 VarcharType.createUnboundedVarcharType (com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType)27 MessageTypeParser.parseMessageType (org.apache.parquet.schema.MessageTypeParser.parseMessageType)27 InternalAggregationFunction (com.facebook.presto.operator.aggregation.InternalAggregationFunction)26 PrimitiveType (org.apache.parquet.schema.PrimitiveType)26 StructuralTestUtil.mapType (com.facebook.presto.tests.StructuralTestUtil.mapType)24 Block (com.facebook.presto.common.block.Block)23 DecimalType (com.facebook.presto.common.type.DecimalType)17