Search in sources :

Example 6 with ScalarOperator

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

the class JsonOperators method castToBoolean.

@ScalarOperator(CAST)
@SqlNullable
@SqlType(BOOLEAN)
public static Boolean castToBoolean(@SqlType(JSON) Slice json) {
    try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
        parser.nextToken();
        Boolean result = currentTokenAsBoolean(parser);
        // check no trailing token
        checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to BOOLEAN");
        return result;
    } catch (IOException | JsonCastException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), BOOLEAN), e);
    }
}
Also used : JsonCastException(com.facebook.presto.util.JsonCastException) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) JsonUtil.currentTokenAsBoolean(com.facebook.presto.util.JsonUtil.currentTokenAsBoolean) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlNullable(com.facebook.presto.spi.function.SqlNullable) SqlType(com.facebook.presto.spi.function.SqlType)

Example 7 with ScalarOperator

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

the class TimestampOperators method castToDate.

@ScalarFunction("date")
@ScalarOperator(CAST)
@SqlType(StandardTypes.DATE)
public static long castToDate(SqlFunctionProperties properties, @SqlType(StandardTypes.TIMESTAMP) long value) {
    ISOChronology chronology;
    if (properties.isLegacyTimestamp()) {
        // round down the current timestamp to days
        chronology = getChronology(properties.getTimeZoneKey());
        long date = chronology.dayOfYear().roundFloor(value);
        // date is currently midnight in timezone of the session
        // convert to UTC
        long millis = date + chronology.getZone().getOffset(date);
        return TimeUnit.MILLISECONDS.toDays(millis);
    } else {
        return TimeUnit.MILLISECONDS.toDays(value);
    }
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) SqlType(com.facebook.presto.spi.function.SqlType)

Example 8 with ScalarOperator

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

the class TimestampWithTimeZoneOperators method castToTimeWithTimeZone.

@ScalarOperator(CAST)
@SqlType(StandardTypes.TIME_WITH_TIME_ZONE)
public static long castToTimeWithTimeZone(SqlFunctionProperties properties, @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long value) {
    if (properties.isLegacyTimestamp()) {
        int millis = modulo24Hour(unpackChronology(value), unpackMillisUtc(value));
        return packDateTimeWithZone(millis, unpackZoneKey(value));
    } else {
        long millis = modulo24Hour(castToTimestamp(properties, value));
        ISOChronology localChronology = unpackChronology(value);
        // This is done due to inadequate TIME WITH TIME ZONE representation.
        return packDateTimeWithZone(millis - localChronology.getZone().getOffset(millis), unpackZoneKey(value));
    }
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlType(com.facebook.presto.spi.function.SqlType)

Example 9 with ScalarOperator

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

the class TimeWithTimeZoneOperators method castToTimestamp.

@ScalarOperator(CAST)
@SqlType(StandardTypes.TIMESTAMP)
public static long castToTimestamp(SqlFunctionProperties properties, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long value) {
    if (properties.isLegacyTimestamp()) {
        return unpackMillisUtc(value);
    } else {
        // This is hack that we need to use as the timezone interpretation depends on date (not only on time)
        // TODO remove REFERENCE_TIMESTAMP_UTC when removing support for political time zones in TIME WIT TIME ZONE
        long currentMillisOfDay = ChronoField.MILLI_OF_DAY.getFrom(Instant.ofEpochMilli(REFERENCE_TIMESTAMP_UTC).atZone(ZoneOffset.UTC));
        long timeMillisUtcInCurrentDay = REFERENCE_TIMESTAMP_UTC - currentMillisOfDay + unpackMillisUtc(value);
        ISOChronology chronology = getChronology(unpackZoneKey(value));
        return unpackMillisUtc(value) + chronology.getZone().getOffset(timeMillisUtcInCurrentDay);
    }
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlType(com.facebook.presto.spi.function.SqlType)

Example 10 with ScalarOperator

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

the class HyperLogLogOperators method castToP4Hll.

@ScalarOperator(CAST)
@SqlType(StandardTypes.P4_HYPER_LOG_LOG)
public static Slice castToP4Hll(@SqlType(StandardTypes.HYPER_LOG_LOG) Slice slice) {
    HyperLogLog hll = HyperLogLog.newInstance(slice);
    hll.makeDense();
    return hll.serialize();
}
Also used : HyperLogLog(com.facebook.airlift.stats.cardinality.HyperLogLog) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)26 SqlType (com.facebook.presto.spi.function.SqlType)24 PrestoException (com.facebook.presto.spi.PrestoException)13 ISOChronology (org.joda.time.chrono.ISOChronology)9 SqlNullable (com.facebook.presto.spi.function.SqlNullable)8 JsonCastException (com.facebook.presto.util.JsonCastException)8 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)8 JsonParser (com.fasterxml.jackson.core.JsonParser)8 IOException (java.io.IOException)8 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)5 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)4 ImmutableList (com.google.common.collect.ImmutableList)2 Slice (io.airlift.slice.Slice)2 UnknownHostException (java.net.UnknownHostException)2 HyperLogLog (com.facebook.airlift.stats.cardinality.HyperLogLog)1 ScalarHeader (com.facebook.presto.operator.scalar.ScalarHeader)1 JsonUtil.currentTokenAsBoolean (com.facebook.presto.util.JsonUtil.currentTokenAsBoolean)1 JsonUtil.currentTokenAsDouble (com.facebook.presto.util.JsonUtil.currentTokenAsDouble)1 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)1 Method (java.lang.reflect.Method)1