APPLY_WATERMARK

Updated at:

In traditional Flink SQL, watermarks can be defined only in the CREATE TABLE DDL. Watermark configuration becomes inflexible when you do not have permission to modify the table DDL, when you need to add a watermark based on a view or a subquery, or when multiple jobs share the same source but require different watermark strategies.

To address these issues, Ververica Runtime (VVR) provides the built-in table-valued function APPLY_WATERMARK. This function defines or redefines a watermark for a table, a view, or a subquery within a query. In streaming mode, it marks the specified time column as an event time attribute (rowtime) and passes the input data through. The resulting rowtime and watermark can be used in event-time computations, such as window aggregations, window joins, event-time interval joins, and event-time temporal joins.

Limits

Only VVR 11.9 and later supports the APPLY_WATERMARK function.

Syntax

APPLY_WATERMARK(
  [input =>] TABLE <table_or_view_name> | (<subquery>),
  [rowtime =>] DESCRIPTOR(<rowtime_column>),
  [strategy =>] <watermark_strategy>
);

Parameters

Parameter

Description

Example

input

A table argument that passes in a table, a view, or a subquery.

TABLE Orders

rowtime

A column descriptor that specifies the single column to mark as the rowtime column. The column must be of the TIMESTAMP(p) or TIMESTAMP_LTZ(p) type, where p ranges from 0 to 3.

DESCRIPTOR(order_time)

strategy

A scalar expression that defines the watermark generation strategy. The result type of the expression must be TIMESTAMP(p) or TIMESTAMP_LTZ(p), where p ranges from 0 to 3.

order_time - INTERVAL '5' SECOND

Examples

Assume that the Orders table contains the order_id BIGINT and order_time TIMESTAMP(3) fields.

  1. Define a watermark for a table

The following statement marks order_time as the rowtime column and generates a watermark that lags order_time by 5 seconds:

SELECT
  order_id,
  order_time
FROM APPLY_WATERMARK(
  TABLE Orders,
  DESCRIPTOR(order_time),
  order_time - INTERVAL '5' SECOND
);
  1. Define a watermark for a subquery

The input parameter accepts a subquery. The subquery must output the rowtime column and the columns that the watermark strategy references:

SELECT
  order_id,
  order_time
FROM APPLY_WATERMARK(
  (
    SELECT
      order_id,
      order_time
    FROM Orders
    WHERE order_time IS NOT NULL
  ),
  DESCRIPTOR(order_time),
  order_time - INTERVAL '5' SECOND
);
  1. Use named parameters

The following example passes parameters by name and allows you to change the parameter order:

SELECT
  order_id,
  order_time
FROM APPLY_WATERMARK(
  strategy => order_time - INTERVAL '5' SECOND,
  rowtime => DESCRIPTOR(order_time),
  input => TABLE Orders
);  
  1. Use the result in a window aggregation

To use the result in a window table-valued function, first create a view:

CREATE TEMPORARY VIEW WatermarkedOrders AS
SELECT
  order_id,
  order_time
FROM APPLY_WATERMARK(
  TABLE Orders,
  DESCRIPTOR(order_time),
  order_time - INTERVAL '5' SECOND
);

Then run a window aggregation based on the view:

SELECT
  window_start,
  window_end,
  COUNT(*) AS order_count
FROM TABLE(
  TUMBLE(
    TABLE WatermarkedOrders,
    DESCRIPTOR(order_time),
    INTERVAL '1' MINUTE
  )
)
GROUP BY window_start, window_end;
  1. Redefine the watermark generation strategy

Assume that OrdersWithWatermark already defines a watermark that lags by 10 seconds. The following DDL shows only the table schema and the original watermark. Configure the connector options based on your actual source:

CREATE TABLE OrdersWithWatermark (
  order_id BIGINT,
  order_time TIMESTAMP(3),
  WATERMARK FOR order_time AS order_time - INTERVAL '10' SECOND
);

You can use APPLY_WATERMARK to redefine the watermark generation strategy:

SELECT
  order_id,
  order_time
FROM APPLY_WATERMARK(
  TABLE OrdersWithWatermark,
  DESCRIPTOR(order_time),
  order_time - INTERVAL '5' SECOND
);

The original watermark node upstream may still be retained, but the strategy of the outermost APPLY_WATERMARK determines the watermark timestamps emitted downstream.

  1. Cascade APPLY_WATERMARK calls

APPLY_WATERMARK supports cascading calls. The following example first defines a watermark that lags by 10 seconds, and then redefines it as a 5-second lag in the outer call:

SELECT
  order_id,
  order_time
FROM APPLY_WATERMARK(
  (
    SELECT
      order_id,
      order_time
    FROM APPLY_WATERMARK(
      TABLE Orders,
      DESCRIPTOR(order_time),
      order_time - INTERVAL '10' SECOND
    )
  ),
  DESCRIPTOR(order_time),
  order_time - INTERVAL '5' SECOND
);

In cascading calls, the strategy of the outermost APPLY_WATERMARK (the one closest to downstream) determines the watermark timestamps that this branch emits downstream.