public static SqlTimestamp sqlTimestampOf(long millis, ConnectorSession session) { if (session.isLegacyTimestamp()) { return new SqlTimestamp(millis, session.getTimeZoneKey()); } else { return new SqlTimestamp(millis); } }
@Override protected Long visitTimeLiteral(TimeLiteral node, ConnectorSession session) { if (session.isLegacyTimestamp()) { return parseTimeLiteral(session.getTimeZoneKey(), node.getValue()); } else { return parseTimeLiteral(node.getValue()); } }
@ScalarOperator(CAST) @SqlType(StandardTypes.TIME) public static long castToTime(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long value) { if (session.isLegacyTimestamp()) { return modulo24Hour(getChronology(session.getTimeZoneKey()), value); } else { return modulo24Hour(value); } }
@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))); }
@Description("hour of the day of the given time") @ScalarFunction("hour") @SqlType(StandardTypes.BIGINT) public static long hourFromTime(ConnectorSession session, @SqlType(StandardTypes.TIME) long time) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).hourOfDay().get(time); } else { return HOUR_OF_DAY.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)); } }
@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); } }
@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("day of the year of the given timestamp") @ScalarFunction(value = "day_of_year", alias = "doy") @SqlType(StandardTypes.BIGINT) public static long dayOfYearFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).dayOfYear().get(timestamp); } else { return DAY_OF_YEAR.get(timestamp); } }
@Description("year of the ISO week of the given timestamp") @ScalarFunction(value = "year_of_week", alias = "yow") @SqlType(StandardTypes.BIGINT) public static long yearOfWeekFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { if (session.isLegacyTimestamp()) { return getChronology(session.getTimeZoneKey()).weekyear().get(timestamp); } else { return YEAR_OF_WEEK.get(timestamp); } }
@ScalarOperator(SUBTRACT) @SqlType(StandardTypes.TIMESTAMP) public static long timestampMinusIntervalYearToMonth(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(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("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("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); } }
@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("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("quarter of the year of the given timestamp") @ScalarFunction("quarter") @SqlType(StandardTypes.BIGINT) public static long quarterFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { if (session.isLegacyTimestamp()) { return QUARTER_OF_YEAR.getField(getChronology(session.getTimeZoneKey())).get(timestamp); } else { return QUARTER_OF_YEAR.getField(UTC_CHRONOLOGY).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()); }
@Description("truncate to the specified precision in the session timezone") @ScalarFunction("date_trunc") @LiteralParameters("x") @SqlType(StandardTypes.TIME) public static long truncateTime(ConnectorSession session, @SqlType("varchar(x)") Slice unit, @SqlType(StandardTypes.TIME) long time) { if (session.isLegacyTimestamp()) { return getTimeField(getChronology(session.getTimeZoneKey()), unit).roundFloor(time); } else { return getTimeField(UTC_CHRONOLOGY, unit).roundFloor(time); } }