Search in sources :

Example 21 with LiteralParameters

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

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(io.prestosql.spi.PrestoException) Constraint(io.prestosql.type.Constraint) ScalarFunction(io.prestosql.spi.function.ScalarFunction) LiteralParameters(io.prestosql.spi.function.LiteralParameters) SqlType(io.prestosql.spi.function.SqlType)

Example 22 with LiteralParameters

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

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(io.prestosql.spi.function.ScalarFunction) Description(io.prestosql.spi.function.Description) LiteralParameters(io.prestosql.spi.function.LiteralParameters) SqlType(io.prestosql.spi.function.SqlType)

Example 23 with LiteralParameters

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

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(io.prestosql.spi.function.ScalarFunction) LiteralParameters(io.prestosql.spi.function.LiteralParameters) SqlType(io.prestosql.spi.function.SqlType)

Example 24 with LiteralParameters

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

the class IpAddressOperators method castFromVarcharToIpAddress.

@LiteralParameters("x")
@ScalarOperator(CAST)
@SqlType(StandardTypes.IPADDRESS)
public static Slice castFromVarcharToIpAddress(@SqlType("varchar(x)") Slice slice) {
    byte[] address;
    try {
        address = InetAddresses.forString(slice.toStringUtf8()).getAddress();
    } catch (IllegalArgumentException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast value to IPADDRESS: " + slice.toStringUtf8());
    }
    byte[] bytes;
    if (address.length == 4) {
        bytes = new byte[16];
        bytes[10] = (byte) 0xff;
        bytes[11] = (byte) 0xff;
        arraycopy(address, 0, bytes, 12, 4);
    } else if (address.length == 16) {
        bytes = address;
    } else {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, "Invalid InetAddress length: " + address.length);
    }
    return wrappedBuffer(bytes);
}
Also used : PrestoException(io.prestosql.spi.PrestoException) ScalarOperator(io.prestosql.spi.function.ScalarOperator) LiteralParameters(io.prestosql.spi.function.LiteralParameters) SqlType(io.prestosql.spi.function.SqlType)

Example 25 with LiteralParameters

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

the class UrlFunctions method urlExtractParameter.

@SqlNullable
@Description("extract query parameter from url")
@ScalarFunction
@LiteralParameters({ "x", "y" })
@SqlType("varchar(x)")
public static Slice urlExtractParameter(@SqlType("varchar(x)") Slice url, @SqlType("varchar(y)") Slice parameterName) {
    URI uri = parseUrl(url);
    if ((uri == null) || (uri.getQuery() == null)) {
        return null;
    }
    Slice query = slice(uri.getQuery());
    String parameter = parameterName.toStringUtf8();
    Iterable<String> queryArgs = QUERY_SPLITTER.split(query.toStringUtf8());
    for (String queryArg : queryArgs) {
        Iterator<String> arg = ARG_SPLITTER.split(queryArg).iterator();
        if (arg.next().equals(parameter)) {
            if (arg.hasNext()) {
                return utf8Slice(arg.next());
            }
            // first matched key is empty
            return Slices.EMPTY_SLICE;
        }
    }
    // no key matched
    return null;
}
Also used : Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) URI(java.net.URI) 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)

Aggregations

LiteralParameters (io.prestosql.spi.function.LiteralParameters)25 SqlType (io.prestosql.spi.function.SqlType)24 ScalarFunction (io.prestosql.spi.function.ScalarFunction)20 Description (io.prestosql.spi.function.Description)15 Constraint (io.prestosql.type.Constraint)14 Slice (io.airlift.slice.Slice)10 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)8 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)8 PrestoException (io.prestosql.spi.PrestoException)7 SqlNullable (io.prestosql.spi.function.SqlNullable)7 Matcher (io.airlift.joni.Matcher)6 BlockBuilder (io.prestosql.spi.block.BlockBuilder)5 DynamicSliceOutput (io.airlift.slice.DynamicSliceOutput)4 SliceOutput (io.airlift.slice.SliceOutput)4 JsonParser (com.fasterxml.jackson.core.JsonParser)3 Region (io.airlift.joni.Region)3 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)3 JsonUtil.createJsonParser (io.prestosql.util.JsonUtil.createJsonParser)3 IOException (java.io.IOException)3 Block (io.prestosql.spi.block.Block)2