Search in sources :

Example 71 with SqlType

use of io.trino.spi.function.SqlType in project trino by trinodb.

the class JoniRegexpFunctions method regexpReplace.

@Description("Replaces substrings matching a regular expression by given string")
@ScalarFunction
@LiteralParameters({ "x", "y", "z" })
// to get the formula: x + max(x * y / 2, y) * (x + 1)
@Constraint(variable = "z", expression = "min(2147483647, x + max(x * y / 2, y) * (x + 1))")
@SqlType("varchar(z)")
public static Slice regexpReplace(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern, @SqlType("varchar(y)") Slice replacement) {
    Matcher matcher = pattern.matcher(source.getBytes());
    SliceOutput sliceOutput = new DynamicSliceOutput(source.length() + replacement.length() * 5);
    int lastEnd = 0;
    // nextStart is the same as lastEnd, unless the last match was zero-width. In such case, nextStart is lastEnd + 1.
    int nextStart = 0;
    while (true) {
        int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
        if (offset == -1) {
            break;
        }
        nextStart = getNextStart(source, matcher);
        Slice sliceBetweenReplacements = source.slice(lastEnd, matcher.getBegin() - lastEnd);
        lastEnd = matcher.getEnd();
        sliceOutput.appendBytes(sliceBetweenReplacements);
        appendReplacement(sliceOutput, source, pattern.regex(), matcher.getEagerRegion(), replacement);
    }
    sliceOutput.appendBytes(source.slice(lastEnd, source.length() - lastEnd));
    return sliceOutput.slice();
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Matcher(io.airlift.joni.Matcher) Slice(io.airlift.slice.Slice) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Constraint(io.trino.type.Constraint) 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 72 with SqlType

use of io.trino.spi.function.SqlType in project trino by trinodb.

the class JoniRegexpFunctions method regexpExtract.

@SqlNullable
@Description("Returns regex group of extracted string with a pattern")
@ScalarFunction
@LiteralParameters("x")
@SqlType("varchar(x)")
public static Slice regexpExtract(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern, @SqlType(StandardTypes.BIGINT) long groupIndex) {
    Matcher matcher = pattern.matcher(source.getBytes());
    validateGroup(groupIndex, matcher.getEagerRegion());
    int group = toIntExact(groupIndex);
    int offset = matcher.search(0, source.length(), Option.DEFAULT);
    if (offset == -1) {
        return null;
    }
    Region region = matcher.getEagerRegion();
    int beg = region.beg[group];
    int end = region.end[group];
    if (beg == -1) {
        // end == -1 must be true
        return null;
    }
    return source.slice(beg, end - beg);
}
Also used : Matcher(io.airlift.joni.Matcher) Region(io.airlift.joni.Region) Constraint(io.trino.type.Constraint) 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)

Example 73 with SqlType

use of io.trino.spi.function.SqlType in project trino by trinodb.

the class JoniRegexpFunctions method regexpExtractAll.

@Description("Group(s) extracted using the given pattern")
@ScalarFunction
@LiteralParameters("x")
@SqlType("array(varchar(x))")
public static Block regexpExtractAll(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern, @SqlType(StandardTypes.BIGINT) long groupIndex) {
    Matcher matcher = pattern.matcher(source.getBytes());
    validateGroup(groupIndex, matcher.getEagerRegion());
    BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, 32);
    int group = toIntExact(groupIndex);
    int nextStart = 0;
    while (true) {
        int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
        if (offset == -1) {
            break;
        }
        nextStart = getNextStart(source, matcher);
        Region region = matcher.getEagerRegion();
        int beg = region.beg[group];
        int end = region.end[group];
        if (beg == -1 || end == -1) {
            blockBuilder.appendNull();
        } else {
            Slice slice = source.slice(beg, end - beg);
            VARCHAR.writeSlice(blockBuilder, slice);
        }
    }
    return blockBuilder.build();
}
Also used : Matcher(io.airlift.joni.Matcher) Slice(io.airlift.slice.Slice) Region(io.airlift.joni.Region) Constraint(io.trino.type.Constraint) BlockBuilder(io.trino.spi.block.BlockBuilder) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 74 with SqlType

use of io.trino.spi.function.SqlType in project trino by trinodb.

the class JsonFunctions method jsonArrayContains.

@SqlNullable
@ScalarFunction
@SqlType(StandardTypes.BOOLEAN)
public static Boolean jsonArrayContains(@SqlType(StandardTypes.JSON) Slice json, @SqlType(StandardTypes.BIGINT) long value) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        if (parser.nextToken() != START_ARRAY) {
            return null;
        }
        while (true) {
            JsonToken token = parser.nextToken();
            if (token == null) {
                return null;
            }
            if (token == END_ARRAY) {
                return false;
            }
            parser.skipChildren();
            if ((token == VALUE_NUMBER_INT) && ((parser.getNumberType() == NumberType.INT) || (parser.getNumberType() == NumberType.LONG)) && (parser.getLongValue() == value)) {
                return true;
            }
        }
    } catch (IOException e) {
        return null;
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonUtil.createJsonParser(io.trino.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) SqlNullable(io.trino.spi.function.SqlNullable) ScalarFunction(io.trino.spi.function.ScalarFunction) SqlType(io.trino.spi.function.SqlType)

Example 75 with SqlType

use of io.trino.spi.function.SqlType in project trino by trinodb.

the class JsonFunctions method jsonArrayContains.

@SqlNullable
@ScalarFunction
@SqlType(StandardTypes.BOOLEAN)
public static Boolean jsonArrayContains(@SqlType(StandardTypes.JSON) Slice json, @SqlType(StandardTypes.BOOLEAN) boolean value) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        if (parser.nextToken() != START_ARRAY) {
            return null;
        }
        while (true) {
            JsonToken token = parser.nextToken();
            if (token == null) {
                return null;
            }
            if (token == END_ARRAY) {
                return false;
            }
            parser.skipChildren();
            if (((token == VALUE_TRUE) && value) || ((token == VALUE_FALSE) && (!value))) {
                return true;
            }
        }
    } catch (IOException e) {
        return null;
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonUtil.createJsonParser(io.trino.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) SqlNullable(io.trino.spi.function.SqlNullable) ScalarFunction(io.trino.spi.function.ScalarFunction) SqlType(io.trino.spi.function.SqlType)

Aggregations

SqlType (io.trino.spi.function.SqlType)176 ScalarFunction (io.trino.spi.function.ScalarFunction)107 Description (io.trino.spi.function.Description)90 LiteralParameters (io.trino.spi.function.LiteralParameters)60 SqlNullable (io.trino.spi.function.SqlNullable)56 BlockBuilder (io.trino.spi.block.BlockBuilder)52 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)46 TrinoException (io.trino.spi.TrinoException)37 Constraint (io.trino.type.Constraint)23 Slice (io.airlift.slice.Slice)22 TypeParameter (io.trino.spi.function.TypeParameter)21 Point (com.esri.core.geometry.Point)17 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)17 MultiPoint (com.esri.core.geometry.MultiPoint)15 JsonParser (com.fasterxml.jackson.core.JsonParser)14 JsonUtil.createJsonParser (io.trino.util.JsonUtil.createJsonParser)14 IOException (java.io.IOException)14 ScalarOperator (io.trino.spi.function.ScalarOperator)13 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)11 Matcher (io.airlift.joni.Matcher)9