Search in sources :

Example 96 with BlockBuilderStatus

use of com.facebook.presto.spi.block.BlockBuilderStatus in project presto by prestodb.

the class BenchmarkArraySort method oldArraySort.

@ScalarFunction
@SqlType("array(varchar)")
public static Block oldArraySort(@SqlType("array(varchar)") Block block) {
    List<Integer> positions = Ints.asList(new int[block.getPositionCount()]);
    for (int i = 0; i < block.getPositionCount(); i++) {
        positions.set(i, i);
    }
    Collections.sort(positions, new Comparator<Integer>() {

        @Override
        public int compare(Integer p1, Integer p2) {
            //TODO: This could be quite slow, it should use parametric equals
            return VARCHAR.compareTo(block, p1, block, p2);
        }
    });
    BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), block.getPositionCount());
    for (int position : positions) {
        VARCHAR.appendTo(block, position, blockBuilder);
    }
    return blockBuilder.build();
}
Also used : BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) SqlType(com.facebook.presto.spi.function.SqlType)

Example 97 with BlockBuilderStatus

use of com.facebook.presto.spi.block.BlockBuilderStatus in project presto by prestodb.

the class BenchmarkArrayDistinct method oldArrayDistinct.

@ScalarFunction
@SqlType("array(varchar)")
public static Block oldArrayDistinct(@SqlType("array(varchar)") Block array) {
    if (array.getPositionCount() == 0) {
        return array;
    }
    TypedSet typedSet = new TypedSet(VARCHAR, array.getPositionCount());
    BlockBuilder distinctElementBlockBuilder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), array.getPositionCount());
    for (int i = 0; i < array.getPositionCount(); i++) {
        if (!typedSet.contains(array, i)) {
            typedSet.add(array, i);
            VARCHAR.appendTo(array, i, distinctElementBlockBuilder);
        }
    }
    return distinctElementBlockBuilder.build();
}
Also used : TypedSet(com.facebook.presto.operator.aggregation.TypedSet) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) SqlType(com.facebook.presto.spi.function.SqlType)

Example 98 with BlockBuilderStatus

use of com.facebook.presto.spi.block.BlockBuilderStatus in project presto by prestodb.

the class TestTypedHistogram method testMassive.

@Test
public void testMassive() throws Exception {
    BlockBuilder inputBlockBuilder = BIGINT.createBlockBuilder(new BlockBuilderStatus(), 5000);
    TypedHistogram typedHistogram = new TypedHistogram(BIGINT, 1000);
    IntStream.range(1, 2000).flatMap(i -> IntStream.iterate(i, IntUnaryOperator.identity()).limit(i)).forEach(j -> BIGINT.writeLong(inputBlockBuilder, j));
    Block inputBlock = inputBlockBuilder.build();
    for (int i = 0; i < inputBlock.getPositionCount(); i++) {
        typedHistogram.add(i, inputBlock, 1);
    }
    Block outputBlock = typedHistogram.serialize();
    for (int i = 0; i < outputBlock.getPositionCount(); i += 2) {
        assertEquals(BIGINT.getLong(outputBlock, i + 1), BIGINT.getLong(outputBlock, i));
    }
}
Also used : IntStream(java.util.stream.IntStream) Block(com.facebook.presto.spi.block.Block) IntUnaryOperator(java.util.function.IntUnaryOperator) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BIGINT(com.facebook.presto.spi.type.BigintType.BIGINT) Block(com.facebook.presto.spi.block.Block) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) Test(org.testng.annotations.Test)

Example 99 with BlockBuilderStatus

use of com.facebook.presto.spi.block.BlockBuilderStatus in project presto by prestodb.

the class TestTypedKeyValueHeap method test.

private static void test(IntStream keyInputStream, Stream<String> valueInputStream, BlockComparator comparator, Iterator<String> outputIterator) {
    BlockBuilder keysBlockBuilder = BIGINT.createBlockBuilder(new BlockBuilderStatus(), INPUT_SIZE);
    BlockBuilder valuesBlockBuilder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), INPUT_SIZE);
    keyInputStream.forEach(x -> BIGINT.writeLong(keysBlockBuilder, x));
    valueInputStream.forEach(x -> VARCHAR.writeString(valuesBlockBuilder, x));
    TypedKeyValueHeap heap = new TypedKeyValueHeap(comparator, BIGINT, VARCHAR, OUTPUT_SIZE);
    heap.addAll(keysBlockBuilder, valuesBlockBuilder);
    BlockBuilder resultBlockBuilder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), OUTPUT_SIZE);
    heap.popAll(resultBlockBuilder);
    Block resultBlock = resultBlockBuilder.build();
    assertEquals(resultBlock.getPositionCount(), OUTPUT_SIZE);
    for (int i = 0; i < OUTPUT_SIZE; i++) {
        assertEquals(VARCHAR.getSlice(resultBlock, i).toStringUtf8(), outputIterator.next());
    }
}
Also used : Block(com.facebook.presto.spi.block.Block) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 100 with BlockBuilderStatus

use of com.facebook.presto.spi.block.BlockBuilderStatus in project presto by prestodb.

the class TestStateCompiler method testComplexSerialization.

@Test
public void testComplexSerialization() {
    Type arrayType = new ArrayType(BIGINT);
    Type mapType = new 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.setSlice(utf8Slice("test"));
    singleState.setAnotherSlice(wrappedDoubleArray(1.0, 2.0, 3.0));
    singleState.setYetAnotherSlice(null);
    Block array = createLongsBlock(45);
    singleState.setBlock(array);
    BlockBuilder mapBlockBuilder = new InterleavedBlockBuilder(ImmutableList.of(BIGINT, VARCHAR), new BlockBuilderStatus(), 1);
    BIGINT.writeLong(mapBlockBuilder, 123L);
    VARCHAR.writeSlice(mapBlockBuilder, utf8Slice("testBlock"));
    Block map = mapBlockBuilder.build();
    singleState.setAnotherBlock(map);
    BlockBuilder builder = new RowType(ImmutableList.of(BOOLEAN, TINYINT, DOUBLE, BIGINT, mapType, VARBINARY, arrayType, VARBINARY, VARBINARY), Optional.empty()).createBlockBuilder(new BlockBuilderStatus(), 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.getSlice(), singleState.getSlice());
    assertEquals(deserializedState.getAnotherSlice(), singleState.getAnotherSlice());
    assertEquals(deserializedState.getYetAnotherSlice(), singleState.getYetAnotherSlice());
    assertEquals(deserializedState.getBlock().getLong(0, 0), singleState.getBlock().getLong(0, 0));
    assertEquals(deserializedState.getAnotherBlock().getLong(0, 0), singleState.getAnotherBlock().getLong(0, 0));
    assertEquals(deserializedState.getAnotherBlock().getSlice(1, 0, 9), singleState.getAnotherBlock().getSlice(1, 0, 9));
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) RowType(com.facebook.presto.type.RowType) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) MapType(com.facebook.presto.type.MapType) ArrayType(com.facebook.presto.type.ArrayType) ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) RowType(com.facebook.presto.type.RowType) Type(com.facebook.presto.spi.type.Type) Block(com.facebook.presto.spi.block.Block) BlockAssertions.createLongsBlock(com.facebook.presto.block.BlockAssertions.createLongsBlock) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) Test(org.testng.annotations.Test)

Aggregations

BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)227 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)210 Block (com.facebook.presto.spi.block.Block)55 Slice (io.airlift.slice.Slice)43 InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)37 Test (org.testng.annotations.Test)35 Type (com.facebook.presto.spi.type.Type)24 SqlType (com.facebook.presto.spi.function.SqlType)20 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)15 ArrayType (com.facebook.presto.type.ArrayType)14 Page (com.facebook.presto.spi.Page)12 TypeJsonUtils.appendToBlockBuilder (com.facebook.presto.type.TypeJsonUtils.appendToBlockBuilder)12 TypeParameter (com.facebook.presto.spi.function.TypeParameter)11 RowType (com.facebook.presto.type.RowType)11 OrcCorruptionException (com.facebook.presto.orc.OrcCorruptionException)10 RunLengthEncodedBlock (com.facebook.presto.spi.block.RunLengthEncodedBlock)10 MapType (com.facebook.presto.type.MapType)10 PrestoException (com.facebook.presto.spi.PrestoException)9 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)8 DecimalType (com.facebook.presto.spi.type.DecimalType)8