Search in sources :

Example 26 with BlockBuilder

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

the class SplitToMapFunction method splitToMap.

@SqlType("map(varchar,varchar)")
public Block splitToMap(@TypeParameter("map(varchar,varchar)") Type mapType, @SqlType(StandardTypes.VARCHAR) Slice string, @SqlType(StandardTypes.VARCHAR) Slice entryDelimiter, @SqlType(StandardTypes.VARCHAR) Slice keyValueDelimiter) {
    checkCondition(entryDelimiter.length() > 0, INVALID_FUNCTION_ARGUMENT, "entryDelimiter is empty");
    checkCondition(keyValueDelimiter.length() > 0, INVALID_FUNCTION_ARGUMENT, "keyValueDelimiter is empty");
    checkCondition(!entryDelimiter.equals(keyValueDelimiter), INVALID_FUNCTION_ARGUMENT, "entryDelimiter and keyValueDelimiter must not be the same");
    Map<Slice, Slice> map = new HashMap<>();
    int entryStart = 0;
    while (entryStart < string.length()) {
        // Extract key-value pair based on current index
        // then add the pair if it can be split by keyValueDelimiter
        Slice keyValuePair;
        int entryEnd = string.indexOf(entryDelimiter, entryStart);
        if (entryEnd >= 0) {
            keyValuePair = string.slice(entryStart, entryEnd - entryStart);
        } else {
            // The rest of the string is the last possible pair.
            keyValuePair = string.slice(entryStart, string.length() - entryStart);
        }
        int keyEnd = keyValuePair.indexOf(keyValueDelimiter);
        if (keyEnd >= 0) {
            int valueStart = keyEnd + keyValueDelimiter.length();
            Slice key = keyValuePair.slice(0, keyEnd);
            Slice value = keyValuePair.slice(valueStart, keyValuePair.length() - valueStart);
            if (value.indexOf(keyValueDelimiter) >= 0) {
                throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Key-value delimiter must appear exactly once in each entry. Bad input: '" + keyValuePair.toStringUtf8() + "'");
            }
            if (map.containsKey(key)) {
                throw new TrinoException(INVALID_FUNCTION_ARGUMENT, format("Duplicate keys (%s) are not allowed", key.toStringUtf8()));
            }
            map.put(key, value);
        } else {
            throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Key-value delimiter must appear exactly once in each entry. Bad input: '" + keyValuePair.toStringUtf8() + "'");
        }
        if (entryEnd < 0) {
            // No more pairs to add
            break;
        }
        // Next possible pair is placed next to the current entryDelimiter
        entryStart = entryEnd + entryDelimiter.length();
    }
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder singleMapBlockBuilder = blockBuilder.beginBlockEntry();
    for (Map.Entry<Slice, Slice> entry : map.entrySet()) {
        VARCHAR.writeSlice(singleMapBlockBuilder, entry.getKey());
        VARCHAR.writeSlice(singleMapBlockBuilder, entry.getValue());
    }
    blockBuilder.closeEntry();
    pageBuilder.declarePosition();
    return (Block) mapType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
}
Also used : HashMap(java.util.HashMap) Slice(io.airlift.slice.Slice) TrinoException(io.trino.spi.TrinoException) Block(io.trino.spi.block.Block) HashMap(java.util.HashMap) Map(java.util.Map) BlockBuilder(io.trino.spi.block.BlockBuilder) SqlType(io.trino.spi.function.SqlType)

Example 27 with BlockBuilder

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

the class RepeatFunction method repeat.

@TypeParameter("T")
@SqlType("array(T)")
public static Block repeat(@TypeParameter("T") Type type, @SqlNullable @SqlType("T") Object element, @SqlType(StandardTypes.INTEGER) long count) {
    BlockBuilder blockBuilder = createBlockBuilder(type, count);
    if (element == null) {
        return repeatNullValues(blockBuilder, count);
    }
    if (count > 0) {
        type.writeObject(blockBuilder, element);
        checkMaxSize(blockBuilder.getSizeInBytes(), count);
    }
    for (int i = 1; i < count; i++) {
        type.writeObject(blockBuilder, element);
    }
    return blockBuilder.build();
}
Also used : BlockBuilder(io.trino.spi.block.BlockBuilder) TypeParameter(io.trino.spi.function.TypeParameter) SqlType(io.trino.spi.function.SqlType)

Example 28 with BlockBuilder

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

the class SequenceIntervalDayToSecond method sequence.

@LiteralParameters("p")
@SqlType("array(timestamp(p))")
public static Block sequence(@SqlType("timestamp(p)") LongTimestamp start, @SqlType("timestamp(p)") LongTimestamp stop, @SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long step) {
    // scale to micros
    step = multiplyExact(step, MICROSECONDS_PER_MILLISECOND);
    long startMicros = start.getEpochMicros();
    long stopMicros = stop.getEpochMicros();
    checkValidStep(startMicros, stopMicros, step);
    int length = toIntExact((stopMicros - startMicros) / step + 1L);
    checkMaxEntry(length);
    BlockBuilder blockBuilder = LONG_TYPE.createBlockBuilder(null, length);
    for (long i = 0, epochMicros = startMicros; i < length; ++i, epochMicros += step) {
        writeLongTimestamp(blockBuilder, epochMicros, start.getPicosOfMicro());
    }
    return blockBuilder.build();
}
Also used : BlockBuilder(io.trino.spi.block.BlockBuilder) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 29 with BlockBuilder

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

the class SequenceIntervalYearToMonth method sequence.

@LiteralParameters("p")
@SqlType("array(timestamp(p))")
public static Block sequence(@SqlType("timestamp(p)") long start, @SqlType("timestamp(p)") long stop, @SqlType(StandardTypes.INTERVAL_YEAR_TO_MONTH) long step) {
    checkValidStep(start, stop, step);
    int length = toIntExact(DateDiff.diff(MONTH, start, stop) / step + 1);
    checkMaxEntry(length);
    BlockBuilder blockBuilder = SHORT_TYPE.createBlockBuilder(null, length);
    int offset = 0;
    for (int i = 0; i < length; ++i) {
        long value = TimestampPlusIntervalYearToMonth.add(start, offset);
        SHORT_TYPE.writeLong(blockBuilder, value);
        offset += step;
    }
    return blockBuilder.build();
}
Also used : BlockBuilder(io.trino.spi.block.BlockBuilder) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 30 with BlockBuilder

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

the class RowNumberOperator method createRowNumberBlock.

private Block createRowNumberBlock() {
    BlockBuilder rowNumberBlock = BIGINT.createFixedSizeBlockBuilder(inputPage.getPositionCount());
    for (int currentPosition = 0; currentPosition < inputPage.getPositionCount(); currentPosition++) {
        long partitionId = getPartitionId(currentPosition);
        long nextRowCount = partitionRowCount.get(partitionId) + 1;
        BIGINT.writeLong(rowNumberBlock, nextRowCount);
        partitionRowCount.set(partitionId, nextRowCount);
    }
    return rowNumberBlock.build();
}
Also used : BlockBuilder(io.trino.spi.block.BlockBuilder)

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