Search in sources :

Example 16 with Slice

use of io.airlift.slice.Slice in project presto by prestodb.

the class PagesSerdeUtil method readSerializedPage.

private static SerializedPage readSerializedPage(SliceInput sliceInput) {
    int positionCount = sliceInput.readInt();
    byte codecMarker = sliceInput.readByte();
    int uncompressedSizeInBytes = sliceInput.readInt();
    int sizeInBytes = sliceInput.readInt();
    Slice slice = sliceInput.readSlice(toIntExact((sizeInBytes)));
    return new SerializedPage(slice, lookupCodecFromMarker(codecMarker), positionCount, uncompressedSizeInBytes);
}
Also used : Slice(io.airlift.slice.Slice)

Example 17 with Slice

use of io.airlift.slice.Slice in project presto by prestodb.

the class DecimalSumAggregation method accumulateValueInState.

private static void accumulateValueInState(Slice unscaledDecimal, LongDecimalWithOverflowState state) {
    initializeIfNeeded(state);
    Slice sum = state.getLongDecimal();
    long overflow = UnscaledDecimal128Arithmetic.addWithOverflow(sum, unscaledDecimal, sum);
    state.setOverflow(state.getOverflow() + overflow);
}
Also used : Slice(io.airlift.slice.Slice)

Example 18 with Slice

use of io.airlift.slice.Slice in project presto by prestodb.

the class CharacterStringCasts method varcharToCharSaturatedFloorCast.

@ScalarOperator(OperatorType.SATURATED_FLOOR_CAST)
@SqlType("char(y)")
@LiteralParameters({ "x", "y" })
public static // Char(y) value that is smaller than the original Varchar(x) value. This is fine though for usage in TupleDomainTranslator.
Slice varcharToCharSaturatedFloorCast(@LiteralParameter("y") Long y, @SqlType("varchar(x)") Slice slice) {
    Slice trimmedSlice = trimSpaces(slice);
    int trimmedTextLength = countCodePoints(trimmedSlice);
    int numberOfTrailingSpaces = slice.length() - trimmedSlice.length();
    // if Varchar(x) value length (including spaces) is greater than y, we can just truncate it
    if (trimmedTextLength + numberOfTrailingSpaces >= y) {
        return truncateToLength(trimmedSlice, y.intValue());
    }
    if (trimmedTextLength == 0) {
        return EMPTY_SLICE;
    }
    // and also remove one additional trailing character to get smaller Char(y) value
    return trimmedSlice.slice(0, offsetOfCodePoint(trimmedSlice, trimmedTextLength - 1));
}
Also used : Slice(io.airlift.slice.Slice) SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 19 with Slice

use of io.airlift.slice.Slice in project presto by prestodb.

the class StringFunctions method splitToMap.

@Description("creates a map using entryDelimiter and keyValueDelimiter")
@ScalarFunction
@SqlType("map<varchar,varchar>")
public static Block splitToMap(@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 PrestoException(INVALID_FUNCTION_ARGUMENT, "Key-value delimiter must appear exactly once in each entry. Bad input: '" + keyValuePair.toStringUtf8() + "'");
            }
            if (map.containsKey(key)) {
                throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Duplicate keys (%s) are not allowed", key.toStringUtf8()));
            }
            map.put(key, value);
        } else {
            throw new PrestoException(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();
    }
    BlockBuilder builder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), map.size());
    for (Map.Entry<Slice, Slice> entry : map.entrySet()) {
        VARCHAR.writeSlice(builder, entry.getKey());
        VARCHAR.writeSlice(builder, entry.getValue());
    }
    return builder.build();
}
Also used : HashMap(java.util.HashMap) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) PrestoException(com.facebook.presto.spi.PrestoException) HashMap(java.util.HashMap) Map(java.util.Map) 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) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 20 with Slice

use of io.airlift.slice.Slice in project presto by prestodb.

the class ShardStats method indexString.

private static ColumnStats indexString(Type type, OrcRecordReader reader, int columnIndex, long columnId) throws IOException {
    boolean minSet = false;
    boolean maxSet = false;
    Slice min = null;
    Slice max = null;
    while (true) {
        int batchSize = reader.nextBatch();
        if (batchSize <= 0) {
            break;
        }
        Block block = reader.readBlock(type, columnIndex);
        for (int i = 0; i < batchSize; i++) {
            if (block.isNull(i)) {
                continue;
            }
            Slice slice = type.getSlice(block, i);
            slice = truncateIndexValue(slice);
            if (!minSet || (slice.compareTo(min) < 0)) {
                minSet = true;
                min = slice;
            }
            if (!maxSet || (slice.compareTo(max) > 0)) {
                maxSet = true;
                max = slice;
            }
        }
    }
    return new ColumnStats(columnId, minSet ? min.toStringUtf8() : null, maxSet ? max.toStringUtf8() : null);
}
Also used : Slice(io.airlift.slice.Slice) ColumnStats(com.facebook.presto.raptor.metadata.ColumnStats) Block(com.facebook.presto.spi.block.Block)

Aggregations

Slice (io.airlift.slice.Slice)564 Test (org.testng.annotations.Test)139 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)108 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)86 List (java.util.List)59 ImmutableList (com.google.common.collect.ImmutableList)57 Block (com.facebook.presto.common.block.Block)53 Type (com.facebook.presto.common.type.Type)53 ArrayList (java.util.ArrayList)52 PrestoException (com.facebook.presto.spi.PrestoException)37 Optional (java.util.Optional)35 Map (java.util.Map)34 Page (com.facebook.presto.common.Page)30 ImmutableMap (com.google.common.collect.ImmutableMap)30 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)29 IOException (java.io.IOException)28 Collectors.toList (java.util.stream.Collectors.toList)27 DynamicSliceOutput (io.airlift.slice.DynamicSliceOutput)26 SqlType (com.facebook.presto.spi.function.SqlType)25 BigDecimal (java.math.BigDecimal)23