Search in sources :

Example 1 with ScalarFunction

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

the class CreateHll method createHll.

@ScalarFunction
@SqlType(StandardTypes.HYPER_LOG_LOG)
public static Slice createHll(@SqlType(StandardTypes.BIGINT) long value) {
    HyperLogLog hll = HyperLogLog.newInstance(4096);
    hll.add(value);
    return hll.serialize();
}
Also used : HyperLogLog(io.airlift.stats.cardinality.HyperLogLog) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) SqlType(com.facebook.presto.spi.function.SqlType)

Example 2 with ScalarFunction

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

the class ColorFunctions method color.

@ScalarFunction
@LiteralParameters("x")
@SqlType(ColorType.NAME)
public static long color(@SqlType("varchar(x)") Slice color) {
    int rgb = parseRgb(color);
    if (rgb != -1) {
        return rgb;
    }
    // encode system colors (0-15) as negative values, offset by one
    try {
        SystemColor systemColor = SystemColor.valueOf(upper(color).toStringUtf8());
        int index = systemColor.getIndex();
        return -(index + 1);
    } catch (IllegalArgumentException e) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Invalid color: '%s'", color.toStringUtf8()), e);
    }
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) Constraint(com.facebook.presto.type.Constraint) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 3 with ScalarFunction

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

the class DateTimeFunctions method toISO8601FromTimestampWithTimeZone.

@ScalarFunction("to_iso8601")
@SqlType("varchar(35)")
public static // the maximum year represented by 64bits timestamp is ~584944387 it may require up to 35 characters.
Slice toISO8601FromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone) {
    long millisUtc = unpackMillisUtc(timestampWithTimeZone);
    DateTimeFormatter formatter = ISODateTimeFormat.dateTime().withChronology(getChronology(unpackZoneKey(timestampWithTimeZone)));
    return utf8Slice(formatter.print(millisUtc));
}
Also used : DateTimeFormatter(org.joda.time.format.DateTimeFormatter) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) SqlType(com.facebook.presto.spi.function.SqlType)

Example 4 with ScalarFunction

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

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(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) SqlType(com.facebook.presto.spi.function.SqlType)

Example 5 with ScalarFunction

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

the class JsonFunctions method jsonArrayLength.

@SqlNullable
@ScalarFunction
@SqlType(StandardTypes.BIGINT)
public static Long jsonArrayLength(@SqlType(StandardTypes.JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        if (parser.nextToken() != START_ARRAY) {
            return null;
        }
        long length = 0;
        while (true) {
            JsonToken token = parser.nextToken();
            if (token == null) {
                return null;
            }
            if (token == END_ARRAY) {
                return length;
            }
            parser.skipChildren();
            length++;
        }
    } 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) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)37 SqlType (com.facebook.presto.spi.function.SqlType)36 Description (com.facebook.presto.spi.function.Description)19 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)17 Constraint (com.facebook.presto.type.Constraint)15 Slice (io.airlift.slice.Slice)10 SqlNullable (com.facebook.presto.spi.function.SqlNullable)9 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)7 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)7 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)7 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)7 IOException (java.io.IOException)7 PrestoException (com.facebook.presto.spi.PrestoException)6 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)6 JsonParser (com.fasterxml.jackson.core.JsonParser)6 JsonToken (com.fasterxml.jackson.core.JsonToken)6 Matcher (io.airlift.joni.Matcher)5 ISOChronology (org.joda.time.chrono.ISOChronology)4 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)3 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)3