use of io.trino.spi.function.ScalarFunction in project trino by trinodb.
the class MathFunctions method betaCdf.
@Description("Beta cdf given the a, b parameters and value")
@ScalarFunction
@SqlType(StandardTypes.DOUBLE)
public static double betaCdf(@SqlType(StandardTypes.DOUBLE) double a, @SqlType(StandardTypes.DOUBLE) double b, @SqlType(StandardTypes.DOUBLE) double value) {
checkCondition(value >= 0 && value <= 1, INVALID_FUNCTION_ARGUMENT, "value must be 0 >= v >= 1");
checkCondition(a > 0 && b > 0, INVALID_FUNCTION_ARGUMENT, "a, b must be > 0");
BetaDistribution distribution = new BetaDistribution(null, a, b, BetaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
return distribution.cumulativeProbability(value);
}
use of io.trino.spi.function.ScalarFunction in project trino by trinodb.
the class SequenceFunction method sequenceDateYearToMonth.
@ScalarFunction("sequence")
@SqlType("array(date)")
public static Block sequenceDateYearToMonth(@SqlType(StandardTypes.DATE) long start, @SqlType(StandardTypes.DATE) long stop, @SqlType(StandardTypes.INTERVAL_YEAR_TO_MONTH) long step) {
checkValidStep(start, stop, step);
int length = toIntExact(diffDate(MONTH, start, stop) / step + 1);
checkMaxEntry(length);
BlockBuilder blockBuilder = DATE.createBlockBuilder(null, length);
int value = 0;
for (int i = 0; i < length; ++i) {
DATE.writeLong(blockBuilder, DateTimeOperators.datePlusIntervalYearToMonth(start, value));
value += step;
}
return blockBuilder.build();
}
use of io.trino.spi.function.ScalarFunction in project trino by trinodb.
the class SessionFunctions method currentGroups.
@ScalarFunction("current_groups")
@Description("Current groups of current user")
@SqlType("array(varchar)")
public static Block currentGroups(ConnectorSession session) {
Set<String> groups = session.getIdentity().getGroups();
BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, groups.size());
for (String group : groups) {
VARCHAR.writeSlice(blockBuilder, utf8Slice(group));
}
return blockBuilder.build();
}
use of io.trino.spi.function.ScalarFunction in project trino by trinodb.
the class StringFunctions method concat.
// TODO: implement N arguments char concat
@Description("Concatenates given character strings")
@ScalarFunction
@LiteralParameters({ "x", "y", "u" })
@Constraint(variable = "u", expression = "x + y")
@SqlType("char(u)")
public static Slice concat(@LiteralParameter("x") Long x, @SqlType("char(x)") Slice left, @SqlType("char(y)") Slice right) {
int rightLength = right.length();
if (rightLength == 0) {
return left;
}
Slice paddedLeft = padSpaces(left, x.intValue());
int leftLength = paddedLeft.length();
Slice result = Slices.allocate(leftLength + rightLength);
result.setBytes(0, paddedLeft);
result.setBytes(leftLength, right);
return result;
}
use of io.trino.spi.function.ScalarFunction in project trino by trinodb.
the class StringFunctions method splitPart.
@SqlNullable
@Description("Splits a string by a delimiter and returns the specified field (counting from one)")
@ScalarFunction
@LiteralParameters({ "x", "y" })
@SqlType("varchar(x)")
public static Slice splitPart(@SqlType("varchar(x)") Slice string, @SqlType("varchar(y)") Slice delimiter, @SqlType(StandardTypes.BIGINT) long index) {
checkCondition(index > 0, INVALID_FUNCTION_ARGUMENT, "Index must be greater than zero");
// Empty delimiter? Then every character will be a split
if (delimiter.length() == 0) {
int startCodePoint = toIntExact(index);
int indexStart = offsetOfCodePoint(string, startCodePoint - 1);
if (indexStart < 0) {
// index too big
return null;
}
int length = lengthOfCodePoint(string, indexStart);
if (indexStart + length > string.length()) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Invalid UTF-8 encoding");
}
return string.slice(indexStart, length);
}
int matchCount = 0;
int previousIndex = 0;
while (previousIndex < string.length()) {
int matchIndex = string.indexOf(delimiter, previousIndex);
// No match
if (matchIndex < 0) {
break;
}
// Reached the requested part?
if (++matchCount == index) {
return string.slice(previousIndex, matchIndex - previousIndex);
}
// Continue searching after the delimiter
previousIndex = matchIndex + delimiter.length();
}
if (matchCount == index - 1) {
// returns last section of the split
return string.slice(previousIndex, string.length() - previousIndex);
}
// index is too big, null is returned
return null;
}
Aggregations