use of io.trino.spi.function.ScalarFunction in project trino by trinodb.
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(null, 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();
}
use of io.trino.spi.function.ScalarFunction in project trino by trinodb.
the class TDigestFunctions method valuesAtQuantiles.
@ScalarFunction("values_at_quantiles")
@Description("For each input q between [0, 1], find the value whose rank in the sorted sequence of the n values represented by the tdigest is qn.")
@SqlType("array(double)")
public static Block valuesAtQuantiles(@SqlType(StandardTypes.TDIGEST) TDigest input, @SqlType("array(double)") Block percentilesArrayBlock) {
List<Double> percentiles = IntStream.range(0, percentilesArrayBlock.getPositionCount()).mapToDouble(i -> DOUBLE.getDouble(percentilesArrayBlock, i)).boxed().collect(toImmutableList());
checkCondition(Ordering.natural().isOrdered(percentiles), INVALID_FUNCTION_ARGUMENT, "percentiles must be sorted in increasing order");
BlockBuilder output = DOUBLE.createBlockBuilder(null, percentilesArrayBlock.getPositionCount());
List<Double> valuesAtPercentiles = input.valuesAt(percentiles);
for (Double value : valuesAtPercentiles) {
DOUBLE.writeDouble(output, value);
}
return output.build();
}
use of io.trino.spi.function.ScalarFunction in project trino by trinodb.
the class Re2JRegexpFunctions method regexpCount.
@ScalarFunction
@Description("Returns the number of times that a pattern occurs in a string")
@LiteralParameters("x")
@SqlType(StandardTypes.BIGINT)
public static long regexpCount(@SqlType("varchar(x)") Slice source, @SqlType(Re2JRegexpType.NAME) Re2JRegexp pattern) {
Matcher matcher = pattern.matcher(source);
int count = 0;
while (matcher.find()) {
count++;
}
return count;
}
use of io.trino.spi.function.ScalarFunction in project trino by trinodb.
the class Re2JRegexpFunctions method regexpPosition.
@ScalarFunction
@Description("Returns the index of the n-th matched substring starting from the specified position")
@LiteralParameters("x")
@SqlType(StandardTypes.INTEGER)
public static long regexpPosition(@SqlType("varchar(x)") Slice source, @SqlType(Re2JRegexpType.NAME) Re2JRegexp pattern, @SqlType(StandardTypes.INTEGER) long start, @SqlType(StandardTypes.INTEGER) long occurrence) {
// start position cannot be smaller than 1
if (start < 1) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "start position cannot be smaller than 1");
}
// occurrence cannot be smaller than 1
if (occurrence < 1) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "occurrence cannot be smaller than 1");
}
// returns -1 if start is greater than the length of source
if (start > SliceUtf8.countCodePoints(source)) {
return -1;
}
int startBytePosition = SliceUtf8.offsetOfCodePoint(source, (int) start - 1);
int length = source.length() - startBytePosition;
Matcher matcher = pattern.matcher(source.slice(startBytePosition, length));
long count = 0;
while (matcher.find()) {
if (++count == occurrence) {
// Plus 1 because position returned start from 1
return SliceUtf8.countCodePoints(source, 0, startBytePosition + matcher.start()) + 1;
}
}
return -1;
}
use of io.trino.spi.function.ScalarFunction in project trino by trinodb.
the class VarbinaryFunctions method toBigEndian64.
@Description("Encode value as a 64-bit 2's complement big endian varbinary")
@ScalarFunction("to_big_endian_64")
@SqlType(StandardTypes.VARBINARY)
public static Slice toBigEndian64(@SqlType(StandardTypes.BIGINT) long value) {
Slice slice = Slices.allocate(Long.BYTES);
slice.setLong(0, Long.reverseBytes(value));
return slice;
}
Aggregations