Search in sources :

Example 1 with SqlNullable

use of io.prestosql.spi.function.SqlNullable in project hetu-core by openlookeng.

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) Regex 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;
    }
    Slice slice = source.slice(beg, end - beg);
    return slice;
}
Also used : Matcher(io.airlift.joni.Matcher) Slice(io.airlift.slice.Slice) Region(io.airlift.joni.Region) Constraint(io.prestosql.type.Constraint) SqlNullable(io.prestosql.spi.function.SqlNullable) ScalarFunction(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) LiteralParameters(io.prestosql.spi.function.LiteralParameters) SqlType(io.prestosql.spi.function.SqlType)

Example 2 with SqlNullable

use of io.prestosql.spi.function.SqlNullable in project hetu-core by openlookeng.

the class JoniRegexpReplaceLambdaFunction method regexpReplace.

@LiteralParameters("x")
@SqlType("varchar")
@SqlNullable
public Slice regexpReplace(@SqlType("varchar") Slice source, @SqlType(JoniRegexpType.NAME) Regex pattern, @SqlType("function(array(varchar), varchar(x))") UnaryFunctionInterface replaceFunction) {
    // If there is no match we can simply return the original source without doing copy.
    Matcher matcher = pattern.matcher(source.getBytes());
    if (matcher.search(0, source.length(), Option.DEFAULT) == -1) {
        return source;
    }
    SliceOutput output = new DynamicSliceOutput(source.length());
    // that will be passed to the lambda function.
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }
    BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
    int groupCount = pattern.numberOfCaptures();
    int appendPosition = 0;
    int nextStart;
    do {
        // In such case, nextStart is last appendPosition + 1.
        if (matcher.getEnd() == matcher.getBegin()) {
            nextStart = matcher.getEnd() + 1;
        } else {
            nextStart = matcher.getEnd();
        }
        // Append the un-matched part
        Slice unmatched = source.slice(appendPosition, matcher.getBegin() - appendPosition);
        appendPosition = matcher.getEnd();
        output.appendBytes(unmatched);
        // Append the capturing groups to the target block that will be passed to lambda
        Region matchedRegion = matcher.getEagerRegion();
        for (int i = 1; i <= groupCount; i++) {
            // Add to the block builder if the matched region is not null. In Joni null is represented as [-1, -1]
            if (matchedRegion.beg[i] >= 0 && matchedRegion.end[i] >= 0) {
                VARCHAR.writeSlice(blockBuilder, source, matchedRegion.beg[i], matchedRegion.end[i] - matchedRegion.beg[i]);
            } else {
                blockBuilder.appendNull();
            }
        }
        pageBuilder.declarePositions(groupCount);
        Block target = blockBuilder.getRegion(blockBuilder.getPositionCount() - groupCount, groupCount);
        // Call the lambda function to replace the block, and append the result to output
        Slice replaced = (Slice) replaceFunction.apply(target);
        if (replaced == null) {
            // replacing a substring with null (unknown) makes the entire string null
            return null;
        }
        output.appendBytes(replaced);
    } while (matcher.search(nextStart, source.length(), Option.DEFAULT) != -1);
    // Append the last un-matched part
    output.writeBytes(source, appendPosition, source.length() - appendPosition);
    return output.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) Region(io.airlift.joni.Region) Block(io.prestosql.spi.block.Block) BlockBuilder(io.prestosql.spi.block.BlockBuilder) SqlNullable(io.prestosql.spi.function.SqlNullable) LiteralParameters(io.prestosql.spi.function.LiteralParameters) SqlType(io.prestosql.spi.function.SqlType)

Example 3 with SqlNullable

use of io.prestosql.spi.function.SqlNullable in project hetu-core by openlookeng.

the class JsonFunctions method jsonArrayContains.

@SqlNullable
@ScalarFunction
@SqlType(StandardTypes.BOOLEAN)
public static Boolean jsonArrayContains(@SqlType(StandardTypes.JSON) Slice json, @SqlType(StandardTypes.DOUBLE) double value) {
    if (!Doubles.isFinite(value)) {
        return false;
    }
    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();
            // noinspection FloatingPointEquality
            if ((token == VALUE_NUMBER_FLOAT) && (parser.getDoubleValue() == value) && (Doubles.isFinite(parser.getDoubleValue()))) {
                return true;
            }
        }
    } catch (IOException e) {
        return null;
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonUtil.createJsonParser(io.prestosql.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) SqlNullable(io.prestosql.spi.function.SqlNullable) ScalarFunction(io.prestosql.spi.function.ScalarFunction) SqlType(io.prestosql.spi.function.SqlType)

Example 4 with SqlNullable

use of io.prestosql.spi.function.SqlNullable in project hetu-core by openlookeng.

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.prestosql.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) SqlNullable(io.prestosql.spi.function.SqlNullable) ScalarFunction(io.prestosql.spi.function.ScalarFunction) SqlType(io.prestosql.spi.function.SqlType)

Example 5 with SqlNullable

use of io.prestosql.spi.function.SqlNullable in project hetu-core by openlookeng.

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.prestosql.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) SqlNullable(io.prestosql.spi.function.SqlNullable) ScalarFunction(io.prestosql.spi.function.ScalarFunction) SqlType(io.prestosql.spi.function.SqlType)

Aggregations

SqlNullable (io.prestosql.spi.function.SqlNullable)52 SqlType (io.prestosql.spi.function.SqlType)52 ScalarFunction (io.prestosql.spi.function.ScalarFunction)40 Description (io.prestosql.spi.function.Description)34 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)28 JsonParser (com.fasterxml.jackson.core.JsonParser)14 PrestoException (io.prestosql.spi.PrestoException)14 JsonUtil.createJsonParser (io.prestosql.util.JsonUtil.createJsonParser)14 IOException (java.io.IOException)14 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)11 MultiPoint (com.esri.core.geometry.MultiPoint)9 Point (com.esri.core.geometry.Point)9 BlockBuilder (io.prestosql.spi.block.BlockBuilder)8 ScalarOperator (io.prestosql.spi.function.ScalarOperator)8 JsonCastException (io.prestosql.util.JsonCastException)8 LiteralParameters (io.prestosql.spi.function.LiteralParameters)7 JsonToken (com.fasterxml.jackson.core.JsonToken)6 Slice (io.airlift.slice.Slice)6 MultiPath (com.esri.core.geometry.MultiPath)4 Block (io.prestosql.spi.block.Block)4