Search in sources :

Example 51 with BlockBuilderStatus

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

the class ZipFunction method zip.

@UsedByGeneratedCode
public static Block zip(List<Type> types, Block... arrays) {
    int biggestCardinality = 0;
    for (Block array : arrays) {
        biggestCardinality = Math.max(biggestCardinality, array.getPositionCount());
    }
    RowType rowType = new RowType(types, Optional.empty());
    BlockBuilder outputBuilder = rowType.createBlockBuilder(new BlockBuilderStatus(), biggestCardinality);
    for (int outputPosition = 0; outputPosition < biggestCardinality; outputPosition++) {
        BlockBuilder rowBuilder = outputBuilder.beginBlockEntry();
        for (int fieldIndex = 0; fieldIndex < arrays.length; fieldIndex++) {
            if (arrays[fieldIndex].getPositionCount() <= outputPosition) {
                rowBuilder.appendNull();
            } else {
                types.get(fieldIndex).appendTo(arrays[fieldIndex], outputPosition, rowBuilder);
            }
        }
        outputBuilder.closeEntry();
    }
    return outputBuilder.build();
}
Also used : Block(com.facebook.presto.spi.block.Block) RowType(com.facebook.presto.type.RowType) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 52 with BlockBuilderStatus

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

the class RowHashCodeOperator method hash.

@UsedByGeneratedCode
public static long hash(Type rowType, Block block) {
    BlockBuilder blockBuilder = rowType.createBlockBuilder(new BlockBuilderStatus(), 1);
    blockBuilder.writeObject(block).closeEntry();
    return rowType.hash(blockBuilder.build(), 0);
}
Also used : BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 53 with BlockBuilderStatus

use of com.facebook.presto.spi.block.BlockBuilderStatus 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 54 with BlockBuilderStatus

use of com.facebook.presto.spi.block.BlockBuilderStatus 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 55 with BlockBuilderStatus

use of com.facebook.presto.spi.block.BlockBuilderStatus 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)

Aggregations

BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)227 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)210 Block (com.facebook.presto.spi.block.Block)55 Slice (io.airlift.slice.Slice)43 InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)37 Test (org.testng.annotations.Test)35 Type (com.facebook.presto.spi.type.Type)24 SqlType (com.facebook.presto.spi.function.SqlType)20 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)15 ArrayType (com.facebook.presto.type.ArrayType)14 Page (com.facebook.presto.spi.Page)12 TypeJsonUtils.appendToBlockBuilder (com.facebook.presto.type.TypeJsonUtils.appendToBlockBuilder)12 TypeParameter (com.facebook.presto.spi.function.TypeParameter)11 RowType (com.facebook.presto.type.RowType)11 OrcCorruptionException (com.facebook.presto.orc.OrcCorruptionException)10 RunLengthEncodedBlock (com.facebook.presto.spi.block.RunLengthEncodedBlock)10 MapType (com.facebook.presto.type.MapType)10 PrestoException (com.facebook.presto.spi.PrestoException)9 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)8 DecimalType (com.facebook.presto.spi.type.DecimalType)8