Java Code Examples for io.prestosql.spi.type.Type#appendTo()

The following examples show how to use io.prestosql.spi.type.Type#appendTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenericPartitioningSpiller.java    From presto with Apache License 2.0 7 votes vote down vote up
private synchronized IntArrayList partitionPage(Page page, IntPredicate spillPartitionMask)
{
    IntArrayList unspilledPositions = new IntArrayList();

    for (int position = 0; position < page.getPositionCount(); position++) {
        int partition = partitionFunction.getPartition(page, position);

        if (!spillPartitionMask.test(partition)) {
            unspilledPositions.add(position);
            continue;
        }

        spilledPartitions.add(partition);
        PageBuilder pageBuilder = pageBuilders.get(partition);
        pageBuilder.declarePosition();
        for (int channel = 0; channel < types.size(); channel++) {
            Type type = types.get(channel);
            type.appendTo(page.getBlock(channel), position, pageBuilder.getBlockBuilder(channel));
        }
    }

    return unspilledPositions;
}
 
Example 2
Source File: PagesIndex.java    From presto with Apache License 2.0 6 votes vote down vote up
public int buildPage(int position, int[] outputChannels, PageBuilder pageBuilder)
{
    while (!pageBuilder.isFull() && position < positionCount) {
        long pageAddress = valueAddresses.getLong(position);
        int blockIndex = decodeSliceIndex(pageAddress);
        int blockPosition = decodePosition(pageAddress);

        // append the row
        pageBuilder.declarePosition();
        for (int i = 0; i < outputChannels.length; i++) {
            int outputChannel = outputChannels[i];
            Type type = types.get(outputChannel);
            Block block = this.channels[outputChannel].get(blockIndex);
            type.appendTo(block, blockPosition, pageBuilder.getBlockBuilder(i));
        }

        position++;
    }

    return position;
}
 
Example 3
Source File: ArrayReverseFunction.java    From presto with Apache License 2.0 6 votes vote down vote up
@TypeParameter("E")
@SqlType("array(E)")
public Block reverse(
        @TypeParameter("E") Type type,
        @SqlType("array(E)") Block block)
{
    int arrayLength = block.getPositionCount();

    if (arrayLength < 2) {
        return block;
    }

    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }

    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    for (int i = arrayLength - 1; i >= 0; i--) {
        type.appendTo(block, i, blockBuilder);
    }
    pageBuilder.declarePositions(arrayLength);

    return blockBuilder.getRegion(blockBuilder.getPositionCount() - arrayLength, arrayLength);
}
 
Example 4
Source File: AbstractTestAggregationFunction.java    From presto with Apache License 2.0 6 votes vote down vote up
protected static Block[] createAlternatingNullsBlock(List<Type> types, Block... sequenceBlocks)
{
    Block[] alternatingNullsBlocks = new Block[sequenceBlocks.length];
    for (int i = 0; i < sequenceBlocks.length; i++) {
        int positionCount = sequenceBlocks[i].getPositionCount();
        Type type = types.get(i);
        BlockBuilder blockBuilder = type.createBlockBuilder(null, positionCount);
        for (int position = 0; position < positionCount; position++) {
            // append null
            blockBuilder.appendNull();
            // append value
            type.appendTo(sequenceBlocks[i], position, blockBuilder);
        }
        alternatingNullsBlocks[i] = blockBuilder.build();
    }
    return alternatingNullsBlocks;
}
 
Example 5
Source File: StreamingAggregationOperator.java    From presto with Apache License 2.0 6 votes vote down vote up
private void evaluateAndFlushGroup(Page page, int position)
{
    pageBuilder.declarePosition();
    for (int i = 0; i < groupByTypes.size(); i++) {
        Block block = page.getBlock(groupByChannels[i]);
        Type type = groupByTypes.get(i);
        type.appendTo(block, position, pageBuilder.getBlockBuilder(i));
    }
    int offset = groupByTypes.size();
    for (int i = 0; i < aggregates.size(); i++) {
        aggregates.get(i).evaluate(pageBuilder.getBlockBuilder(offset + i));
    }

    if (pageBuilder.isFull()) {
        outputPages.add(pageBuilder.build());
        pageBuilder.reset();
    }

    aggregates = setupAggregates(step, accumulatorFactories);
}
 
Example 6
Source File: AbstractMinMaxNAggregationFunction.java    From presto with Apache License 2.0 6 votes vote down vote up
public static void output(ArrayType outputType, MinMaxNState state, BlockBuilder out)
{
    TypedHeap heap = state.getTypedHeap();
    if (heap == null || heap.isEmpty()) {
        out.appendNull();
        return;
    }

    Type elementType = outputType.getElementType();

    BlockBuilder reversedBlockBuilder = elementType.createBlockBuilder(null, heap.getCapacity());
    long startSize = heap.getEstimatedSize();
    heap.popAll(reversedBlockBuilder);
    state.addMemoryUsage(heap.getEstimatedSize() - startSize);

    BlockBuilder arrayBlockBuilder = out.beginBlockEntry();
    for (int i = reversedBlockBuilder.getPositionCount() - 1; i >= 0; i--) {
        elementType.appendTo(reversedBlockBuilder, i, arrayBlockBuilder);
    }
    out.closeEntry();
}
 
Example 7
Source File: ValueStore.java    From presto with Apache License 2.0 5 votes vote down vote up
/**
 * This will add an item if not already in the system. It returns a pointer that is unique for multiple instances of the value. If item present,
 * returns the pointer into the system
 */
public int addAndGetPosition(Type type, Block block, int position, long valueHash)
{
    if (values.getPositionCount() >= maxFill) {
        rehash();
    }

    int bucketId = getBucketId(valueHash, mask);
    int valuePointer;

    // look for an empty slot or a slot containing this key
    int probeCount = 1;
    int originalBucketId = bucketId;
    while (true) {
        checkState(probeCount < bucketCount, "could not find match for value nor empty slot in %s buckets", bucketCount);
        valuePointer = buckets.get(bucketId);

        if (valuePointer == EMPTY_BUCKET) {
            valuePointer = values.getPositionCount();
            valueHashes.set(valuePointer, (int) valueHash);
            type.appendTo(block, position, values);
            buckets.set(bucketId, valuePointer);

            return valuePointer;
        }
        else if (type.equalTo(block, position, values, valuePointer)) {
            // value at position
            return valuePointer;
        }
        else {
            int probe = nextProbe(probeCount);
            bucketId = nextBucketId(originalBucketId, mask, probe);
            probeCount++;
        }
    }
}
 
Example 8
Source File: ArrayConcatFunction.java    From presto with Apache License 2.0 5 votes vote down vote up
@UsedByGeneratedCode
public static Block concat(Type elementType, Object state, Block[] blocks)
{
    int resultPositionCount = 0;

    // fast path when there is at most one non empty block
    Block nonEmptyBlock = null;
    for (int i = 0; i < blocks.length; i++) {
        resultPositionCount += blocks[i].getPositionCount();
        if (blocks[i].getPositionCount() > 0) {
            nonEmptyBlock = blocks[i];
        }
    }
    if (nonEmptyBlock == null) {
        return blocks[0];
    }
    if (resultPositionCount == nonEmptyBlock.getPositionCount()) {
        return nonEmptyBlock;
    }

    PageBuilder pageBuilder = (PageBuilder) state;
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }

    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    for (int blockIndex = 0; blockIndex < blocks.length; blockIndex++) {
        Block block = blocks[blockIndex];
        for (int i = 0; i < block.getPositionCount(); i++) {
            elementType.appendTo(block, i, blockBuilder);
        }
    }
    pageBuilder.declarePositions(resultPositionCount);
    return blockBuilder.getRegion(blockBuilder.getPositionCount() - resultPositionCount, resultPositionCount);
}
 
Example 9
Source File: ArrayConcatUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
@UsedByGeneratedCode
public static Block appendElement(Type elementType, Block block, boolean value)
{
    BlockBuilder blockBuilder = elementType.createBlockBuilder(null, block.getPositionCount() + 1);
    for (int i = 0; i < block.getPositionCount(); i++) {
        elementType.appendTo(block, i, blockBuilder);
    }

    elementType.writeBoolean(blockBuilder, value);

    return blockBuilder.build();
}
 
Example 10
Source File: ArrayConcatUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
@UsedByGeneratedCode
public static Block prependElement(Type elementType, double value, Block block)
{
    BlockBuilder blockBuilder = elementType.createBlockBuilder(null, block.getPositionCount() + 1);

    elementType.writeDouble(blockBuilder, value);
    for (int i = 0; i < block.getPositionCount(); i++) {
        elementType.appendTo(block, i, blockBuilder);
    }

    return blockBuilder.build();
}
 
Example 11
Source File: ArrayConcatUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
@UsedByGeneratedCode
public static Block appendElement(Type elementType, Block block, double value)
{
    BlockBuilder blockBuilder = elementType.createBlockBuilder(null, block.getPositionCount() + 1);
    for (int i = 0; i < block.getPositionCount(); i++) {
        elementType.appendTo(block, i, blockBuilder);
    }

    elementType.writeDouble(blockBuilder, value);

    return blockBuilder.build();
}
 
Example 12
Source File: StreamingIndexedData.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void appendTo(long position, PageBuilder pageBuilder, int outputChannelOffset)
{
    // TODO: use the code generator here
    checkState(currentPage != null, "getJoinPosition not called first");
    int intPosition = toIntExact(position);
    for (int i = 0; i < outputTypes.size(); i++) {
        Type type = outputTypes.get(i);
        Block block = currentPage.getBlock(i);
        BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(i + outputChannelOffset);
        type.appendTo(block, intPosition, blockBuilder);
    }
}
 
Example 13
Source File: ArrayUnionFunction.java    From presto with Apache License 2.0 5 votes vote down vote up
private static void appendTypedArray(Block array, Type type, TypedSet typedSet, BlockBuilder blockBuilder)
{
    for (int i = 0; i < array.getPositionCount(); i++) {
        if (!typedSet.contains(array, i)) {
            typedSet.add(array, i);
            type.appendTo(array, i, blockBuilder);
        }
    }
}
 
Example 14
Source File: ArrayShuffleFunction.java    From presto with Apache License 2.0 5 votes vote down vote up
@TypeParameter("E")
@SqlType("array(E)")
public Block shuffle(
        @TypeParameter("E") Type type,
        @SqlType("array(E)") Block block)
{
    int length = block.getPositionCount();
    if (positions.length < length) {
        positions = new int[length];
    }
    for (int i = 0; i < length; i++) {
        positions[i] = i;
    }

    // Fisher-Yates shuffle
    // Randomly swap a pair of positions
    for (int i = length - 1; i > 0; i--) {
        int index = ThreadLocalRandom.current().nextInt(i + 1);
        int swap = positions[i];
        positions[i] = positions[index];
        positions[index] = swap;
    }

    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }

    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);

    for (int i = 0; i < length; i++) {
        type.appendTo(block, positions[i], blockBuilder);
    }
    pageBuilder.declarePositions(length);

    return blockBuilder.getRegion(blockBuilder.getPositionCount() - length, length);
}
 
Example 15
Source File: ArrayConcatUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
@UsedByGeneratedCode
public static Block appendElement(Type elementType, Block block, Object value)
{
    BlockBuilder blockBuilder = elementType.createBlockBuilder(null, block.getPositionCount() + 1);
    for (int i = 0; i < block.getPositionCount(); i++) {
        elementType.appendTo(block, i, blockBuilder);
    }

    elementType.writeObject(blockBuilder, value);

    return blockBuilder.build();
}
 
Example 16
Source File: BlockPositionState.java    From presto with Apache License 2.0 5 votes vote down vote up
static void write(Type type, BlockPositionState state, BlockBuilder out)
{
    if (state.getBlock() == null) {
        out.appendNull();
    }
    else {
        type.appendTo(state.getBlock(), state.getPosition(), out);
    }
}
 
Example 17
Source File: ArrayConcatUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
@UsedByGeneratedCode
public static Block prependElement(Type elementType, Object value, Block block)
{
    BlockBuilder blockBuilder = elementType.createBlockBuilder(null, block.getPositionCount() + 1);

    elementType.writeObject(blockBuilder, value);
    for (int i = 0; i < block.getPositionCount(); i++) {
        elementType.appendTo(block, i, blockBuilder);
    }

    return blockBuilder.build();
}
 
Example 18
Source File: SortBuffer.java    From presto with Apache License 2.0 5 votes vote down vote up
public static void appendPositionTo(Page page, int position, PageBuilder pageBuilder)
{
    pageBuilder.declarePosition();
    for (int i = 0; i < page.getChannelCount(); i++) {
        Type type = pageBuilder.getType(i);
        Block block = page.getBlock(i);
        BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(i);
        type.appendTo(block, position, blockBuilder);
    }
}
 
Example 19
Source File: PartitionedOutputOperator.java    From presto with Apache License 2.0 5 votes vote down vote up
private void appendRow(PageBuilder pageBuilder, Page page, int position)
{
    pageBuilder.declarePosition();

    for (int channel = 0; channel < sourceTypes.size(); channel++) {
        Type type = sourceTypes.get(channel);
        type.appendTo(page.getBlock(channel), position, pageBuilder.getBlockBuilder(channel));
    }
}
 
Example 20
Source File: MapFromEntriesFunction.java    From presto with Apache License 2.0 4 votes vote down vote up
@TypeParameter("K")
@TypeParameter("V")
@SqlType("map(K,V)")
@SqlNullable
public Block mapFromEntries(
        @TypeParameter("map(K,V)") MapType mapType,
        ConnectorSession session,
        @SqlType("array(row(K,V))") Block block)
{
    Type keyType = mapType.getKeyType();
    Type valueType = mapType.getValueType();
    RowType rowType = RowType.anonymous(ImmutableList.of(keyType, valueType));

    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }

    int entryCount = block.getPositionCount();

    BlockBuilder mapBlockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder resultBuilder = mapBlockBuilder.beginBlockEntry();
    TypedSet uniqueKeys = new TypedSet(keyType, entryCount, "map_from_entries");

    for (int i = 0; i < entryCount; i++) {
        if (block.isNull(i)) {
            mapBlockBuilder.closeEntry();
            pageBuilder.declarePosition();
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map entry cannot be null");
        }
        Block rowBlock = rowType.getObject(block, i);

        if (rowBlock.isNull(0)) {
            mapBlockBuilder.closeEntry();
            pageBuilder.declarePosition();
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
        }

        if (uniqueKeys.contains(rowBlock, 0)) {
            mapBlockBuilder.closeEntry();
            pageBuilder.declarePosition();
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Duplicate keys (%s) are not allowed", keyType.getObjectValue(session, rowBlock, 0)));
        }
        uniqueKeys.add(rowBlock, 0);

        keyType.appendTo(rowBlock, 0, resultBuilder);
        valueType.appendTo(rowBlock, 1, resultBuilder);
    }

    mapBlockBuilder.closeEntry();
    pageBuilder.declarePosition();
    return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}