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);
}
}
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);
}
}
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));
}
}
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);
}
}
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();
}
Aggregations