Search in sources :

Example 86 with ScalarFunction

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);
}
Also used : BetaDistribution(org.apache.commons.math3.distribution.BetaDistribution) ScalarFunction(io.trino.spi.function.ScalarFunction) DecimalOperators.modulusScalarFunction(io.trino.type.DecimalOperators.modulusScalarFunction) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 87 with ScalarFunction

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();
}
Also used : BlockBuilder(io.trino.spi.block.BlockBuilder) ScalarFunction(io.trino.spi.function.ScalarFunction) SqlType(io.trino.spi.function.SqlType)

Example 88 with ScalarFunction

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();
}
Also used : BlockBuilder(io.trino.spi.block.BlockBuilder) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 89 with ScalarFunction

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;
}
Also used : Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Constraint(io.trino.type.Constraint) SliceUtf8.lengthOfCodePoint(io.airlift.slice.SliceUtf8.lengthOfCodePoint) SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) LiteralParameters(io.trino.spi.function.LiteralParameters) Constraint(io.trino.type.Constraint) SqlType(io.trino.spi.function.SqlType)

Example 90 with ScalarFunction

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;
}
Also used : TrinoException(io.trino.spi.TrinoException) Constraint(io.trino.type.Constraint) SliceUtf8.lengthOfCodePoint(io.airlift.slice.SliceUtf8.lengthOfCodePoint) SliceUtf8.offsetOfCodePoint(io.airlift.slice.SliceUtf8.offsetOfCodePoint) SqlNullable(io.trino.spi.function.SqlNullable) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Aggregations

ScalarFunction (io.trino.spi.function.ScalarFunction)109 SqlType (io.trino.spi.function.SqlType)108 Description (io.trino.spi.function.Description)91 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)46 SqlNullable (io.trino.spi.function.SqlNullable)44 LiteralParameters (io.trino.spi.function.LiteralParameters)25 BlockBuilder (io.trino.spi.block.BlockBuilder)22 Constraint (io.trino.type.Constraint)21 Slice (io.airlift.slice.Slice)18 Point (com.esri.core.geometry.Point)17 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)17 TrinoException (io.trino.spi.TrinoException)17 MultiPoint (com.esri.core.geometry.MultiPoint)15 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)10 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)9 Matcher (io.airlift.joni.Matcher)8 GeometryType (io.trino.geospatial.GeometryType)7 JsonParser (com.fasterxml.jackson.core.JsonParser)6 JsonToken (com.fasterxml.jackson.core.JsonToken)6 Envelope (com.esri.core.geometry.Envelope)5