@ScalarOperator(LESS_THAN) @SqlType(StandardTypes.BOOLEAN) public static boolean lessThan(@SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long left, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long right) { return unpackMillisUtc(left) < unpackMillisUtc(right); }
@ScalarOperator(GREATER_THAN) @SqlType(StandardTypes.BOOLEAN) public static boolean greaterThan(@SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long left, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long right) { return unpackMillisUtc(left) > unpackMillisUtc(right); }
@ScalarOperator(LESS_THAN_OR_EQUAL) @SqlType(StandardTypes.BOOLEAN) public static boolean lessThanOrEqual(@SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long left, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long right) { return unpackMillisUtc(left) <= unpackMillisUtc(right); }
@ScalarOperator(XX_HASH_64) @SqlType(StandardTypes.BIGINT) public static long xxHash64(@SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long value) { return XxHash64.hash(unpackMillisUtc(value)); }
@ScalarOperator(CAST) @SqlType(StandardTypes.TIMESTAMP) public static long castToTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long value) { if (session.isLegacyTimestamp()) { return unpackMillisUtc(value); } else { ISOChronology chronology = getChronology(unpackZoneKey(value)); return chronology.getZone().convertUTCToLocal(unpackMillisUtc(value)); } }
@ScalarOperator(ADD) @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long timestampWithTimeZonePlusIntervalDayToSecond(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long left, @SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long right) { return updateMillisUtc(unpackMillisUtc(left) + right, left); }
@Description("difference of the given times in the given unit") @ScalarFunction("date_diff") @LiteralParameters("x") @SqlType(StandardTypes.BIGINT) public static long diffTimeWithTimeZone( @SqlType("varchar(x)") Slice unit, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone1, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone2) { return getTimeField(unpackChronology(timeWithTimeZone1), unit).getDifferenceAsLong(unpackMillisUtc(timeWithTimeZone2), unpackMillisUtc(timeWithTimeZone1)); }
@Description("millisecond of the second of the given timestamp") @ScalarFunction("millisecond") @SqlType(StandardTypes.BIGINT) public static long millisecondFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone) { // No need to check the associated zone here. A zone always has offset of whole minutes. return MILLISECOND_OF_SECOND.get(unpackMillisUtc(timestampWithTimeZone)); }
@Description("difference of the given times in the given unit") @ScalarFunction("date_diff") @LiteralParameters("x") @SqlType(StandardTypes.BIGINT) public static long diffTimeWithTimeZone( @SqlType("varchar(x)") Slice unit, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone1, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone2) { return getTimeField(unpackChronology(timeWithTimeZone1), unit).getDifferenceAsLong(unpackMillisUtc(timeWithTimeZone2), unpackMillisUtc(timeWithTimeZone1)); }
@Description("second of the minute of the given timestamp") @ScalarFunction("second") @SqlType(StandardTypes.BIGINT) public static long secondFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone) { // No need to check the associated zone here. A zone always has offset of whole minutes. return SECOND_OF_MINUTE.get(unpackMillisUtc(timestampWithTimeZone)); }
@ScalarOperator(ADD) @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) public static long intervalDayToSecondPlusTimeWithTimeZone(@SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long left, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long right) { return updateMillisUtc((long) modulo24Hour(unpackChronology(right), left + unpackMillisUtc(right)), right); }
@ScalarOperator(SUBTRACT) @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) public static long timeWithTimeZoneMinusIntervalDayToSecond(@SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long left, @SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long right) { return updateMillisUtc((long) modulo24Hour(unpackChronology(left), unpackMillisUtc(left) - right), left); }
@Description("minute of the hour of the given time") @ScalarFunction("minute") @SqlType(StandardTypes.BIGINT) public static long minuteFromTimeWithTimeZone(@SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone) { return unpackChronology(timeWithTimeZone).minuteOfHour().get(unpackMillisUtc(timeWithTimeZone)); }
@Description("hour of the day of the given time") @ScalarFunction("hour") @SqlType(StandardTypes.BIGINT) public static long hourFromTimeWithTimeZone(@SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone) { return unpackChronology(timeWithTimeZone).hourOfDay().get(unpackMillisUtc(timeWithTimeZone)); }
@Description("day of the year of the given timestamp") @ScalarFunction(value = "day_of_year", alias = "doy") @SqlType(StandardTypes.BIGINT) public static long dayOfYearFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone) { return unpackChronology(timestampWithTimeZone).dayOfYear().get(unpackMillisUtc(timestampWithTimeZone)); }
@Description("hour of the day of the given timestamp") @ScalarFunction("hour") @SqlType(StandardTypes.BIGINT) public static long hourFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone) { return unpackChronology(timestampWithTimeZone).hourOfDay().get(unpackMillisUtc(timestampWithTimeZone)); }
@Description("year of the given timestamp") @ScalarFunction("year") @SqlType(StandardTypes.BIGINT) public static long yearFromTimestampWithTimeZone(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone) { return unpackChronology(timestampWithTimeZone).year().get(unpackMillisUtc(timestampWithTimeZone)); }
@Description("add the specified amount of time to the given timestamp") @LiteralParameters("x") @ScalarFunction("date_add") @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long addFieldValueTimestampWithTimeZone( @SqlType("varchar(x)") Slice unit, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone) { long millis = getTimestampField(unpackChronology(timestampWithTimeZone), unit).add(unpackMillisUtc(timestampWithTimeZone), toIntExact(value)); return updateMillisUtc(millis, timestampWithTimeZone); }
@ScalarOperator(CAST) @SqlType(StandardTypes.TIME) public static long castToTime(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long value) { if (session.isLegacyTimestamp()) { return modulo24Hour(unpackChronology(value), unpackMillisUtc(value)); } else { return modulo24Hour(castToTimestamp(session, value)); } }
@Description("truncate to the specified precision") @ScalarFunction("date_trunc") @LiteralParameters("x") @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) public static long truncateTimeWithTimeZone(@SqlType("varchar(x)") Slice unit, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone) { long millis = getTimeField(unpackChronology(timeWithTimeZone), unit).roundFloor(unpackMillisUtc(timeWithTimeZone)); return updateMillisUtc(millis, timeWithTimeZone); }