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);
}
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);
}
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));
}
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();
}
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);
}
Aggregations