Search in sources :

Example 11 with BlockBuilder

use of io.trino.spi.block.BlockBuilder in project trino by trinodb.

the class ArrayConcatUtils method prependElement.

// Usage of prependElement: ElementToArrayConcatFunction
@UsedByGeneratedCode
public static Block prependElement(Type elementType, Slice value, Block block) {
    BlockBuilder blockBuilder = elementType.createBlockBuilder(null, block.getPositionCount() + 1);
    elementType.writeSlice(blockBuilder, value);
    for (int i = 0; i < block.getPositionCount(); i++) {
        elementType.appendTo(block, i, blockBuilder);
    }
    return blockBuilder.build();
}
Also used : BlockBuilder(io.trino.spi.block.BlockBuilder) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Example 12 with BlockBuilder

use of io.trino.spi.block.BlockBuilder in project trino by trinodb.

the class ArrayConcatUtils method appendElement.

@UsedByGeneratedCode
public static Block appendElement(Type elementType, Block block, Slice value) {
    BlockBuilder blockBuilder = elementType.createBlockBuilder(null, block.getPositionCount() + 1);
    for (int i = 0; i < block.getPositionCount(); i++) {
        elementType.appendTo(block, i, blockBuilder);
    }
    elementType.writeSlice(blockBuilder, value);
    return blockBuilder.build();
}
Also used : BlockBuilder(io.trino.spi.block.BlockBuilder) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Example 13 with BlockBuilder

use of io.trino.spi.block.BlockBuilder in project trino by trinodb.

the class ArrayDistinctFunction method bigintDistinct.

@SqlType("array(bigint)")
public Block bigintDistinct(@SqlType("array(bigint)") Block array) {
    if (array.getPositionCount() == 0) {
        return array;
    }
    boolean containsNull = false;
    LongSet set = new LongOpenHashSet(array.getPositionCount());
    int distinctCount = 0;
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder distinctElementBlockBuilder = pageBuilder.getBlockBuilder(0);
    for (int i = 0; i < array.getPositionCount(); i++) {
        if (array.isNull(i)) {
            if (!containsNull) {
                containsNull = true;
                distinctElementBlockBuilder.appendNull();
                distinctCount++;
            }
            continue;
        }
        long value = BIGINT.getLong(array, i);
        if (!set.contains(value)) {
            set.add(value);
            distinctCount++;
            BIGINT.appendTo(array, i, distinctElementBlockBuilder);
        }
    }
    pageBuilder.declarePositions(distinctCount);
    return distinctElementBlockBuilder.getRegion(distinctElementBlockBuilder.getPositionCount() - distinctCount, distinctCount);
}
Also used : LongSet(it.unimi.dsi.fastutil.longs.LongSet) LongOpenHashSet(it.unimi.dsi.fastutil.longs.LongOpenHashSet) BlockBuilder(io.trino.spi.block.BlockBuilder) SqlType(io.trino.spi.function.SqlType)

Example 14 with BlockBuilder

use of io.trino.spi.block.BlockBuilder in project trino by trinodb.

the class ArrayFilterFunction method filterDouble.

@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = double.class)
@SqlType("array(T)")
public static Block filterDouble(@TypeParameter("T") Type elementType, @SqlType("array(T)") Block arrayBlock, @SqlType("function(T, boolean)") DoubleToBooleanFunction function) {
    int positionCount = arrayBlock.getPositionCount();
    BlockBuilder resultBuilder = elementType.createBlockBuilder(null, positionCount);
    for (int position = 0; position < positionCount; position++) {
        Double input = null;
        if (!arrayBlock.isNull(position)) {
            input = elementType.getDouble(arrayBlock, position);
        }
        Boolean keep = function.apply(input);
        if (TRUE.equals(keep)) {
            elementType.appendTo(arrayBlock, position, resultBuilder);
        }
    }
    return resultBuilder.build();
}
Also used : BlockBuilder(io.trino.spi.block.BlockBuilder) TypeParameterSpecialization(io.trino.spi.function.TypeParameterSpecialization) TypeParameter(io.trino.spi.function.TypeParameter) SqlType(io.trino.spi.function.SqlType)

Example 15 with BlockBuilder

use of io.trino.spi.block.BlockBuilder in project trino by trinodb.

the class ArrayFilterFunction method filterObject.

@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = Object.class)
@SqlType("array(T)")
public static Block filterObject(@TypeParameter("T") Type elementType, @SqlType("array(T)") Block arrayBlock, @SqlType("function(T, boolean)") ObjectToBooleanFunction function) {
    int positionCount = arrayBlock.getPositionCount();
    BlockBuilder resultBuilder = elementType.createBlockBuilder(null, positionCount);
    for (int position = 0; position < positionCount; position++) {
        Object input = null;
        if (!arrayBlock.isNull(position)) {
            input = elementType.getObject(arrayBlock, position);
        }
        Boolean keep = function.apply(input);
        if (TRUE.equals(keep)) {
            elementType.appendTo(arrayBlock, position, resultBuilder);
        }
    }
    return resultBuilder.build();
}
Also used : BlockBuilder(io.trino.spi.block.BlockBuilder) TypeParameterSpecialization(io.trino.spi.function.TypeParameterSpecialization) TypeParameter(io.trino.spi.function.TypeParameter) SqlType(io.trino.spi.function.SqlType)

Aggregations

BlockBuilder (io.trino.spi.block.BlockBuilder)459 Block (io.trino.spi.block.Block)98 Test (org.testng.annotations.Test)89 Slice (io.airlift.slice.Slice)87 Type (io.trino.spi.type.Type)61 SqlType (io.trino.spi.function.SqlType)52 Page (io.trino.spi.Page)41 ArrayType (io.trino.spi.type.ArrayType)41 RowType (io.trino.spi.type.RowType)35 MapType (io.trino.spi.type.MapType)34 Map (java.util.Map)25 PageBuilder (io.trino.spi.PageBuilder)22 VarcharType (io.trino.spi.type.VarcharType)22 ScalarFunction (io.trino.spi.function.ScalarFunction)21 TypeParameter (io.trino.spi.function.TypeParameter)20 UsedByGeneratedCode (io.trino.annotation.UsedByGeneratedCode)19 TrinoException (io.trino.spi.TrinoException)18 RowBlockBuilder (io.trino.spi.block.RowBlockBuilder)18 VariableWidthBlockBuilder (io.trino.spi.block.VariableWidthBlockBuilder)17 Description (io.trino.spi.function.Description)16