Search in sources :

Example 86 with BlockBuilder

use of com.facebook.presto.spi.block.BlockBuilder in project presto by prestodb.

the class SequenceFunction method fixedWidthSequence.

private static Block fixedWidthSequence(long start, long stop, long step, FixedWidthType type) {
    checkCondition(step != 0, INVALID_FUNCTION_ARGUMENT, "step must not be zero");
    checkCondition(step > 0 ? stop >= start : stop < start, INVALID_FUNCTION_ARGUMENT, "sequence stop value should be greater than or equal to start value if step is greater than zero otherwise stop should be less than start");
    int length = toIntExact((stop - start) / step + 1L);
    BlockBuilder blockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), length);
    for (long i = 0, value = start; i < length; ++i, value += step) {
        type.writeLong(blockBuilder, value);
    }
    return blockBuilder.build();
}
Also used : BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 87 with BlockBuilder

use of com.facebook.presto.spi.block.BlockBuilder in project presto by prestodb.

the class StringFunctions method split.

@ScalarFunction
@LiteralParameters({ "x", "y" })
@SqlType("array(varchar(x))")
public static Block split(@SqlType("varchar(x)") Slice string, @SqlType("varchar(y)") Slice delimiter, @SqlType(StandardTypes.BIGINT) long limit) {
    checkCondition(limit > 0, INVALID_FUNCTION_ARGUMENT, "Limit must be positive");
    checkCondition(limit <= Integer.MAX_VALUE, INVALID_FUNCTION_ARGUMENT, "Limit is too large");
    checkCondition(delimiter.length() > 0, INVALID_FUNCTION_ARGUMENT, "The delimiter may not be the empty string");
    BlockBuilder parts = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), 1, string.length());
    // If limit is one, the last and only element is the complete string
    if (limit == 1) {
        VARCHAR.writeSlice(parts, string);
        return parts.build();
    }
    int index = 0;
    while (index < string.length()) {
        int splitIndex = string.indexOf(delimiter, index);
        // Found split?
        if (splitIndex < 0) {
            break;
        }
        // Add the part from current index to found split
        VARCHAR.writeSlice(parts, string, index, splitIndex - index);
        // Continue searching after delimiter
        index = splitIndex + delimiter.length();
        // Reached limit-1 parts so we can stop
        if (parts.getPositionCount() == limit - 1) {
            break;
        }
    }
    // Rest of string
    VARCHAR.writeSlice(parts, string, index, string.length() - index);
    return parts.build();
}
Also used : Constraint(com.facebook.presto.type.Constraint) SliceUtf8.lengthOfCodePoint(io.airlift.slice.SliceUtf8.lengthOfCodePoint) SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 88 with BlockBuilder

use of com.facebook.presto.spi.block.BlockBuilder in project presto by prestodb.

the class MapTransformKeyFunction method transform.

public static Block transform(Type keyType, Type transformedKeyType, Type valueType, ConnectorSession session, Block block, MethodHandle function) {
    int positionCount = block.getPositionCount();
    BlockBuilder resultBuilder = new InterleavedBlockBuilder(ImmutableList.of(transformedKeyType, valueType), new BlockBuilderStatus(), positionCount);
    TypedSet typedSet = new TypedSet(transformedKeyType, positionCount / 2);
    for (int position = 0; position < positionCount; position += 2) {
        Object key = readNativeValue(keyType, block, position);
        Object value = readNativeValue(valueType, block, position + 1);
        Object transformedKey;
        try {
            transformedKey = function.invoke(key, value);
        } catch (Throwable throwable) {
            throw Throwables.propagate(throwable);
        }
        if (transformedKey == null) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
        }
        writeNativeValue(transformedKeyType, resultBuilder, transformedKey);
        valueType.appendTo(block, position + 1, resultBuilder);
        if (typedSet.contains(resultBuilder, position)) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Duplicate keys (%s) are not allowed", transformedKeyType.getObjectValue(session, resultBuilder, position)));
        }
        typedSet.add(resultBuilder, position);
    }
    return resultBuilder.build();
}
Also used : TypedSet(com.facebook.presto.operator.aggregation.TypedSet) PrestoException(com.facebook.presto.spi.PrestoException) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 89 with BlockBuilder

use of com.facebook.presto.spi.block.BlockBuilder in project presto by prestodb.

the class MapTransformValueFunction method transform.

public static Block transform(Type keyType, Type valueType, Type transformedValueType, Block block, MethodHandle function) {
    int positionCount = block.getPositionCount();
    BlockBuilder resultBuilder = new InterleavedBlockBuilder(ImmutableList.of(keyType, transformedValueType), new BlockBuilderStatus(), positionCount);
    for (int position = 0; position < positionCount; position += 2) {
        Object key = readNativeValue(keyType, block, position);
        Object value = readNativeValue(valueType, block, position + 1);
        Object transformedValue;
        try {
            transformedValue = function.invoke(key, value);
        } catch (Throwable throwable) {
            throw Throwables.propagate(throwable);
        }
        keyType.appendTo(block, position, resultBuilder);
        writeNativeValue(transformedValueType, resultBuilder, transformedValue);
    }
    return resultBuilder.build();
}
Also used : InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 90 with BlockBuilder

use of com.facebook.presto.spi.block.BlockBuilder in project presto by prestodb.

the class RowEqualOperator method equals.

public static boolean equals(Type rowType, Block leftRow, Block rightRow) {
    // TODO: Fix this. It feels very inefficient and unnecessary to wrap and unwrap with Block
    BlockBuilder leftBlockBuilder = rowType.createBlockBuilder(new BlockBuilderStatus(), 1);
    BlockBuilder rightBlockBuilder = rowType.createBlockBuilder(new BlockBuilderStatus(), 1);
    rowType.writeObject(leftBlockBuilder, leftRow);
    rowType.writeObject(rightBlockBuilder, rightRow);
    return rowType.equalTo(leftBlockBuilder.build(), 0, rightBlockBuilder.build(), 0);
}
Also used : BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Aggregations

BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)290 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)211 Block (com.facebook.presto.spi.block.Block)56 Slice (io.airlift.slice.Slice)53 InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)39 Test (org.testng.annotations.Test)38 Type (com.facebook.presto.spi.type.Type)33 SqlType (com.facebook.presto.spi.function.SqlType)24 ArrayType (com.facebook.presto.type.ArrayType)19 Page (com.facebook.presto.spi.Page)18 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)17 TypeParameter (com.facebook.presto.spi.function.TypeParameter)15 MapType (com.facebook.presto.type.MapType)14 RowType (com.facebook.presto.type.RowType)14 TypeJsonUtils.appendToBlockBuilder (com.facebook.presto.type.TypeJsonUtils.appendToBlockBuilder)13 PageBuilder (com.facebook.presto.spi.PageBuilder)12 ImmutableList (com.google.common.collect.ImmutableList)12 PrestoException (com.facebook.presto.spi.PrestoException)11 RunLengthEncodedBlock (com.facebook.presto.spi.block.RunLengthEncodedBlock)11 DictionaryBlock (com.facebook.presto.spi.block.DictionaryBlock)10