Refine search
@ScalarOperator(ADD) @SqlType(StandardTypes.TIME) public static long intervalDayToSecondPlusTime(ConnectorSession session, @SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long left, @SqlType(StandardTypes.TIME) long right) { return modulo24Hour(getChronology(session.getTimeZoneKey()), left + right); }
@Description("current time zone") @ScalarFunction("current_timezone") @SqlType(StandardTypes.VARCHAR) public static Slice currentTimeZone(ConnectorSession session) { return utf8Slice(session.getTimeZoneKey().getId()); }
@ScalarOperator(CAST) @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) public static long castToTimeWithTimeZone(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long value) { int timeMillis = modulo24Hour(getChronology(session.getTimeZoneKey()), value); return packDateTimeWithZone(timeMillis, session.getTimeZoneKey()); }
@ScalarOperator(ADD) @SqlType(StandardTypes.TIME) public static long timePlusIntervalDayToSecond(ConnectorSession session, @SqlType(StandardTypes.TIME) long left, @SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long right) { return modulo24Hour(getChronology(session.getTimeZoneKey()), left + right); }
@Description("current timestamp with time zone") @ScalarFunction(value = "current_timestamp", alias = "now") @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long currentTimestamp(ConnectorSession session) { return packDateTimeWithZone(session.getStartTime(), session.getTimeZoneKey()); }
@ScalarOperator(CAST) @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long castToTimestampWithTimeZone(ConnectorSession session, @SqlType(StandardTypes.DATE) long value) { long utcMillis = TimeUnit.DAYS.toMillis(value); // date is encoded as milliseconds at midnight in UTC // convert to midnight if the session timezone ISOChronology chronology = getChronology(session.getTimeZoneKey()); long millis = utcMillis - chronology.getZone().getOffset(utcMillis); return packDateTimeWithZone(millis, session.getTimeZoneKey()); }
@ScalarOperator(SUBTRACT) @SqlType(StandardTypes.TIME) public static long timeMinusIntervalDayToSecond(ConnectorSession session, @SqlType(StandardTypes.TIME) long left, @SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long right) { return modulo24Hour(getChronology(session.getTimeZoneKey()), left - right); }
@ScalarOperator(CAST) @SqlType(JSON) public static Slice castFromTimestamp(ConnectorSession session, @SqlType(TIMESTAMP) long value) { try { SliceOutput output = new DynamicSliceOutput(25); try (JsonGenerator jsonGenerator = createJsonGenerator(JSON_FACTORY, output)) { jsonGenerator.writeString(printTimestampWithoutTimeZone(session.getTimeZoneKey(), value)); } return output.slice(); } catch (IOException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", value, JSON)); } }
@ScalarFunction("to_iso8601") @SqlType(StandardTypes.VARCHAR) public static Slice toISO8601FromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { DateTimeFormatter formatter = ISODateTimeFormat.dateTime() .withChronology(getChronology(session.getTimeZoneKey())); return utf8Slice(formatter.print(timestamp)); }
@ScalarOperator(CAST) @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long castToTimestampWithTimeZone(ConnectorSession session, @SqlType(StandardTypes.DATE) long value) { long utcMillis = TimeUnit.DAYS.toMillis(value); // date is encoded as milliseconds at midnight in UTC // convert to midnight in the session timezone ISOChronology chronology = getChronology(session.getTimeZoneKey()); long millis = utcMillis - chronology.getZone().getOffset(utcMillis); return packDateTimeWithZone(millis, session.getTimeZoneKey()); }
@ScalarOperator(CAST) @LiteralParameters("x") @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long castFromSlice(ConnectorSession session, @SqlType("varchar(x)") Slice value) { try { return parseTimestampWithTimeZone(session.getTimeZoneKey(), trim(value).toStringUtf8()); } catch (IllegalArgumentException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to timestamp with time zone: " + value.toStringUtf8(), e); } }
@ScalarOperator(CAST) @SqlType(StandardTypes.TIMESTAMP) public static long castToTimestamp(ConnectorSession session, @SqlType(StandardTypes.DATE) long value) { long utcMillis = TimeUnit.DAYS.toMillis(value); // date is encoded as milliseconds at midnight in UTC // convert to midnight if the session timezone ISOChronology chronology = getChronology(session.getTimeZoneKey()); return utcMillis - chronology.getZone().getOffset(utcMillis); }
@ScalarOperator(CAST) @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long castToTimestampWithTimeZone(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long value) { if (session.isLegacyTimestamp()) { return packDateTimeWithZone(value, session.getTimeZoneKey()); } else { ISOChronology localChronology = getChronology(session.getTimeZoneKey()); // This cast does treat TIMESTAMP as wall time in session TZ. This means that in order to get // its UTC representation we need to shift the value by the offset of TZ. return packDateTimeWithZone(localChronology.getZone().convertLocalToUTC(value, false), session.getTimeZoneKey()); } }
@ScalarOperator(CAST) @LiteralParameters("x") @SqlType("varchar(x)") public static Slice castToSlice(ConnectorSession session, @SqlType(StandardTypes.TIME) long value) { if (session.isLegacyTimestamp()) { return utf8Slice(printTimeWithoutTimeZone(session.getTimeZoneKey(), value)); } else { return utf8Slice(printTimeWithoutTimeZone(value)); } }
@ScalarOperator(CAST) @SqlType(StandardTypes.TIME) public static long castToTime(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long value) { return modulo24Hour(getChronology(session.getTimeZoneKey()), value); }
@ScalarOperator(CAST) @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) public static long castToTimeWithTimeZone(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long value) { if (session.isLegacyTimestamp()) { int timeMillis = modulo24Hour(getChronology(session.getTimeZoneKey()), value); return packDateTimeWithZone(timeMillis, session.getTimeZoneKey()); } else { ISOChronology localChronology = getChronology(session.getTimeZoneKey()); // This cast does treat TIMESTAMP as wall time in session TZ. This means that in order to get // its UTC representation we need to shift the value by the offset of TZ. return packDateTimeWithZone(localChronology.getZone().convertLocalToUTC(modulo24Hour(value), false), session.getTimeZoneKey()); } }
@ScalarOperator(CAST) @LiteralParameters("x") @SqlType("varchar(x)") public static Slice castToSlice(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long value) { if (session.isLegacyTimestamp()) { return utf8Slice(printTimestampWithoutTimeZone(session.getTimeZoneKey(), value)); } else { return utf8Slice(printTimestampWithoutTimeZone(value)); } }
@Description("current time with time zone") @ScalarFunction @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) public static long currentTime(ConnectorSession session) { // Stack value is number of milliseconds from start of the current day, // but the start of the day is relative to the current time zone. long millis = getChronology(session.getTimeZoneKey()).millisOfDay().get(session.getStartTime()); return packDateTimeWithZone(millis, session.getTimeZoneKey()); }
@ScalarOperator(CAST) @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) public static long castToTimeWithTimeZone(ConnectorSession session, @SqlType(StandardTypes.TIME) long value) { if (session.isLegacyTimestamp()) { return packDateTimeWithZone(value, session.getTimeZoneKey()); } else { ISOChronology localChronology = getChronology(session.getTimeZoneKey()); // This cast does treat TIME as wall time in session TZ. This means that in order to get // its UTC representation we need to shift the value by the offset of TZ. // We use value offset in this place to be sure that we will have same hour represented // in TIME WITH TIME ZONE. Calculating real TZ offset will happen when really required. // This is done due to inadequate TIME WITH TIME ZONE representation. return packDateTimeWithZone(localChronology.getZone().convertLocalToUTC(value, false), session.getTimeZoneKey()); } }
@Description("current time with time zone") @ScalarFunction @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) public static long currentTime(ConnectorSession session) { // We do all calculation in UTC, as session.getStartTime() is in UTC // and we need to have UTC millis for packDateTimeWithZone long millis = UTC_CHRONOLOGY.millisOfDay().get(session.getStartTime()); if (!session.isLegacyTimestamp()) { // However, those UTC millis are pointing to the correct UTC timestamp // Our TIME WITH TIME ZONE representation does use UTC 1970-01-01 representation // So we have to hack here in order to get valid representation // of TIME WITH TIME ZONE millis -= valueToSessionTimeZoneOffsetDiff(session.getStartTime(), getDateTimeZone(session.getTimeZoneKey())); } return packDateTimeWithZone(millis, session.getTimeZoneKey()); }