Search in sources :

Example 21 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

the class MapConstructor method createMap.

@UsedByGeneratedCode
public static Block createMap(MapType mapType, MethodHandle keyEqual, MethodHandle keyHashCode, MethodHandle keyIndeterminate, State state, ConnectorSession session, Block keyBlock, Block valueBlock) {
    checkCondition(keyBlock.getPositionCount() == valueBlock.getPositionCount(), INVALID_FUNCTION_ARGUMENT, "Key and value arrays must be the same length");
    PageBuilder pageBuilder = state.getPageBuilder();
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) pageBuilder.getBlockBuilder(0);
    BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
    for (int i = 0; i < keyBlock.getPositionCount(); i++) {
        if (keyBlock.isNull(i)) {
            // close block builder before throwing as we may be in a TRY() call
            // so that subsequent calls do not find it in an inconsistent state
            mapBlockBuilder.closeEntry();
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
        }
        Object keyObject = readNativeValue(mapType.getKeyType(), keyBlock, i);
        try {
            if ((boolean) keyIndeterminate.invoke(keyObject, false)) {
                throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be indeterminate: " + mapType.getKeyType().getObjectValue(session, keyBlock, i));
            }
        } catch (Throwable t) {
            mapBlockBuilder.closeEntry();
            throw internalError(t);
        }
        mapType.getKeyType().appendTo(keyBlock, i, blockBuilder);
        mapType.getValueType().appendTo(valueBlock, i, blockBuilder);
    }
    try {
        mapBlockBuilder.closeEntryStrict();
    } catch (DuplicateMapKeyException e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e.getDetailedMessage(mapType.getKeyType(), session), e);
    } finally {
        pageBuilder.declarePosition();
    }
    return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
Also used : MapBlockBuilder(io.prestosql.spi.block.MapBlockBuilder) DuplicateMapKeyException(io.prestosql.spi.block.DuplicateMapKeyException) PrestoException(io.prestosql.spi.PrestoException) PageBuilder(io.prestosql.spi.PageBuilder) BlockBuilder(io.prestosql.spi.block.BlockBuilder) MapBlockBuilder(io.prestosql.spi.block.MapBlockBuilder) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode)

Example 22 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

the class TestRealHistogramAggregation method makeInput.

private static Page makeInput(int numberOfBuckets) {
    PageBuilder builder = new PageBuilder(ImmutableList.of(BIGINT, REAL, DOUBLE));
    for (int i = 0; i < 100; i++) {
        builder.declarePosition();
        BIGINT.writeLong(builder.getBlockBuilder(0), numberOfBuckets);
        // value
        REAL.writeLong(builder.getBlockBuilder(1), i);
        // weight
        DOUBLE.writeDouble(builder.getBlockBuilder(2), 1);
    }
    return builder.build();
}
Also used : PageBuilder(io.prestosql.spi.PageBuilder)

Example 23 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

the class ZipWithFunction method zipWith.

public static Block zipWith(Type leftElementType, Type rightElementType, ArrayType outputArrayType, Object state, Block leftBlock, Block rightBlock, BinaryFunctionInterface function) {
    Type outputElementType = outputArrayType.getElementType();
    int leftPositionCount = leftBlock.getPositionCount();
    int rightPositionCount = rightBlock.getPositionCount();
    int outputPositionCount = max(leftPositionCount, rightPositionCount);
    PageBuilder pageBuilder = (PageBuilder) state;
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder arrayBlockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder blockBuilder = arrayBlockBuilder.beginBlockEntry();
    for (int position = 0; position < outputPositionCount; position++) {
        Object left = position < leftPositionCount ? readNativeValue(leftElementType, leftBlock, position) : null;
        Object right = position < rightPositionCount ? readNativeValue(rightElementType, rightBlock, position) : null;
        Object output;
        try {
            output = function.apply(left, right);
        } catch (Throwable throwable) {
            // Restore pageBuilder into a consistent state.
            arrayBlockBuilder.closeEntry();
            pageBuilder.declarePosition();
            throwIfUnchecked(throwable);
            throw new RuntimeException(throwable);
        }
        writeNativeValue(outputElementType, blockBuilder, output);
    }
    arrayBlockBuilder.closeEntry();
    pageBuilder.declarePosition();
    return outputArrayType.getObject(arrayBlockBuilder, arrayBlockBuilder.getPositionCount() - 1);
}
Also used : Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) PageBuilder(io.prestosql.spi.PageBuilder) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Example 24 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

the class BenchmarkGroupByHash method createVarcharPages.

private static List<Page> createVarcharPages(int positionCount, int groupCount, int channelCount, boolean hashEnabled) {
    List<Type> types = Collections.nCopies(channelCount, VARCHAR);
    ImmutableList.Builder<Page> pages = ImmutableList.builder();
    if (hashEnabled) {
        types = ImmutableList.copyOf(Iterables.concat(types, ImmutableList.of(BIGINT)));
    }
    PageBuilder pageBuilder = new PageBuilder(types);
    for (int position = 0; position < positionCount; position++) {
        int rand = ThreadLocalRandom.current().nextInt(groupCount);
        Slice value = Slices.wrappedBuffer(ByteBuffer.allocate(4).putInt(rand));
        pageBuilder.declarePosition();
        for (int channel = 0; channel < channelCount; channel++) {
            VARCHAR.writeSlice(pageBuilder.getBlockBuilder(channel), value);
        }
        if (hashEnabled) {
            BIGINT.writeLong(pageBuilder.getBlockBuilder(channelCount), VarcharOperators.hashCode(value));
        }
        if (pageBuilder.isFull()) {
            pages.add(pageBuilder.build());
            pageBuilder.reset();
        }
    }
    pages.add(pageBuilder.build());
    return pages.build();
}
Also used : Type(io.prestosql.spi.type.Type) ImmutableList(com.google.common.collect.ImmutableList) Slice(io.airlift.slice.Slice) Page(io.prestosql.spi.Page) PageBuilder(io.prestosql.spi.PageBuilder)

Example 25 with PageBuilder

use of io.prestosql.spi.PageBuilder in project hetu-core by openlookeng.

the class BenchmarkGroupByHash method addPagePreCompute.

@Benchmark
@OperationsPerInvocation(POSITIONS)
public Object addPagePreCompute(BenchmarkData data) {
    GroupByHash groupByHash = new MultiChannelGroupByHash(data.getTypes(), data.getChannels(), data.getHashChannel(), EXPECTED_SIZE, false, getJoinCompiler(), NOOP);
    data.getPages().forEach(p -> groupByHash.addPage(p).process());
    ImmutableList.Builder<Page> pages = ImmutableList.builder();
    PageBuilder pageBuilder = new PageBuilder(groupByHash.getTypes());
    for (int groupId = 0; groupId < groupByHash.getGroupCount(); groupId++) {
        pageBuilder.declarePosition();
        groupByHash.appendValuesTo(groupId, pageBuilder, 0);
        if (pageBuilder.isFull()) {
            pages.add(pageBuilder.build());
            pageBuilder.reset();
        }
    }
    pages.add(pageBuilder.build());
    return pageBuilder.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Page(io.prestosql.spi.Page) PageBuilder(io.prestosql.spi.PageBuilder) Benchmark(org.openjdk.jmh.annotations.Benchmark) OperationsPerInvocation(org.openjdk.jmh.annotations.OperationsPerInvocation)

Aggregations

PageBuilder (io.prestosql.spi.PageBuilder)58 Page (io.prestosql.spi.Page)28 BlockBuilder (io.prestosql.spi.block.BlockBuilder)27 Type (io.prestosql.spi.type.Type)24 ImmutableList (com.google.common.collect.ImmutableList)18 Test (org.testng.annotations.Test)14 List (java.util.List)11 Block (io.prestosql.spi.block.Block)10 ArrayList (java.util.ArrayList)9 INTEGER (io.prestosql.spi.type.IntegerType.INTEGER)8 Collectors.toList (java.util.stream.Collectors.toList)8 Slice (io.airlift.slice.Slice)7 Benchmark (org.openjdk.jmh.annotations.Benchmark)7 Slices (io.airlift.slice.Slices)6 ConnectorPageSource (io.prestosql.spi.connector.ConnectorPageSource)6 ConnectorSession (io.prestosql.spi.connector.ConnectorSession)6 ArrayType (io.prestosql.spi.type.ArrayType)6 BIGINT (io.prestosql.spi.type.BigintType.BIGINT)6 DOUBLE (io.prestosql.spi.type.DoubleType.DOUBLE)6 VarcharType.createUnboundedVarcharType (io.prestosql.spi.type.VarcharType.createUnboundedVarcharType)6