@Description("decodes the UTF-8 encoded string") @ScalarFunction @SqlType(StandardTypes.VARCHAR) public static Slice fromUtf8(@SqlType(StandardTypes.VARBINARY) Slice slice, @SqlType(StandardTypes.VARCHAR) Slice replacementCharacter) { int count = countCodePoints(replacementCharacter); if (count > 1) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Replacement character string must empty or a single character"); } OptionalInt replacementCodePoint; if (count == 1) { try { replacementCodePoint = OptionalInt.of(SliceUtf8.getCodePointAt(replacementCharacter, 0)); } catch (InvalidUtf8Exception e) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Invalid replacement character"); } } else { replacementCodePoint = OptionalInt.empty(); } return SliceUtf8.fixInvalidUtf8(slice, replacementCodePoint); }
@Description("Converts a string to a TIMESTAMP data type") @ScalarFunction("to_timestamp") @SqlType(StandardTypes.TIMESTAMP) public static long toTimestamp( ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice dateTime, @SqlType(StandardTypes.VARCHAR) Slice formatString) { return parseMillis(session, dateTime, formatString); }
@Description("current date") @ScalarFunction @SqlType(StandardTypes.DATE) public static long currentDate(ConnectorSession session) { long millis = getChronology(session.getTimeZoneKey()).dayOfMonth().roundFloor(session.getStartTime()); return MILLISECONDS.toDays(millis); }
@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()); }
@Description("decode URL safe base64 encoded binary data") @ScalarFunction("from_base64url") @SqlType(StandardTypes.VARBINARY) public static Slice fromBase64UrlVarbinary(@SqlType(StandardTypes.VARBINARY) Slice slice) { return Slices.wrappedBuffer(Base64.getUrlDecoder().decode(slice.getBytes())); }
@Nullable @Description("string extracted using the given pattern") @ScalarFunction @SqlType(StandardTypes.VARCHAR) public static Slice regexpExtract(@SqlType(StandardTypes.VARCHAR) Slice source, @SqlType(RegexpType.NAME) Regex pattern) { return regexpExtract(source, pattern, 0); }
@Description("square root") @ScalarFunction @SqlType(StandardTypes.DOUBLE) public static double sqrt(@SqlType(StandardTypes.DOUBLE) double num) { return Math.sqrt(num); }
@Description("current timestamp without time zone") @ScalarFunction("localtimestamp") @SqlType(StandardTypes.TIMESTAMP) public static long localTimestamp(ConnectorSession session) { return session.getStartTime(); }
@Description("cosine") @ScalarFunction @SqlType(StandardTypes.DOUBLE) public static double cos(@SqlType(StandardTypes.DOUBLE) double num) { return Math.cos(num); }
@Description("round to given number of decimal places") @ScalarFunction @SqlType(StandardTypes.DOUBLE) public static double round(@SqlType(StandardTypes.DOUBLE) double num, @SqlType(StandardTypes.BIGINT) long decimals) { if (num == 0.0) { return 0; } if (num < 0) { return -round(-num, decimals); } double factor = Math.pow(10, decimals); return Math.floor(num * factor + 0.5) / factor; }
@Description("truncate to the specified precision") @ScalarFunction("date_trunc") @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) public static long truncateTimeWithTimeZone(@SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone) { long millis = getTimeField(unpackChronology(timeWithTimeZone), unit).roundFloor(unpackMillisUtc(timeWithTimeZone)); return updateMillisUtc(millis, timeWithTimeZone); }
@Description("truncate to the specified precision in the session timezone") @ScalarFunction("date_trunc") @SqlType(StandardTypes.TIMESTAMP) public static long truncateTimestamp(ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { return getTimestampField(getChronology(session.getTimeZoneKey()), unit).roundFloor(timestamp); }
@Description("truncate to the specified precision") @ScalarFunction("date_trunc") @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long truncateTimestampWithTimezone(@SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone) { long millis = getTimestampField(unpackChronology(timestampWithTimeZone), unit).roundFloor(unpackMillisUtc(timestampWithTimeZone)); return updateMillisUtc(millis, timestampWithTimeZone); }
@Description("add the specified amount of date to the given date") @ScalarFunction("date_add") @SqlType(StandardTypes.DATE) public static long addFieldValueDate(ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.DATE) long date) { long millis = getDateField(UTC_CHRONOLOGY, unit).add(DAYS.toMillis(date), Ints.checkedCast(value)); return MILLISECONDS.toDays(millis); }
@Description("add the specified amount of time to the given time") @ScalarFunction("date_add") @SqlType(StandardTypes.TIME) public static long addFieldValueTime(ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.TIME) long time) { ISOChronology chronology = getChronology(session.getTimeZoneKey()); return modulo24Hour(chronology, getTimeField(chronology, unit).add(time, Ints.checkedCast(value))); }
@Description("add the specified amount of time to the given time") @ScalarFunction("date_add") @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) public static long addFieldValueTimeWithTimeZone( @SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone) { ISOChronology chronology = unpackChronology(timeWithTimeZone); long millis = modulo24Hour(chronology, getTimeField(chronology, unit).add(unpackMillisUtc(timeWithTimeZone), Ints.checkedCast(value))); return updateMillisUtc(millis, timeWithTimeZone); }
@Description("add the specified amount of time to the given timestamp") @ScalarFunction("date_add") @SqlType(StandardTypes.TIMESTAMP) public static long addFieldValueTimestamp( ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { return getTimestampField(getChronology(session.getTimeZoneKey()), unit).add(timestamp, Ints.checkedCast(value)); }
@Description("add the specified amount of time to the given timestamp") @ScalarFunction("date_add") @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long addFieldValueTimestampWithTimeZone( @SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone) { long millis = getTimestampField(unpackChronology(timestampWithTimeZone), unit).add(unpackMillisUtc(timestampWithTimeZone), Ints.checkedCast(value)); return updateMillisUtc(millis, timestampWithTimeZone); }
@Description("difference of the given dates in the given unit") @ScalarFunction("date_diff") @SqlType(StandardTypes.BIGINT) public static long diffDate(ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.DATE) long date1, @SqlType(StandardTypes.DATE) long date2) { return getDateField(UTC_CHRONOLOGY, unit).getDifferenceAsLong(DAYS.toMillis(date2), DAYS.toMillis(date1)); }
@Description("tangent") @ScalarFunction @SqlType(StandardTypes.DOUBLE) public static double tan(@SqlType(StandardTypes.DOUBLE) double num) { return Math.tan(num); }
@Description("difference of the given times in the given unit") @ScalarFunction("date_diff") @SqlType(StandardTypes.BIGINT) public static long diffTimeWithTimeZone( @SqlType(StandardTypes.VARCHAR) 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("difference of the given times in the given unit") @ScalarFunction("date_diff") @SqlType(StandardTypes.BIGINT) public static long diffTimestamp( ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.TIMESTAMP) long timestamp1, @SqlType(StandardTypes.TIMESTAMP) long timestamp2) { return getTimestampField(getChronology(session.getTimeZoneKey()), unit).getDifferenceAsLong(timestamp2, timestamp1); }
@Description("difference of the given times in the given unit") @ScalarFunction("date_diff") @SqlType(StandardTypes.BIGINT) public static long diffTimestampWithTimeZone( @SqlType(StandardTypes.VARCHAR) Slice unit, @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone1, @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long timestampWithTimeZone2) { return getTimestampField(unpackChronology(timestampWithTimeZone1), unit).getDifferenceAsLong(unpackMillisUtc(timestampWithTimeZone2), unpackMillisUtc(timestampWithTimeZone1)); }
@Description("decode URL safe base64 encoded binary data") @ScalarFunction("from_base64url") @SqlType(StandardTypes.VARBINARY) public static Slice fromBase64UrlVarchar(@SqlType(StandardTypes.VARCHAR) Slice slice) { return Slices.wrappedBuffer(Base64.getUrlDecoder().decode(slice.getBytes())); }
@Description("formats the given time by the given format") @ScalarFunction @SqlType(StandardTypes.VARCHAR) public static Slice formatDatetime(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp, @SqlType(StandardTypes.VARCHAR) Slice formatString) { return formatDatetime(getChronology(session.getTimeZoneKey()), session.getLocale(), timestamp, formatString); }
@Description("decode hex encoded binary data") @ScalarFunction("from_hex") @SqlType(StandardTypes.VARBINARY) public static Slice fromHexVarbinary(@SqlType(StandardTypes.VARBINARY) Slice slice) { return fromHexVarchar(slice); }
@Description("a pseudo-random value") @ScalarFunction(alias = "rand", deterministic = false) @SqlType(StandardTypes.DOUBLE) public static double random() { return ThreadLocalRandom.current().nextDouble(); }
@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) { // Time is effectively UTC so no need for a custom chronology return SECOND_OF_MINUTE.get(unpackMillisUtc(timestampWithTimeZone)); }
@Description("second of the minute of the given time") @ScalarFunction("second") @SqlType(StandardTypes.BIGINT) public static long secondFromTime(@SqlType(StandardTypes.TIME) long time) { // Time is effectively UTC so no need for a custom chronology return SECOND_OF_MINUTE.get(time); }
@Description("Returns index of first occurrence of a substring (or 0 if not found)") @ScalarFunction("index") @SqlType(StandardTypes.BIGINT) public static long index(@SqlType(StandardTypes.VARCHAR) Slice string, @SqlType(StandardTypes.VARCHAR) Slice substring) { return StringFunctions.stringPosition(string, substring); }
@Description("second of the minute of the given interval") @ScalarFunction("second") @SqlType(StandardTypes.BIGINT) public static long secondFromInterval(@SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long milliseconds) { return (milliseconds % MILLISECONDS_IN_MINUTE) / MILLISECONDS_IN_SECOND; }
@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) { return getChronology(session.getTimeZoneKey()).minuteOfHour().get(timestamp); }
@Description("arc tangent of given fraction") @ScalarFunction @SqlType(StandardTypes.DOUBLE) public static double atan2(@SqlType(StandardTypes.DOUBLE) double num1, @SqlType(StandardTypes.DOUBLE) double num2) { return Math.atan2(num1, num2); }
@Description("hyperbolic cosine") @ScalarFunction @SqlType(StandardTypes.DOUBLE) public static double cosh(@SqlType(StandardTypes.DOUBLE) double num) { return Math.cosh(num); }
@Description("logarithm to base 2") @ScalarFunction @SqlType(StandardTypes.DOUBLE) public static double log2(@SqlType(StandardTypes.DOUBLE) double num) { return Math.log(num) / Math.log(2); }
@Description("encode binary data as hex") @ScalarFunction @SqlType(StandardTypes.VARCHAR) public static Slice toHex(@SqlType(StandardTypes.VARBINARY) Slice slice) { return Slices.utf8Slice(BaseEncoding.base16().encode(slice.getBytes())); }
@Description("round down to nearest integer") @ScalarFunction @SqlType(StandardTypes.BIGINT) public static long floor(@SqlType(StandardTypes.BIGINT) long num) { return num; }
@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("converts an angle in degrees to radians") @ScalarFunction @SqlType(StandardTypes.DOUBLE) public static double radians(@SqlType(StandardTypes.DOUBLE) double degrees) { return Math.toRadians(degrees); }
@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)); }