Use a timestamp that the source connector writes into each record as the event time of
a table, and use it in time-based operations such as temporal joins.
The following example uses the order_time field, which the source
provides, to join orders with the currency rate that was valid when each order was
placed.
-
Create the table of orders, with a watermark on
order_time:
CREATE TABLE orders (
order_id STRING,
price DECIMAL(32,2),
currency STRING,
order_time TIMESTAMP(3),
WATERMARK FOR order_time AS order_time
) WITH (/* ... */);
-
Create the table of currency rates, with a watermark on
update_time:
CREATE TABLE currency_rates (
currency STRING,
conversion_rate DECIMAL(32,2),
update_time TIMESTAMP(3),
WATERMARK FOR update_time AS update_time,
PRIMARY KEY (currency) NOT ENFORCED
) WITH (/* ... */);
-
Run an event-time temporal join to add the conversion rate to each order:
SELECT order_id, price, orders.currency, conversion_rate, order_time
FROM orders
LEFT JOIN currency_rates FOR SYSTEM_TIME AS OF orders.order_time
ON orders.currency = currency_rates.currency;
Each order gets the conversion rate that was valid at its order_time. For
more information, see Creating temporal joins.