public static SqlTimestamp sqlTimestampOf(long millis, ConnectorSession session) { if (session.isLegacyTimestamp()) { return new SqlTimestamp(millis, session.getTimeZoneKey()); } else { return new SqlTimestamp(millis); } }
@Description("add the specified amount of time to the given time") @LiteralParameters("x") @ScalarFunction("date_add") @SqlType(StandardTypes.TIME) public static long addFieldValueTime(ConnectorSession session, @SqlType("varchar(x)") Slice unit, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.TIME) long time) { if (session.isLegacyTimestamp()) { ISOChronology chronology = getChronology(session.getTimeZoneKey()); return modulo24Hour(chronology, getTimeField(chronology, unit).add(time, toIntExact(value))); } return modulo24Hour(getTimeField(UTC_CHRONOLOGY, unit).add(time, toIntExact(value))); }
@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)); } }
@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)); } }
@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)); } }
@Description("year of the given timestamp") @ScalarFunction("year") @SqlType(StandardTypes.BIGINT) public static long yearFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).year().get(timestamp); } else { return YEAR.get(timestamp); } }
@Description("minute of the hour of the given time") @ScalarFunction("minute") @SqlType(StandardTypes.BIGINT) public static long minuteFromTime(ConnectorSession session, @SqlType(StandardTypes.TIME) long time) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).minuteOfHour().get(time); } else { return MINUTE_OF_HOUR.get(time); } }
@Description("month of the year of the given timestamp") @ScalarFunction("month") @SqlType(StandardTypes.BIGINT) public static long monthFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).monthOfYear().get(timestamp); } else { return MONTH_OF_YEAR.get(timestamp); } }
@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)); } }
@Description("week of the year of the given timestamp") @ScalarFunction(value = "week", alias = "week_of_year") @SqlType(StandardTypes.BIGINT) public static long weekFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).weekOfWeekyear().get(timestamp); } else { return WEEK_OF_YEAR.get(timestamp); } }
@Description("month of the year of the given timestamp") @ScalarFunction("month") @SqlType(StandardTypes.BIGINT) public static long monthFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).monthOfYear().get(timestamp); } else { return MONTH_OF_YEAR.get(timestamp); } }
@ScalarOperator(ADD) @SqlType(StandardTypes.TIMESTAMP) public static long timestampPlusIntervalYearToMonth(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long left, @SqlType(StandardTypes.INTERVAL_YEAR_TO_MONTH) long right) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).monthOfYear().add(left, right); } else { return MONTH_OF_YEAR_UTC.add(left, right); } }
@ScalarOperator(ADD) @SqlType(StandardTypes.TIMESTAMP) public static long timestampPlusIntervalYearToMonth(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long left, @SqlType(StandardTypes.INTERVAL_YEAR_TO_MONTH) long right) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).monthOfYear().add(left, right); } else { return MONTH_OF_YEAR_UTC.add(left, right); } }
@ScalarOperator(ADD) @SqlType(StandardTypes.TIMESTAMP) public static long intervalYearToMonthPlusTimestamp(ConnectorSession session, @SqlType(StandardTypes.INTERVAL_YEAR_TO_MONTH) long left, @SqlType(StandardTypes.TIMESTAMP) long right) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).monthOfYear().add(right, left); } else { return MONTH_OF_YEAR_UTC.add(right, left); } }
@Description("hour of the day of the given timestamp") @ScalarFunction("hour") @SqlType(StandardTypes.BIGINT) public static long hourFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).hourOfDay().get(timestamp); } else { return HOUR_OF_DAY.get(timestamp); } }
@Description("day of the month of the given timestamp") @ScalarFunction(value = "day", alias = "day_of_month") @SqlType(StandardTypes.BIGINT) public static long dayFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).dayOfMonth().get(timestamp); } else { return DAY_OF_MONTH.get(timestamp); } }
@Description("week of the year of the given timestamp") @ScalarFunction(value = "week", alias = "week_of_year") @SqlType(StandardTypes.BIGINT) public static long weekFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).weekOfWeekyear().get(timestamp); } else { return WEEK_OF_YEAR.get(timestamp); } }
@Description("minute of the hour of the given timestamp") @ScalarFunction("minute") @SqlType(StandardTypes.BIGINT) public static long minuteFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).minuteOfHour().get(timestamp); } else { return MINUTE_OF_HOUR.get(timestamp); } }
@Description("current time without time zone") @ScalarFunction("localtime") @SqlType(StandardTypes.TIME) public static long localTime(ConnectorSession session) { if (session.isLegacyTimestamp()) { return UTC_CHRONOLOGY.millisOfDay().get(session.getStartTime()); } ISOChronology localChronology = getChronology(session.getTimeZoneKey()); return localChronology.millisOfDay().get(session.getStartTime()); }
@ScalarFunction @LiteralParameters("x") @SqlType(StandardTypes.VARCHAR) public static Slice dateFormat(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp, @SqlType("varchar(x)") Slice formatString) { if (session.isLegacyTimestamp()) { return dateFormat(getChronology(session.getTimeZoneKey()), session.getLocale(), timestamp, formatString); } else { return dateFormat(UTC_CHRONOLOGY, session.getLocale(), timestamp, formatString); } }