Search in sources :

Example 16 with BlockBuilder

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

the class ArrayFilterFunction method filterBoolean.

@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = boolean.class)
@SqlType("array(T)")
public static Block filterBoolean(@TypeParameter("T") Type elementType, @SqlType("array(T)") Block arrayBlock, @SqlType("function(T, boolean)") BooleanToBooleanFunction function) {
    int positionCount = arrayBlock.getPositionCount();
    BlockBuilder resultBuilder = elementType.createBlockBuilder(null, positionCount);
    for (int position = 0; position < positionCount; position++) {
        Boolean input = null;
        if (!arrayBlock.isNull(position)) {
            input = elementType.getBoolean(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 17 with BlockBuilder

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

the class PagePartitioner method appendToOutputPartition.

private void appendToOutputPartition(PageBuilder outputPartition, Page page, IntArrayList positions, PositionsAppender[] positionsAppenders) {
    outputPartition.declarePositions(positions.size());
    for (int channel = 0; channel < positionsAppenders.length; channel++) {
        Block partitionBlock = page.getBlock(channel);
        BlockBuilder target = outputPartition.getBlockBuilder(channel);
        positionsAppenders[channel].appendTo(positions, partitionBlock, target);
    }
}
Also used : DictionaryBlock(io.trino.spi.block.DictionaryBlock) Block(io.trino.spi.block.Block) RunLengthEncodedBlock(io.trino.spi.block.RunLengthEncodedBlock) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 18 with BlockBuilder

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

the class PropertyUtil method evaluateProperty.

private static Object evaluateProperty(Expression expression, PropertyMetadata<?> property, Session session, PlannerContext plannerContext, AccessControl accessControl, Map<NodeRef<Parameter>, Expression> parameters, ErrorCodeSupplier errorCode, String propertyTypeDescription) {
    Object sqlObjectValue;
    try {
        Type expectedType = property.getSqlType();
        Expression rewritten = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(parameters), expression);
        Object value = evaluateConstantExpression(rewritten, expectedType, plannerContext, session, accessControl, parameters);
        // convert to object value type of SQL type
        BlockBuilder blockBuilder = expectedType.createBlockBuilder(null, 1);
        writeNativeValue(expectedType, blockBuilder, value);
        sqlObjectValue = expectedType.getObjectValue(session.toConnectorSession(), blockBuilder, 0);
    } catch (TrinoException e) {
        throw new TrinoException(errorCode, format("Invalid value for %s '%s': Cannot convert [%s] to %s", propertyTypeDescription, property.getName(), expression, property.getSqlType()), e);
    }
    if (sqlObjectValue == null) {
        throw new TrinoException(errorCode, format("Invalid null value for %s '%s' from [%s]", propertyTypeDescription, property.getName(), expression));
    }
    try {
        return property.decode(sqlObjectValue);
    } catch (Exception e) {
        throw new TrinoException(errorCode, format("Unable to set %s '%s' to [%s]: %s", propertyTypeDescription, property.getName(), expression, e.getMessage()), e);
    }
}
Also used : Type(io.trino.spi.type.Type) ParameterRewriter(io.trino.sql.planner.ParameterRewriter) ExpressionInterpreter.evaluateConstantExpression(io.trino.sql.planner.ExpressionInterpreter.evaluateConstantExpression) Expression(io.trino.sql.tree.Expression) TrinoException(io.trino.spi.TrinoException) TrinoException(io.trino.spi.TrinoException) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 19 with BlockBuilder

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

the class ArrayAggregationStateSerializer method serialize.

@Override
public void serialize(ArrayAggregationState state, BlockBuilder out) {
    if (state.isEmpty()) {
        out.appendNull();
    } else {
        BlockBuilder entryBuilder = out.beginBlockEntry();
        state.forEach((block, position) -> elementType.appendTo(block, position, entryBuilder));
        out.closeEntry();
    }
}
Also used : BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 20 with BlockBuilder

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

the class DoubleHistogramAggregation method output.

@OutputFunction("map(double,double)")
public static void output(@AggregationState State state, BlockBuilder out) {
    if (state.get() == null) {
        out.appendNull();
    } else {
        Map<Double, Double> value = state.get().getBuckets();
        BlockBuilder entryBuilder = out.beginBlockEntry();
        for (Map.Entry<Double, Double> entry : value.entrySet()) {
            DoubleType.DOUBLE.writeDouble(entryBuilder, entry.getKey());
            DoubleType.DOUBLE.writeDouble(entryBuilder, entry.getValue());
        }
        out.closeEntry();
    }
}
Also used : Map(java.util.Map) BlockBuilder(io.trino.spi.block.BlockBuilder) OutputFunction(io.trino.spi.function.OutputFunction)

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