Temporal Functions

Table 1 lists the time functions supported by Flink OpenSource SQL.

Description

Table 1 Temporal Functions

Function

Return Type

Description

DATE string

DATE

Returns the SQL date parsed from a string in the format of "yyyy-MM-dd".

DATE_ADD

STRING

Resulting date after adding a certain number of days to a specified date. The data type is STRING.

DATE_SUB

STRING

Resulting date after subtracting a certain number of days from a specified date. The data type is STRING.

TIME string

TIME

Returns the SQL time parsed from a string in the format of "HH:mm:ss".

TIMESTAMP string

TIMESTAMP

Returns the SQL timestamp parsed from a string in the format of "yyyy-MM-dd HH:mm:ss[.SSS]".

INTERVAL string range

INTERVAL

Parses the SQL millisecond interval from a string in the format of "dd hh:mm:ss.fff" or the SQL month interval from a string in the format of "yyyy-mm".

The interval range can be DAY, MINUTE, DAY TO HOUR, or DAY TO SECOND, with the interval in milliseconds; YEAR or YEAR TO MONTH represents the interval in months.

For example, INTERVAL '10 00:00:00.004' DAY TO SECOND, INTERVAL '10' DAY, or INTERVAL '2-10' YEAR TO MONTH returns the interval.

CURRENT_DATE

DATE

Returns the current SQL date in the local time zone. In streaming mode, it is evaluated for each record. In batch processing mode, it is evaluated once at the beginning of the query and the same result is used for each row.

CURRENT_TIME

TIME

Returns the current SQL time in the local time zone, which is a synonym for LOCAL_TIME.

CURRENT_TIMESTAMP

TIMESTAMP

Returns the current SQL timestamp in the local time zone, with the return type of TIMESTAMP_LTZ(3). In streaming mode, it is evaluated for each record. In batch processing mode, it is evaluated once at the beginning of the query and the same result is used for each row.

LOCALTIME

TIME

Returns the current SQL time in the local time zone, with the return type of TIME(0). In streaming mode, it is evaluated for each record. In batch processing mode, it is evaluated once at the beginning of the query and the same result is used for each row.

LOCALTIMESTAMP

TIMESTAMP

Returns the current SQL timestamp in the local time zone, with the return type of TIMESTAMP(3). In streaming mode, it is evaluated for each record. In batch processing mode, it is evaluated once at the beginning of the query and the same result is used for each row.

NOW()

TIMESTAMP

Returns the current SQL timestamp in the local time zone, which is a synonym for CURRENT_TIMESTAMP.

CURRENT_ROW_TIMESTAMP()

TIMESTAMP_LTZ(3)

Returns the current SQL timestamp in the local time zone, with the return type of TIMESTAMP_LTZ(3). It is evaluated for each record, regardless of whether it is in batch processing mode or streaming mode.

EXTRACT(timeintervalunit FROM temporal)

BIGINT

Returns the long value extracted from the time interval unit part of the time.

For example, EXTRACT(DAY FROM DATE '2006-06-05') returns 5.

YEAR(date)

BIGINT

Returns the year from the SQL date, which is equivalent to EXTRACT(YEAR FROM date). For example, YEAR(DATE '1994-09-27') returns 1994.

QUARTER(date)

BIGINT

Returns the quarter of the year from the SQL date, which is an integer between 1 and 4, equivalent to EXTRACT(QUARTER FROM date).

For example, QUARTER(DATE '1994-09-27') returns 3.

MONTH(date)

BIGINT

Returns the month of the year from the SQL date, which is an integer between 1 and 12, equivalent to EXTRACT(MONTH FROM date).

For example, MONTH(DATE '1994-09-27') returns 9.

WEEK(date)

BIGINT

Returns the week of the year from the SQL date, which is an integer between 1 and 53, equivalent to EXTRACT(WEEK FROM date).

For example, WEEK(DATE '1994-09-27') returns 39.

DAYOFYEAR(date)

BIGINT

Returns the day of the year from the SQL date, which is an integer between 1 and 366, equivalent to EXTRACT(DOY FROM date).

For example, DAYOFYEAR(DATE '1994-09-27') returns 270.

DAYOFMONTH(date)

BIGINT

Returns the day of the month from the SQL date, which is an integer between 1 and 31, equivalent to EXTRACT(DAY FROM date).

For example, DAYOFWEEK(DATE '1994-09-27') returns 3.

DAYOFWEEK(date)

BIGINT

Calculates which day of the week the current date is, with Sunday being 1.

For example, DAYOFWEEK(DATE'1994-09-27') returns 3.

HOUR(timestamp)

BIGINT

Returns the hour part of the hour unit from the SQL timestamp, which is an integer between 0 and 23, equivalent to EXTRACT(HOUR FROM timestamp).

For example, MINUTE(TIMESTAMP '1994-09-27 13:14:15') returns 14.

MINUTE(timestamp)

BIGINT

Returns the minute part of the minute unit from the SQL timestamp, which is an integer between 0 and 59, equivalent to EXTRACT(MINUTE FROM timestamp).

For example, MINUTE(TIMESTAMP '1994-09-27 13:14:15') returns 14.

SECOND(timestamp)

BIGINT

Returns the second part of the second unit from the SQL timestamp, which is an integer between 0 and 59, equivalent to EXTRACT(SECOND FROM timestamp).

For example, SECOND(TIMESTAMP '1994-09-27 13:14:15') returns 15.

FLOOR(timepoint TO timeintervalunit)

TIME

Returns the value of timepoint rounded down to the time interval unit timeintervalunit. For example, FLOOR(TIME '12:44:31' TO MINUTE) returns 12:44:00.

CEIL(timepoint TO timeintervalunit)

TIME

Return the value of timepoint rounded up to the time interval unit timeintervalunit.

For example, CEIL(TIME '12:44:31' TO MINUTE) returns 12:45:00.

(timepoint1, temporal1) OVERLAPS (timepoint2, temporal2)

BOOLEAN

Returns TRUE if the two time intervals defined by (timepoint1, temporal1) and (timepoint2, temporal2) overlap. The time value can be a time point or a time interval. For example, (TIME '2:55:00', INTERVAL '1' HOUR) OVERLAPS (TIME '3:30:00', INTERVAL '2' HOUR) returns TRUE;

(TIME '9:00:00', TIME '10:00:00') OVERLAPS (TIME '10:15:00', INTERVAL '3' HOUR) returns FALSE.

DATE_FORMAT(timestamp, string)

STRING

Converts the timestamp to a string value in the specified date format string. The format string is compatible with Java's SimpleDateFormat.

TIMESTAMPADD(timeintervalunit, interval, timepoint)

TIMESTAMP/DATE/TIME

Adds the result of combining interval with timeintervalunit to a timepoint that includes a date or datetime, and returns the resulting datetime.

For example, TIMESTAMPADD(WEEK, 1, DATE '2003-01-02') returns 2003-01-09.

TIMESTAMPDIFF(timepointunit, timepoint1, timepoint2)

INT

Returns the time interval between timepoint1 and timepoint2. The unit of the interval is given by the first parameter, which should be one of the following values: SECOND, MINUTE, HOUR, DAY, MONTH, or YEAR.

CONVERT_TZ(string1, string2, string3)

TIMESTAMP

Convert the datetime string1 (with the default ISO timestamp format 'yyyy-MM-dd HH:mm:ss') from time zone string2 to the value in time zone string3. The format of time zone should be either an abbreviation such as PST, a full name such as Country A/City A, or a custom ID such as GMT-08:00.

For example, CONVERT_TZ('1970-01-01 00:00:00', 'UTC', 'Country A/City A') returns '1969-12-31 16:00:00'.

FROM_UNIXTIME(numeric[, string])

STRING

Returns the representation of the numeric parameter numberic in the string format (default is yyyy-MM-dd HH:mm:ss). Numeric is an internal timestamp value that represents the number of seconds since '1970-01-01 00:00:00' UTC, generated by the UNIX_TIMESTAMP() function. The return value is represented in the session time zone (specified in TableConfig).

For example, if in the UTC time zone, FROM_UNIXTIME(44) returns 1970-01-01 00:00:44, and if in the Asia/Tokyo time zone, it returns 1970-01-01 09:00:44.

UNIX_TIMESTAMP()

BIGINT

Gets the current Unix timestamp in seconds. This function is non-deterministic, meaning it will be recomputed for each record.

UNIX_TIMESTAMP(string1[, string2])

BIGINT

Converts the datetime string1 in the format of string2 (default is 'yyyy-MM-dd HH:mm:ss') to a Unix timestamp in seconds, using the time zone specified in the table configuration.

TO_DATE(string1[, string2])

DATE

Converts the string1 in the format of string2 (default is yyyy-MM-dd) to a date.

TO_TIMESTAMP_LTZ(numeric, precision)

TIMESTAMP_LTZ

Converts the epoch seconds or epoch milliseconds to TIMESTAMP_LTZ, with a valid precision of 0 or 3, where 0 represents TO_TIMESTAMP_LTZ(epochSeconds, 0) and 3 represents TO_TIMESTAMP_LTZ(epochMilliseconds, 3).

TO_TIMESTAMP(string1[, string2])

TIMESTAMP

Converts the string1 in the format of string2 (default is yyyy-MM-dd HH:mm:ss) in the UTC+0 time zone to a timestamp.

CURRENT_WATERMARK(rowtime)

-

Returns the current watermark of the given time column attribute rowtime. If there is no common watermark available from upstream operations in the pipeline, the function returns NULL. The return type of the function is inferred to match the provided time column attribute, but with an adjusted precision of 3. For example, if the time column attribute is TIMESTAMP_LTZ(9), the function returns TIMESTAMP_LTZ(3).

Note that this function can return NULL, which you may need to consider. For example, if you want to filter out late data, you can use:

WHERE   CURRENT_WATERMARK(ts) IS NULL   OR ts > CURRENT_WATERMARK(ts)

DATE

DATE_ADD

DATE_SUB

TIME

TIMESTAMP

INTERVAL

CURRENT_DATE

CURRENT_TIME

CURRENT_TIMESTAMP

LOCALTIME

LOCALTIMESTAMP

EXTRACT

YEAR

QUARTER

MONTH

WEEK

DAYOFYEAR

DAYOFMONTH

DAYOFWEEK

HOUR

MINUTE

SECOND

FLOOR

CEIL

OVERLAPS

DATE_FORMAT

TIMESTAMPADD

TIMESTAMPDIFF

CONVERT_TZ

FROM_UNIXTIME

UNIX_TIMESTAMP

UNIX_TIMESTAMP(string1[, string2])

TO_DATE

TO_TIMESTAMP