Search in sources :

Example 1 with BlockBuilder

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

the class QuerySystemTable method resourceGroupIdToBlock.

private static Block resourceGroupIdToBlock(ResourceGroupId resourceGroupId) {
    requireNonNull(resourceGroupId, "resourceGroupId is null");
    List<String> segments = resourceGroupId.getSegments();
    BlockBuilder blockBuilder = createUnboundedVarcharType().createBlockBuilder(null, segments.size());
    for (String segment : segments) {
        createUnboundedVarcharType().writeSlice(blockBuilder, utf8Slice(segment));
    }
    return blockBuilder.build();
}
Also used : BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 2 with BlockBuilder

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

the class RuleStatsSystemTable method pageSource.

@Override
public ConnectorPageSource pageSource(ConnectorTransactionHandle transactionHandle, ConnectorSession session, TupleDomain<Integer> constraint) {
    checkState(ruleStatsRecorder.isPresent(), "Rule stats system table can return results only on coordinator");
    Map<Class<?>, RuleStats> ruleStats = ruleStatsRecorder.get().getStats();
    int positionCount = ruleStats.size();
    Map<String, BlockBuilder> blockBuilders = ruleStatsTable.getColumns().stream().collect(toImmutableMap(ColumnMetadata::getName, column -> column.getType().createBlockBuilder(null, positionCount)));
    for (Map.Entry<Class<?>, RuleStats> entry : ruleStats.entrySet()) {
        RuleStats stats = entry.getValue();
        VARCHAR.writeString(blockBuilders.get("rule_name"), entry.getKey().getSimpleName());
        BIGINT.writeLong(blockBuilders.get("invocations"), stats.getInvocations());
        BIGINT.writeLong(blockBuilders.get("matches"), stats.getHits());
        BIGINT.writeLong(blockBuilders.get("failures"), stats.getFailures());
        DOUBLE.writeDouble(blockBuilders.get("average_time"), stats.getTime().getAvg());
        BlockBuilder mapWriter = blockBuilders.get("time_distribution_percentiles").beginBlockEntry();
        for (Map.Entry<Double, Double> percentile : stats.getTime().getPercentiles().entrySet()) {
            DOUBLE.writeDouble(mapWriter, percentile.getKey());
            DOUBLE.writeDouble(mapWriter, percentile.getValue());
        }
        blockBuilders.get("time_distribution_percentiles").closeEntry();
    }
    Block[] blocks = ruleStatsTable.getColumns().stream().map(column -> blockBuilders.get(column.getName()).build()).toArray(Block[]::new);
    return new FixedPageSource(ImmutableList.of(new Page(positionCount, blocks)));
}
Also used : TableMetadataBuilder.tableMetadataBuilder(io.trino.metadata.MetadataUtil.TableMetadataBuilder.tableMetadataBuilder) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) Page(io.trino.spi.Page) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) RuleStatsRecorder(io.trino.sql.planner.RuleStatsRecorder) Inject(javax.inject.Inject) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) Block(io.trino.spi.block.Block) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) ConnectorPageSource(io.trino.spi.connector.ConnectorPageSource) SINGLE_COORDINATOR(io.trino.spi.connector.SystemTable.Distribution.SINGLE_COORDINATOR) ConnectorSession(io.trino.spi.connector.ConnectorSession) TupleDomain(io.trino.spi.predicate.TupleDomain) SchemaTableName(io.trino.spi.connector.SchemaTableName) Preconditions.checkState(com.google.common.base.Preconditions.checkState) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) RuleStats(io.trino.sql.planner.iterative.RuleStats) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) FixedPageSource(io.trino.spi.connector.FixedPageSource) BIGINT(io.trino.spi.type.BigintType.BIGINT) Optional(java.util.Optional) BlockBuilder(io.trino.spi.block.BlockBuilder) TypeSignature.mapType(io.trino.spi.type.TypeSignature.mapType) TypeManager(io.trino.spi.type.TypeManager) SystemTable(io.trino.spi.connector.SystemTable) ConnectorTransactionHandle(io.trino.spi.connector.ConnectorTransactionHandle) Page(io.trino.spi.Page) FixedPageSource(io.trino.spi.connector.FixedPageSource) RuleStats(io.trino.sql.planner.iterative.RuleStats) Block(io.trino.spi.block.Block) Map(java.util.Map) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 3 with BlockBuilder

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

the class TransactionsSystemTable method createStringsBlock.

private static Block createStringsBlock(List<CatalogName> values) {
    VarcharType varchar = createUnboundedVarcharType();
    BlockBuilder builder = varchar.createBlockBuilder(null, values.size());
    for (CatalogName value : values) {
        if (value == null) {
            builder.appendNull();
        } else {
            varchar.writeString(builder, value.getCatalogName());
        }
    }
    return builder.build();
}
Also used : VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(io.trino.spi.type.VarcharType) CatalogName(io.trino.connector.CatalogName) BlockBuilder(io.trino.spi.block.BlockBuilder)

Example 4 with BlockBuilder

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

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(io.trino.spi.block.Block) PageBuilder(io.trino.spi.PageBuilder) BlockBuilder(io.trino.spi.block.BlockBuilder) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Example 5 with BlockBuilder

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

the class ArrayToArrayCast method filterObject.

@TypeParameter("F")
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = Object.class)
@SqlType("array(T)")
public static Block filterObject(@TypeParameter("T") Type resultType, @CastDependency(fromType = "F", toType = "T", convention = @Convention(arguments = BLOCK_POSITION, result = NULLABLE_RETURN, session = true)) MethodHandle cast, ConnectorSession session, @SqlType("array(F)") Block array) throws Throwable {
    int positionCount = array.getPositionCount();
    BlockBuilder resultBuilder = resultType.createBlockBuilder(null, positionCount);
    for (int position = 0; position < positionCount; position++) {
        if (!array.isNull(position)) {
            Object value = (Object) cast.invoke(session, array, position);
            if (value != null) {
                resultType.writeObject(resultBuilder, value);
                continue;
            }
        }
        resultBuilder.appendNull();
    }
    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