Search in sources :

Example 6 with SliceUtf8.offsetOfCodePoint

use of io.airlift.slice.SliceUtf8.offsetOfCodePoint in project presto by prestodb.

the class RcFileDecoderUtils method calculateTruncationLength.

private static int calculateTruncationLength(int maxCharacterCount, Slice slice, int offset, int length) {
    requireNonNull(slice, "slice is null");
    if (maxCharacterCount < 0) {
        throw new IllegalArgumentException("Max length must be greater or equal than zero");
    }
    if (length <= maxCharacterCount) {
        return length;
    }
    int indexEnd = offsetOfCodePoint(slice, offset, maxCharacterCount);
    if (indexEnd < 0) {
        return length;
    }
    return indexEnd - offset;
}
Also used : SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint)

Example 7 with SliceUtf8.offsetOfCodePoint

use of io.airlift.slice.SliceUtf8.offsetOfCodePoint in project presto by prestodb.

the class Varchars method truncateToLength.

public static Slice truncateToLength(Slice slice, int maxLength) {
    requireNonNull(slice, "slice is null");
    if (maxLength < 0) {
        throw new IllegalArgumentException("Max length must be greater or equal than zero");
    }
    if (maxLength == 0) {
        return Slices.EMPTY_SLICE;
    }
    // min codepoint size is 1 byte.
    // if size in bytes is less than the max length
    // we don't need to decode codepoints
    int sizeInBytes = slice.length();
    if (sizeInBytes <= maxLength) {
        return slice;
    }
    int indexEnd = offsetOfCodePoint(slice, maxLength);
    if (indexEnd < 0) {
        return slice;
    }
    return slice.slice(0, indexEnd);
}
Also used : SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint)

Aggregations

SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)7 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)4 SqlType (com.facebook.presto.spi.function.SqlType)4 Constraint (com.facebook.presto.type.Constraint)4 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)4 Description (com.facebook.presto.spi.function.Description)3 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)3 PrestoException (com.facebook.presto.spi.PrestoException)2 Slice (io.airlift.slice.Slice)2 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)1 SqlNullable (com.facebook.presto.spi.function.SqlNullable)1 InvalidUtf8Exception (io.airlift.slice.InvalidUtf8Exception)1 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)1 OptionalInt (java.util.OptionalInt)1