Search in sources :

Example 1 with UsedByGeneratedCode

use of com.facebook.presto.annotation.UsedByGeneratedCode in project presto by prestodb.

the class ArrayConcatFunction method concat.

@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);
}
Also used : Block(com.facebook.presto.spi.block.Block) PageBuilder(com.facebook.presto.spi.PageBuilder) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 2 with UsedByGeneratedCode

use of com.facebook.presto.annotation.UsedByGeneratedCode in project presto by prestodb.

the class ArrayConcatUtils method appendElement.

@UsedByGeneratedCode
public static Block appendElement(Type elementType, Block block, boolean value) {
    BlockBuilder blockBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), block.getPositionCount() + 1);
    for (int i = 0; i < block.getPositionCount(); i++) {
        elementType.appendTo(block, i, blockBuilder);
    }
    elementType.writeBoolean(blockBuilder, value);
    return blockBuilder.build();
}
Also used : BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 3 with UsedByGeneratedCode

use of com.facebook.presto.annotation.UsedByGeneratedCode in project presto by prestodb.

the class JsonToArrayCast method toArray.

@UsedByGeneratedCode
public static Block toArray(Type arrayType, ConnectorSession connectorSession, Slice json) {
    try {
        List<?> array = (List<?>) stackRepresentationToObject(connectorSession, json, arrayType);
        if (array == null) {
            return null;
        }
        Type elementType = ((ArrayType) arrayType).getElementType();
        BlockBuilder blockBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), array.size());
        for (Object element : array) {
            appendToBlockBuilder(elementType, element, blockBuilder);
        }
        return blockBuilder.build();
    } catch (RuntimeException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to " + arrayType, e);
    }
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) ArrayType(com.facebook.presto.type.ArrayType) Type(com.facebook.presto.spi.type.Type) OperatorType(com.facebook.presto.spi.function.OperatorType) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) TypeJsonUtils.stackRepresentationToObject(com.facebook.presto.type.TypeJsonUtils.stackRepresentationToObject) PrestoException(com.facebook.presto.spi.PrestoException) TypeJsonUtils.appendToBlockBuilder(com.facebook.presto.type.TypeJsonUtils.appendToBlockBuilder) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 4 with UsedByGeneratedCode

use of com.facebook.presto.annotation.UsedByGeneratedCode in project presto by prestodb.

the class ArrayConcatUtils method appendElement.

@UsedByGeneratedCode
public static Block appendElement(Type elementType, Block block, long value) {
    BlockBuilder blockBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), block.getPositionCount() + 1);
    for (int i = 0; i < block.getPositionCount(); i++) {
        elementType.appendTo(block, i, blockBuilder);
    }
    elementType.writeLong(blockBuilder, value);
    return blockBuilder.build();
}
Also used : BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 5 with UsedByGeneratedCode

use of com.facebook.presto.annotation.UsedByGeneratedCode in project presto by prestodb.

the class ZipFunction method zip.

@UsedByGeneratedCode
public static Block zip(List<Type> types, Block... arrays) {
    int biggestCardinality = 0;
    for (Block array : arrays) {
        biggestCardinality = Math.max(biggestCardinality, array.getPositionCount());
    }
    RowType rowType = new RowType(types, Optional.empty());
    BlockBuilder outputBuilder = rowType.createBlockBuilder(new BlockBuilderStatus(), biggestCardinality);
    for (int outputPosition = 0; outputPosition < biggestCardinality; outputPosition++) {
        BlockBuilder rowBuilder = outputBuilder.beginBlockEntry();
        for (int fieldIndex = 0; fieldIndex < arrays.length; fieldIndex++) {
            if (arrays[fieldIndex].getPositionCount() <= outputPosition) {
                rowBuilder.appendNull();
            } else {
                types.get(fieldIndex).appendTo(arrays[fieldIndex], outputPosition, rowBuilder);
            }
        }
        outputBuilder.closeEntry();
    }
    return outputBuilder.build();
}
Also used : Block(com.facebook.presto.spi.block.Block) RowType(com.facebook.presto.type.RowType) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Aggregations

UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)23 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)17 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)15 PrestoException (com.facebook.presto.spi.PrestoException)7 Block (com.facebook.presto.spi.block.Block)3 Slice (io.airlift.slice.Slice)3 PageBuilder (com.facebook.presto.spi.PageBuilder)2 InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)2 OperatorType (com.facebook.presto.spi.function.OperatorType)2 Type (com.facebook.presto.spi.type.Type)2 TypeJsonUtils.appendToBlockBuilder (com.facebook.presto.type.TypeJsonUtils.appendToBlockBuilder)2 BigDecimal (java.math.BigDecimal)2 TypedSet (com.facebook.presto.operator.aggregation.TypedSet)1 InterleavedBlock (com.facebook.presto.spi.block.InterleavedBlock)1 ArrayType (com.facebook.presto.type.ArrayType)1 MapType (com.facebook.presto.type.MapType)1 RowType (com.facebook.presto.type.RowType)1 TypeJsonUtils.stackRepresentationToObject (com.facebook.presto.type.TypeJsonUtils.stackRepresentationToObject)1 ImmutableList (com.google.common.collect.ImmutableList)1 DynamicSliceOutput (io.airlift.slice.DynamicSliceOutput)1