Search in sources :

Example 6 with LiteralParameters

use of com.facebook.presto.spi.function.LiteralParameters in project presto by prestodb.

the class JsonFunctions method jsonParse.

@ScalarFunction
@LiteralParameters("x")
@SqlType(StandardTypes.JSON)
public static Slice jsonParse(@SqlType("varchar(x)") Slice slice) {
    try {
        byte[] in = slice.getBytes();
        SliceOutput dynamicSliceOutput = new DynamicSliceOutput(in.length);
        SORTED_MAPPER.writeValue((OutputStream) dynamicSliceOutput, SORTED_MAPPER.readValue(in, Object.class));
        return dynamicSliceOutput.slice();
    } catch (Exception e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Cannot convert '%s' to JSON", slice.toStringUtf8()));
    }
}
Also used : SliceOutput(io.airlift.slice.SliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) PrestoException(com.facebook.presto.spi.PrestoException) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 7 with LiteralParameters

use of com.facebook.presto.spi.function.LiteralParameters in project presto by prestodb.

the class JsonFunctions method jsonArrayContains.

@SqlNullable
@ScalarFunction
@LiteralParameters("x")
@SqlType(StandardTypes.BOOLEAN)
public static Boolean jsonArrayContains(@SqlType(StandardTypes.JSON) Slice json, @SqlType("varchar(x)") Slice value) {
    String valueString = value.toStringUtf8();
    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_STRING && valueString.equals(parser.getValueAsString())) {
                return true;
            }
        }
    } catch (IOException e) {
        return null;
    }
}
Also used : JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) SqlNullable(com.facebook.presto.spi.function.SqlNullable) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 8 with LiteralParameters

use of com.facebook.presto.spi.function.LiteralParameters in project presto by prestodb.

the class DateTimeFunctions method addFieldValueTimeWithTimeZone.

@Description("add the specified amount of time to the given time")
@LiteralParameters("x")
@ScalarFunction("date_add")
@SqlType(StandardTypes.TIME_WITH_TIME_ZONE)
public static long addFieldValueTimeWithTimeZone(@SqlType("varchar(x)") Slice unit, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone) {
    ISOChronology chronology = unpackChronology(timeWithTimeZone);
    long millis = modulo24Hour(chronology, getTimeField(chronology, unit).add(unpackMillisUtc(timeWithTimeZone), toIntExact(value)));
    return updateMillisUtc(millis, timeWithTimeZone);
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 9 with LiteralParameters

use of com.facebook.presto.spi.function.LiteralParameters in project presto by prestodb.

the class DateTimeFunctions method fromISO8601Date.

@ScalarFunction("from_iso8601_date")
@LiteralParameters("x")
@SqlType(StandardTypes.DATE)
public static long fromISO8601Date(ConnectorSession session, @SqlType("varchar(x)") Slice iso8601DateTime) {
    DateTimeFormatter formatter = ISODateTimeFormat.dateElementParser().withChronology(UTC_CHRONOLOGY);
    DateTime dateTime = parseDateTimeHelper(formatter, iso8601DateTime.toStringUtf8());
    return MILLISECONDS.toDays(dateTime.getMillis());
}
Also used : DateTimeFormatter(org.joda.time.format.DateTimeFormatter) DateTime(org.joda.time.DateTime) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 10 with LiteralParameters

use of com.facebook.presto.spi.function.LiteralParameters in project presto by prestodb.

the class JoniRegexpFunctions method regexpSplit.

@ScalarFunction
@LiteralParameters("x")
@Description("returns array of strings split by pattern")
@SqlType("array(varchar(x))")
public static Block regexpSplit(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) Regex pattern) {
    Matcher matcher = pattern.matcher(source.getBytes());
    BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), 32);
    int lastEnd = 0;
    int nextStart = 0;
    while (true) {
        int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
        if (offset == -1) {
            break;
        }
        if (matcher.getEnd() == matcher.getBegin()) {
            nextStart = matcher.getEnd() + 1;
        } else {
            nextStart = matcher.getEnd();
        }
        Slice slice = source.slice(lastEnd, matcher.getBegin() - lastEnd);
        lastEnd = matcher.getEnd();
        VARCHAR.writeSlice(blockBuilder, slice);
    }
    VARCHAR.writeSlice(blockBuilder, source.slice(lastEnd, source.length() - lastEnd));
    return blockBuilder.build();
}
Also used : Matcher(io.airlift.joni.Matcher) Slice(io.airlift.slice.Slice) Constraint(com.facebook.presto.type.Constraint) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)21 SqlType (com.facebook.presto.spi.function.SqlType)19 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)17 Description (com.facebook.presto.spi.function.Description)12 Constraint (com.facebook.presto.type.Constraint)12 Slice (io.airlift.slice.Slice)8 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)7 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)6 PrestoException (com.facebook.presto.spi.PrestoException)5 SqlNullable (com.facebook.presto.spi.function.SqlNullable)5 Matcher (io.airlift.joni.Matcher)5 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)3 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)3 IOException (java.io.IOException)3 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)2 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)2 JsonParser (com.fasterxml.jackson.core.JsonParser)2 JsonToken (com.fasterxml.jackson.core.JsonToken)2 Region (io.airlift.joni.Region)2 DynamicSliceOutput (io.airlift.slice.DynamicSliceOutput)2