Flink SQL DML¶
Updates
Updated: 11/23: Add streaming bootstrap, update snapshot query part. Fix typos, grammar. Cleaned up inline code formatting. Added new examples 06/15/2026 Created 10/24
This chapter continues the discussion of best practices for implementing Flink SQL Data Manipulation Language (DML) queries. DML defines statements that modify data without changing metadata.
Sources of information¶
- Confluent SQL documentation for DML samples
- Apache Flink SQL
- Confluent Developer Flink tutorials
- Flink built-in system functions
Common Patterns¶
It is important to recall that a SELECT applies to a stream of records, so the results change as new records arrive. The query below shows the latest top 10 orders; when a new record arrives, the list updates.
What are the different SQL execution modes? (OSS)
Using the previous table, you can count rows with:
We get different behavior depending on the execution mode:
set 'execution.runtime-mode' = 'batch';
-- default mode
set 'execution.runtime-mode' = 'streaming';
set 'sql-client.execution.result-mode' = 'table';
In changelog mode, the SQL Client doesn't just update the count in place, but instead displays each message in the stream of updates it receives from the Flink SQL runtime:

See this lab in changelog mode and this section in the SQL concepts chapter.
Playing with Inserts¶
- Adding a NULL to a string column:
insert into sl_raw_groups values('grp_001','tenant-01','gp_n1', 'g_typ_1', CAST(NULL AS STRING), false) - Specifying columns explicitly:
insert into sl_raw_groups(group_id,tenant_id) VALUES('grp_001','tenant-01'); this populates omitted columns with NULL.
How to propagate NULL values from a source table column to a sink table?
A source table may have NULL values in the testresults column. Does Flink propagate those values?
Create a raw_tickets table (see Example in flink-sql/01-process-null):
CREATE TABLE raw_tickets (
case_id STRING,
testresults STRING,
description STRING,
priority INT,
owner STRING,
creation_ts TIMESTAMP_LTZ(3)
) WITH (
'changelog.mode' = 'append',
'value.format' = 'json-registry'
)
INSERT INTO raw_tickets (case_id, description, priority, owner, testresults, creation_ts)
VALUES
('case_001', 'Login failure on checkout', 1, 'alice', 'PASS', TO_TIMESTAMP_LTZ(1700000000, 3)),
('case_002', CAST(NULL AS STRING), 2, 'bob', CAST(NULL AS STRING), TO_TIMESTAMP_LTZ(1700001000, 3));
Now create a sink table (src_tickets):
CREATE TABLE src_tickets (
case_id STRING NOT NULL,
description STRING,
priority INT,
owner STRING,
testresults STRING,
creation_ts TIMESTAMP_LTZ(3),
PRIMARY KEY(case_id) NOT ENFORCED
) WITH (
'changelog.mode' = 'upsert',
'value.format' = 'json-registry'
)
insert into src_tickets select
case_id,
description,
priority,
owner,
testresults,
creation_ts,
`$rowtime` as first_ts
from raw_tickets;
As another example, using a Kafka Producer with Schema Registry and a SQL query, the producer sends records with missing columns, cycling RawTicket through three testresults states:
- Cycle (1-based index mod 3):
testresultspopulatedtestresultsexplicitlyNone(NULL in JSON)testresultsomitted entirely (field not present in JSON)
The query on the source topic returns:

Adding src_tickets as the sink table and running the transformation DML to read from raw_tickets and insert into src_tickets, we can see records as well.
Filtering¶
- Start with this Confluent tutorial or the Apache Flink
SELECTdocumentation. SELECT ... FROM ... WHERE ...uses column projections or filters and is stateless. Unless the output table has aretractchangelog while the input isupsert, the sink will use achangelog materializer(see section below).SELECT DISTINCTremoves duplicate rows, which requires keeping state for each distinct row.
How to filter out records?
Use the WHERE clause:
Count the number of events related to a cancelled flight (using one of the selected fields as a grouping key):
select flight_id, count(*) as cancelled_fl from flight_events where status = 'cancelled' group by flight_id;
Recall that this query produces a dynamic table.
How to use HAVING to filter after aggregation?
The HAVING clause is used to filter results after aggregation (such as after a GROUP BY). It is similar to the WHERE clause, but while WHERE filters rows before aggregation, HAVING filters rows after aggregation:
How to combine records from multiple tables (UNION)?
When two tables have the same number of columns with compatible types, combine them using UNION or UNION ALL:
See product documentation on UNION. Remember that UNION applies DISTINCT and removes duplicates, while UNION ALL keeps all rows, including duplicates.
How to filter rows whose column content does not match a regular expression?
Use REGEX
Navigate a hierarchical structure in a table
The table uses a node-and-ancestors representation. Suppose the graph represents a procedure at the highest level, then an operation, then a phase, and a phase step at level 4. The procedures table can have rows like:
id, parent_ids, depth, information
'id_1', [], 0 , 'procedure 1'
'id_2', ['id_1'], 1 , 'operation 1'
'id_3', ['id_1','id_2'], 2 , 'phase 1'
'id_4', ['id_1','id_2','id_3'], 3 , 'phase_step 1'
'id_5', ['id_1','id_2','id_3'], 3 , 'phase_step 2'
Suppose we want to extract matching procedure_id, operation_id, phase_id, and phase_step_id like:
id, procedure_id, operation_id, phase_id, phase_step_id, information
'id_1', 'id_1', NULL, NULL, NULL, 'procedure 1'
'id_2', 'id_1', 'id_2', NULL, NULL, 'operation 1'
'id_3', 'id_1', 'id_2', 'id_3', NULL, 'phase 1'
'id_4', 'id_1', 'id_2', 'id_3', 'id_4', 'phase_step 1'
'id_5', 'id_1', 'id_2', 'id_3', 'id_5', 'phase_step 2'
If the depth is 3, then the response should have all IDs populated; if 0, only the top level is returned:
with `procedures` as (
select 'id_1' as id, array[''] as parentIds, 0 as `depth`, 'procedure 1' as info
UNION ALL
select 'id_2' as id, array['id_1'] as parentIds, 1 as `depth`, 'operation 1' as info
UNION ALL
select 'id_3' as id, array['id_1','id_2'] as parentIds, 2 as `depth`, 'phase 1' as info
UNION ALL
select 'id_4' as id, array['id_1','id_2','id_3'] as parentIds, 3 as `depth`, 'phase_step 1' as info
UNION ALL
select 'id_5' as id, array['id_1','id_2','id_3'] as parentIds, 3 as `depth`, 'phase_step 2' as info
)
select
id,
parent_id,
case when `depth` = 3 then id end as phase_step_id,
case when `depth` = 2 then id end as phase_id,
case when `depth` = 1 then id end as operation_id,
case when `depth` = 0 then id end as procedure_id,
info from `procedures` cross join unnest(parentIds) as ids(parent_id);
Snapshot queries¶
Enabling snapshot queries forces Flink to process data in a topic as a bounded set, from the earliest available record to the latest offset when the query is submitted. On Confluent Cloud, when Tableflow is enabled on the topic, the query will also read historical data from the Iceberg table and then read only the latest records from the Kafka topic.
Snapshot queries are used for reporting, assessing historical data, and investigating issues on past records:

When submitting statements via the REST API, the mode is set via spec properties.
Flink automatically switches to batch mode processing. Snapshot queries use read-committed as the default isolation level, which aligns with Kafka’s exactly-once semantics (EOS).
-
The Confluent CLI supports snapshot queries directly:
-
Use a dedicated compute pool to run batch queries because Flink scales up the required resources.
-
Here is an example of using the confluent-sql Python module to perform a snapshot query programmatically:
See also confluent_sql_client.py from the Streamhouse demo.def query(self, sql: str) -> list[dict]: # error handling omitted for brevity conn = confluent_sql.connect( organization_id=s.FLINK_ORG_ID, environment_id=s.FLINK_ENV_ID, cloud_provider=s.CLOUD_PROVIDER, cloud_region=s.CLOUD_REGION, compute_pool_id=s.FLINK_COMPUTE_POOL_ID, flink_api_key=s.FLINK_API_KEY, flink_api_secret=s.FLINK_API_SECRET, database=s.FLINK_DATABASE, database_kafka_cluster_id=s.KAFKA_CLUSTER_ID, ) cur = conn.cursor() # snapshot mode is the default cur.execute(sql) columns = [d[0] for d in (cur.description or [])] return [dict(zip(columns, row)) for row in cur.fetchall()] -
Snapshot queries guarantee exactly-once delivery when writing results to a sink. This can be configured to use
at-least-oncedelivery instead:
Confluent Streaming bootstrap¶
Streaming Bootstrap lets a streaming query build its state from historical data before it starts processing live data.
Flink has the HybridSource capability to sequentially read input from heterogeneous sources to produce a single input stream. A typical use case is to read days of data from an S3 bucket and then continue to consume streaming data from Kafka. The historical records source needs to be bounded.
Confluent Cloud supports Streaming Bootstrap via Tableflow when enabled on a source topic. Flink reads historical records from the Tableflow/Iceberg table and then switches to reading recent records directly from Kafka. When Tableflow is not enabled, it reads directly from the Kafka topic.
Once the historical batch catch-up finishes, the query automatically shifts to continuous stream processing mode without duplicating state, as it starts streaming from the state that the catch-up phase produced:
Deduplication¶
Deduplication occurs automatically on an upsert table with a primary key: the latest record per $rowtime or other timestamp is kept. When the source table is in append mode, the approach is to use ROW_NUMBER() combined with OVER():
CREATE TABLE unique_clicks
AS SELECT ip_address, url, TO_TIMESTAMP(FROM_UNIXTIME(click_ts_raw)) as click_timestamp
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY ip_address ORDER BY TO_TIMESTAMP(FROM_UNIXTIME(click_ts_raw)) ASC) as rownum
FROM clicks
)
WHERE rownum = 1;
- The query is designed to identify and persist only the earliest record. Once the record is written to the deduplicated table, any subsequent events pertaining to the same
ip_addressare effectively discarded by theWHERE rownum = 1filter. ROW_NUMBER()assigns a unique, sequential number to each row. It is part of the Top-N query pattern.- Use
OVERaggregation to compute aggregate values for each input row under a window specification and a filter condition to express a Top-N query. Combined withPARTITION BY, Flink supports per-group Top-N. TheORDER BYdoes not need to be a timestamp in the context of deduplication. - The inner query adds a
rownumfor each row in each partition. - The subsequent
WHERE rownum = 1clause filters the results to retain only the very first event observed for each uniqueip_addressbased on its timestamp. - The created table is an append table. There is no mechanism within this query to generate update or delete operations for records that have already been processed. Even if the underlying
clickstable has a primary key defined, the transformation applied here dictates that theunique_clickstable will only ever grow by appending new, uniqueip_addressentries. - If the sort order is
DESC, a retraction or update may be emitted when a new record arrives. - After deduplication, the upsert sink primary key should align with the partition keys used in the deduplication logic.
See this example in the Confluent product documentation: select * from dedup_table; returns eight messages with no duplicates, and the Kafka topic also contains eight messages.
That walkthrough does not show that the last message per key is kept. The deduplication sample shows how an upsert table removes duplicates and retains the last record per key.
- Confluent Cloud has limitations:
ORDER BYin this pattern may be restricted to a timestamp column in ascending order. -
Example of deduplication on OSS Flink where
ORDER BYcan use columns of other types (where supported): -
Developers should not assume that deduplication in a CTE prevents downstream operations from introducing duplicates. For example, a
CROSS JOIN UNNEST(or any fan-out) multiplies rows. If the fan-out logic or join keys are not unique, multiple identical records per primary key can be created. UsingGROUP BYon the same key may help, but it may also be necessary to add a second deduplication step immediately before writing to the final sink on the target primary key. As a best practice: deduplicate early to simplify upstream processing, and deduplicate again after any fan-out,UNNEST, or joins that can reintroduce duplicate keys before writing to the final table.
The Frankenstein rows
The dimension deduplication pattern above may produce "Frankenstein rows" — output rows whose column values are silently blended from different source rows, violating row integrity. See this dedicated lab to assess the type of queries that may generate those rows
Transformation¶
Some important resources:
- Transform a Topic with Confluent Cloud for Apache Flink (product doc) covers topic transformation from the Data Portal: Actions → Transform Topic, which creates a CTAS Flink SQL statement and deploys it to the configured compute pool. This is efficient for Avro-to-JSON conversion, field renames, and primary-key changes.
How to transform an epoch timestamp field to a TIMESTAMP?
Where the epoch value is a BIGINT:
How to change a date string to a timestamp?
See all date and time functions.
Use the Kafka timestamp as a fallback for NULL timestamps
If created_date is NULL, fall back to $rowtime:
How to compare a date field with current system time?
If the target table uses 'changelog.mode' = 'upsert', using now() is problematic because the execution time is not deterministic per row. The statement above is appropriate mainly for append mode.
How to extract the number of days between a date field and now?
To calculate the difference between date/timestamp types, first cast the DATE column to a timestamp, then use CURRENT_DATE and the DAY unit. See supported units (SECOND, MINUTE, HOUR, DAY, MONTH, or YEAR):
How to access elements of an array of rows?
Suppose the table has a column that is an array of rows:
To create one record per row within the array (exploding the array), use CROSS JOIN UNNEST:
SELECT
t.key_col,
unnested_row.id,
unnested_row.name
FROM
my_table AS t
CROSS JOIN UNNEST(t.nested_data) AS unnested_row;
Each element in the nested_data array becomes a row in the output table alongside the matching key_col.
How to use conditional functions?
Flink has built-in conditional functions (see also Confluent conditional functions), especially CASE WHEN:
How to mask a field?
Create a new table from the existing one, and then use REGEXP_REPLACE to mask an existing attribute:
JSON Transformation¶
Flink provides built-in JSON functions to extract information from JSON strings. '$' denotes the root node in a JSON path.
Paths can access properties ($.a), array elements ($.a[0].b), or branch over all elements in an array ($.a[*].b).
There are two main groups of functions for extracting values from a JSON string:
- Return a scalar value with
JSON_VALUE: -
Return a string or array of strings with
JSON_QUERY(laxsuppresses errors and returns NULL or empty results if the path does not match the JSON schema): -
Build a
JSON_OBJECTstring from a list of key-value pairs:Suppose
datais aROW<version DOUBLE, transactionId VARCHAR(2147483647)>. To create a JSON object string such as'{"version": 1.1}':
How to access JSON data from a string column that contains a JSON object?
Use the JSON_QUERY function in the SELECT clause:
Use JSON_VALUE() when extracting a scalar value from JSON object content:
How to transform a JSON array column into an array and generate N rows?
Extract an array from a JSON string:
To create as many rows as there are elements in the nested array:
SELECT existing_column, anewcolumn from table_name
cross join unnest (JSON_QUERY(`data`, '$' RETURNING ARRAY<STRING>)) as t(anewcolumn);
UNNEST returns a new row for each element in the array. See multiset expansion documentation.
Extract content from a JSON array as strings and explode to multiple rows?
Suppose the input record has a parameters column with an array of objects (key-value fields), like:
[{"parameter_name": "Pressure", "parameter_value": 10.0, "parameter_type": "float", "parameter_tolerance": 1.0}, {"parameter_name": "FlowRate", "parameter_value": 2.5, "parameter_type": "float", "parameter_tolerance": 0.5}, {"parameter_name": "MotorSpeed", "parameter_value": 3200.0, "parameter_type": "float", "parameter_tolerance": 150.0}]
CROSS JOIN UNNEST on the array. Then extract each JSON object field. Guard with conditions on the parameters string so JSON_QUERY receives valid input: SELECT
prescription_id,
patient_id,
device_id,
medication_or_therapy,
JSON_VALUE(param_obj, 'lax $.parameter_name') AS metric_name,
CAST(JSON_VALUE(param_obj, 'lax $.parameter_value' RETURNING DOUBLE) AS DOUBLE) AS target_value,
CAST(JSON_VALUE(param_obj, 'lax $.parameter_tolerance' RETURNING DOUBLE) AS DOUBLE) AS tolerance_range,
start_date,
end_date
FROM `healthcare.public.prescriptions`
CROSS JOIN UNNEST(
JSON_QUERY(parameters, 'lax $[*]' RETURNING ARRAY<STRING>)
) AS t(param_obj)
WHERE parameters IS NOT NULL AND TRIM(parameters) <> '' AND TRIM(parameters) <> '[]';
How to implement the equivalent of SQL EXPLODE?
In classical SQL, EXPLODE creates a row for each element in the array or map and ignores null or empty entries in the array:
SQL also defines EXPLODE_OUTER, which returns all values in the array, including null or empty entries.
To translate this to Flink SQL, you can use MAP_ENTRIES and MAP_FROM_ARRAYS. MAP_ENTRIES returns an array of all entries in the given map. MAP_FROM_ARRAYS returns a map created from parallel arrays of keys and values.
As presented in the previous question, CROSS JOIN UNNEST(array) is the recommended and standard solution in Flink SQL.
Statement Set¶
Bundling statements into a single statement set reduces repeated reads from the source for each INSERT: one read from the source is executed and shared across downstream inserts.
Do not use a statement set when the sources differ for each statement inside the set. If one statement in the set fails, all queries in the set fail. State is shared across statements in the set, so one stateful query can affect the others.
How to route late messages to a business DLQ using a statement set?
First, create a DLQ table such as late_orders based on the orders table:
Group the main stream processing and late-arrival handling in one statement set:
EXECUTE STATEMENT SET
BEGIN
INSERT INTO late_orders SELECT * FROM orders WHERE `$rowtime` < CURRENT_WATERMARK(`$rowtime`);
INSERT INTO order_counts -- the sink table
SELECT window_time, COUNT(*) as cnt
FROM TABLE(TUMBLE(TABLE orders DESCRIPTOR(`$rowtime`), INTERVAL '1' MINUTE))
GROUP BY window_start, window_end, window_time;
END;
Stateful aggregations¶
An aggregate function computes a single result from multiple input rows.
GROUP BY¶
This provides classical SQL grouping of records, but in streaming the state may grow without bound. The state size depends on the number of groups and how much data must be retained per group. GROUP BY generates upsert events as it manages key-value state and repartitions data:
EXPLAIN
SELECT
account_number,
transaction_type,
SUM(amount)
FROM `transactions`
where transaction_type = 'withdrawal'
GROUP BY account_number, transaction_type
HAVING SUM(amount) > 5000;
The physical plan looks like:
== Physical Plan ==
StreamSink [6]
+- StreamCalc [5]
+- StreamGroupAggregate [4]
+- StreamExchange [3]
+- StreamCalc [2]
+- StreamTableSourceScan [1]
== Physical Details ==
[1] StreamTableSourceScan
Table: `j9r-env`.`j9r-kafka`.`transactions`
Primary key: (txn_id)
Changelog mode: append
Upsert key: (txn_id)
State size: low
Startup mode: earliest-offset
Key format: avro-registry
Key registry schemas: (:.:transactions/100220)
Value format: avro-registry
Value registry schemas: (:.:transactions/100219)
[4] StreamGroupAggregate
Changelog mode: retract
Upsert key: (account_number,transaction_type)
State size: medium
State TTL: never
[5] StreamCalc
Changelog mode: retract
Upsert key: (account_number,transaction_type)
[6] StreamSink
Table: Foreground
Changelog mode: retract
Upsert key: (account_number,transaction_type)
State size: low
DISTINCT¶
Remove duplicates before aggregating:
ARRAY_AGG¶
Array aggregation combines multiple data elements into a single array. It is widely used to regroup data points for downstream processing.
This Flink ARRAY_AGG aggregate function is common in record transformations. It returns an array whose elements come from multiple input rows.
Start with simple array indexing (indexes are 1-based through n). Below, VALUES creates in-memory test data in table alias T with column array_field:
The following SQL creates a view with an array aggregate: it collects URLs per user_id over one-minute tumbling windows.
CREATE VIEW visited_pages_per_minute AS
SELECT
window_time,
user_id,
ARRAY_AGG(url) AS urls
FROM TABLE(TUMBLE(TABLE `examples.marketplace.clicks`, DESCRIPTOR(`$rowtime`), INTERVAL '1' MINUTE))
GROUP BY window_start, window_end, window_time, user_id;
-- once the view is created
SELECT * from visited_pages_per_minute;
-- it is possible to expand an array into multiple rows using cross join unnest
SELECT v.window_time, v.user_id, u.url FROM visited_pages_per_minute AS v
CROSS JOIN UNNEST(v.urls) AS u(url)
Important: new clicks for the same user_id with a new URL produce a new output row with the updated aggregated array. See the cc-array-agg study.
To optimize processing, deduplicate and/or filter before aggregating; using a Flink view is a typical pattern:
create view suites_versioned as
select suite_id, suite_name, asset_id, asset_name, asset_price_min, asset_price_max, ts_ltz
from (
select *,
ROW_NUMBER() OVER (PARTITION BY suite_id, asset_id ORDER BY ts_ltz DESC) as rn
from suites
) where rn = 1;
Then perform the array aggregation:
select
suite_id,
ARRAY_AGG(ROW(asset_id, asset_name, ROW(asset_price_min, asset_price_max))) as asset_data,
max(ts_ltz) as ts_ltz
from suites_versioned group by suite_id;
As in the example above, the array element type can be a row containing nested rows. See SQL examples.
Flink Views
Views in Flink are not materialized. They are logical constructs that represent saved queries and do not store or maintain state themselves; they act like an alias for a query. In ksqlDB, a materialized view is a precomputed, incrementally updated query result stored for fast access and direct querying. In contrast, Flink SQL views are executed when referenced in queries.
OVER¶
OVER aggregations compute an aggregated value for every input row over a range of ordered rows. They do not reduce the row count the way GROUP BY does; they emit one result per input row.
OVER specifies the time or row window over which the aggregation is performed. A classic example is computing a moving sum or average, such as the number of orders in the last 10 seconds:
SELECT
order_id,
customer_id,
`$rowtime`,
SUM(price) OVER w AS total_price_ten_secs,
COUNT(*) OVER w AS total_orders_ten_secs
FROM `examples`.`marketplace`.`orders`
WINDOW w AS (
PARTITION BY customer_id
ORDER BY `$rowtime`
RANGE BETWEEN INTERVAL '10' SECONDS PRECEDING AND CURRENT ROW
);
The window definition is externalized and can be shared across multiple aggregates, like the SUM() and COUNT() examples above.
The source topic should be in append mode because over-window aggregation does not support retraction/update semantics. OVER is useful when each input row must be evaluated against a time or row interval.
LAG and OVER
To detect when metrics exceed limits for the first time or when aggregates fall below thresholds, use LAG:
-
Compute the total price and order count over a 10-second period for each customer:
WITH orders_ten_secs AS ( SELECT order_id, customer_id, `$rowtime`, SUM(price) OVER w AS total_price_ten_secs, COUNT(*) OVER w AS total_orders_ten_secs FROM `examples`.`marketplace`.`orders` WINDOW w AS ( PARTITION BY customer_id ORDER BY `$rowtime` RANGE BETWEEN INTERVAL '10' SECONDS PRECEDING AND CURRENT ROW ) ), -
Get previous and current order aggregates per customer using
LAG: -
Filter orders when the order price or order count crossed thresholds between previous and current window evaluations:
SELECT customer_id, 'BLOCK' AS action, `$rowtime` AS updated_at FROM orders_ten_secs_with_lag WHERE (total_price_ten_secs > 300 AND total_price_ten_secs_lag <= 300) OR (total_orders_ten_secs > 5 AND total_orders_ten_secs_lag <= 5) UNION ALL SELECT customer_id, 'UNBLOCK' AS action, `$rowtime` AS updated_at FROM orders_ten_secs_with_lag WHERE (total_price_ten_secs <= 300 AND total_price_ten_secs_lag > 300) OR (total_orders_ten_secs <= 5 AND total_orders_ten_secs_lag > 5);
How to perform aggregation over the last N preceding elements?
The OVER window ends at the current row and stretches backward through the stream for a bounded interval, either in time or by row count. For example, counting the number of flight_events of the same key over the last 100 events:
select
flight_id,
evt_type,
count(evt_type) OVER w as number_evt
from flight_events
window w as (partition by flight_id order by `$rowtime` rows between 100 preceding and current row);
Results are updated for every input row. Partitioning is by flight_id, and ORDER BY $rowtime is required.
When and how to use a custom watermark?
Developers should define a custom watermark strategy when there are few records per topic or partition, a large watermark delay is required, or a non-default event-time column is used. The default uses SOURCE_WATERMARK(), a watermark supplied by the source. A common explicit choice is maximum-out-of-orderness so late-arriving events can still fall in the correct window, trading latency for accuracy. Example:
The minimum out-of-orderness is 50ms and can be set up to 7 days. See Confluent documentation.
Late Events¶
Late events are relevant only when you have event-time-based operators, such as window aggregations. These operators drop late events silently in Flink SQL, whereas in the DataStream API developers can handle them using side outputs. Remember that when a late event arrives and is not dropped, it can trigger another window firing.
When statements do not include any event-time operations, late events are not an issue and no records are dropped.
The task/operator metric numLateRecordsDropped counts the records dropped for being late by these operators.
Confluent Cloud for Flink provides a setting, 'late-handling.mode' = 'filter', to drop or filter out events that fall behind the defined watermark. Two modes are supported: pass-through and filter. With pass-through, Flink operators apply their own semantics to manage late events, which may lead to drops.
filter allows developers to filter late events immediately at the source operator level and handle them explicitly regardless of downstream operator types. Events with timestamp <= watermark are sent to this side output.
Filtered events remain in a system table (<table_name>$late) for inspection or reprocessing (a union of non-late and late records).
Retention of late events in the $late table is the same as the source table.
Joins¶
When you run a join in a database, the result reflects the join state at execution time. In streaming, as both sides of a join receive new rows, both sides keep evolving. This is a continuous query on dynamic tables, and the engine must retain substantial state—effectively rows from each input.
Below is a common join between two tables:
SELECT t.amount, t.order_type, s.name, s.opening_value FROM transactions t
LEFT JOIN stocks s
ON t.stockid = s.id;
On the left, the fact table changes at high velocity; events are typically append-only or immutable. On the right, the dimension table receives new records more slowly.
When doing a join, Flink needs to fully materialize both the right and left sides of the join tables in state, which can require significant memory. If a row in the left-hand table (LHT, also named the probe side) is updated, the operator must emit updated matches for all matching rows in the right-hand table (RHT, or build side). The cardinality of the right side is typically bounded at a given point in time, but the left side may vary significantly. The join operator emits matching rows to downstream operators.
The key points to keep in mind are:
- Regular joins typically produce a full cross-product of all matching records. However, in streaming scenarios, this behavior is often undesirable—for example, when enriching an event with reference data—because retaining full state in distributed memory can become excessively large.
- When the RHS is an upsert table, the join output is often upsert-shaped as well: results may be re-emitted when the RHS changes. To pin dimension values to event time, use a temporal join so the RHS is evaluated as of the LHS event time (time-versioned enrichment).
- See basic joins examples.
- Flink must handle scenarios where left-side events do not have corresponding right-side events at the time of processing. Upsert tables allow Flink to store unmatched left records and update them when matching right records arrive later, ensuring all left records are included in the result and supporting
LEFT JOINsemantics effectively in a streaming context. -
To avoid unbounded state growth, define a State Time-to-Live (TTL) hint (
/*+ STATE_TTL(o='2h', p='30d') */) on both tables: -
Join order matters: prefer joining the slowest-changing dimension first when possible.
- A cross join without selective predicates can explode cardinality or hit resource limits.
- Watermarks set on both tables do not impact regular join results. Watermarks matter only for time-based joins, not regular equi-joins. Simple
JOIN/LEFT JOINqueries without event-time conditions do not use watermarks to decide when to emit results. - For interval joins and temporal (time-versioned) joins, watermarks directly affect output timing and completeness. Flink uses watermarks to know when enough event time has passed to safely produce results; without watermarks, these operators do not produce output.
- For temporal joins, the join may wait until the watermark on the versioned/enrichment side reaches the probe record’s timestamp before emitting, which introduces latency.
- If one side is idle or lagging, the join can appear stalled because two-input operators generally wait on the relevant watermarks from both sides.
- For interval joins, watermarks are propagated with an additional delay equal to the maximum join interval bound, so larger time ranges increase buffering and downstream latency.
Temporal Join¶
A temporal join joins one table with another table that is updated over time. This join is made possible by linking both tables using a time attribute, which allows the join to evaluate historical changes in the table.
insert into enriched_transactions
SELECT t.amount, t.order_type, s.name, s.opening_value FROM transactions t
LEFT JOIN stocks s FOR SYSTEM_TIME AS OF t.purchase_ts
ON t.stockid = s.id;
- Temporal joins reduce state because only time-relevant versions of the RHS are needed. Progress is tied to watermarks. If
opening_valuechanges over time, earlier enriched rows are not retroactively updated. - When the event time comes from the LHS, it needs to have a watermark strategy defined; otherwise, it is not considered a valid time attribute. By default on Confluent Cloud,
$rowtimecan be used. To ensure completeness, the join operator only joins and emits data when the watermark advances. The join will not emit data if one input stops sending new watermarks because no new data is being received. This can cause significant latency. Latency is also directly affected by the watermark strategy: for example, if the watermark strategy ists - INTERVAL '1' MINUTE, there will be at least 1 minute of latency. Settingscan.idle-timeoutto x seconds adds latency because it takes at least x seconds before an idle partition is recognized. -
Temporal
JOINs must include allPRIMARY KEYcolumns of the versioned (right-side) table: theONconditions must match the exact primary key columns:create table dim_rule_config ( tenant_id STRING NOT NULL, rule_id BIGINT NOT NULL, rule_name STRING NOT NULL, parameter_id BIGINT NOT NULL, parameter_value BIGINT, primary key (tenant_id, rule_id, parameter_id) not enforced ); -- Joining in a separate DML statement: ... from extended_sensors s left join dim_rule_config for system_time as of s.event_ts as rule ON s.tenant_id = rule.tenant_id AND s.rule_id = rule.rule_id AND s.parameter_id = rule.parameter_id;extended_sensorsis a CTE that adds constant columns so theONconditions can reference columns on the LHS. To look up a specific rule and parameter, define those constants in the CTE:with extended_sensors as ( select -- sensor attributes 10 as rule_id, 1 as parameter_id from sensors ) select ...;See also this sample for rule-based control with temporal joins and constant columns, and the price value at exchange rate time study.
-
When the LHS of a temporal join is in upsert mode, the sink often requires retract changelog mode; with an append LHS, an append sink is typical.
- An inner join with only equality predicates is not a full Cartesian product; unconstrained joins can behave like one.
- Outer joins (left, right, full) may emit rows with nulls for non-matching sides.
- In upsert mode, a Kafka tombstone (
<key, value>pair with a NULL value) is interpreted as a DELETE (-D) for that key in the table. The temporal join semantics apply the following logic:- For each new row on A: look up the matching version in B "as of" that event time and emit a joined row.
- Deletes/retractions from A are treated as "remove previously emitted results", not as new join inputs.
- Tombstones from A become -D (retractions) for the corresponding key in A's logical table.
- The temporal join does not treat a tombstone as a new row to be joined, so there is no new output row for the tombstone itself.
Interval Join¶
-
Interval joins are particularly useful when working with unbounded data streams. Here is an example for orders and payments:
CREATE TABLE valid_orders ( order_id STRING, customer_id INT, product_id STRING, order_time TIMESTAMP_LTZ(3), payment_time TIMESTAMP_LTZ(3), amount DECIMAL, WATERMARK FOR order_time AS order_time - INTERVAL '5' SECOND ) AS SELECT unique_orders.order_id, customer_id, product_id, unique_orders.`$rowtime` AS order_time, payment_time, amount FROM unique_orders INNER JOIN payments ON unique_orders.order_id = payments.order_id WHERE unique_orders.`$rowtime` BETWEEN payment_time - INTERVAL '10' MINUTES AND payment_time; -
An
INTERVAL JOINrequires at least one equi-join predicate and a join condition that bounds the time on both sides:
Lateral Joins¶
The LATERAL TABLE clause is used to invoke a Table-Valued Function (TVF) or a User-Defined Table Function (UDTF) for every row of a base (outer) table. The Lateral Join evaluates a subquery or function for each row of the first table, and the result is joined back to the original row. The UDTF/TVF on the right side of the join can reference columns from the table on the left side:
SELECT
t.*,
tf.*
FROM
input_table AS t,
LATERAL TABLE(udtf_function(t.column_a, t.column_b)) AS tf(output_column_1, output_column_2);
You can combine LATERAL TABLE with different join types to control which rows are preserved:
| Joins | Behavior |
|---|---|
CROSS JOIN LATERAL TABLE(...) | If the UDTF returns zero rows for a given input row, the original row from the outer table is discarded. |
LEFT JOIN LATERAL | Returns all rows from the outer table, even if the UDTF produces zero rows. |
LATERAL TABLE lets you transform stream records row by row, which is awkward with standard joins alone.
Multi-way joins¶
See Confluent Flink SQL documentation for multi-way joins and Flink state optimization. Add hints such as:
SELECT /*+ MULTI_JOIN(o, c, a) */
* FROM orders o
JOIN customers c ON o.customer_id = c.id
JOIN addresses a ON c.id = a.customer_id;
Because state can grow rapidly when many-to-many joined rows are created, it is important to reduce the number of records on each side of the joins toward one row per join key. This reduction can be achieved by deduplicating to the latest record or by applying domain-specific business filtering.
Heartbeat pattern¶
Flink SQL streaming queries depend upon a continuous flow of new events to advance event time so that event-time-based operations can emit results. With event-time-based operations like temporal joins, interval joins, window TVFs (TUMBLE, HOP, CUMULATE, SESSION), and OVER aggregations, sparse input streams can prevent results from being emitted. To avoid this problem, dummy events (heartbeats) can be injected into the slow stream to advance the watermark.
The pattern is best implemented with a separate topic to avoid requiring other consumers to filter out heartbeat records. The Flink statement uses UNION ALL to combine the heartbeat topic with the slow stream, and then filters out heartbeat records before producing downstream results:
-
Create the topic via SQL:
-
Create a CTE to union records from both tables:
- Implement the windowing or other temporal operations on the CTE, and filter out the heartbeat records:
References¶
Here is a list of important tutorials on joins:
- Confluent Cloud: joins documentation explains how joins behave in Flink SQL.
- Confluent Developer: How to join streams. Related exercises live in the flink-sql/04-joins folder for Confluent Cloud or Confluent Platform for Flink.
- Confluent temporal join documentation
- Window Join Queries in Confluent Cloud for Apache Flink
- Temporal Join Study in this repo
FAQs¶
Deep dive on temporal joins
Event-time temporal joins align two or more tables on a common event time (from a business timestamp column or the Kafka record timestamp in $rowtime). With event time, the operator can read a key as it was at a past instant. The versioned RHS retains rows keyed by time up to the watermark horizon.
Temporal Flink SQL looks like:
SELECT [column_list]
FROM table1 [AS <alias1>]
[LEFT] JOIN table2 FOR SYSTEM_TIME AS OF table1.{ rowtime } [AS <alias2>]
ON table1.column-name1 = table2.column-name1;
When enriching table1, an event-time temporal join waits until the watermark on table2 reaches that table1 row's timestamp, so the join uses a complete snapshot of relevant table2 rows as of that time. table2 rows may be older if that side's watermark lags.
How to join two tables on a key within a time window using an event column as timestamp and store results in a target table?
Full example:
-- Use separate statements to create the tables
create table Transactions (ts TIMESTAMP(3), tid BIGINT, amount INT);
create table Payments (ts TIMESTAMP(3), tid BIGINT, type STRING);
create table Matched (tid BIGINT, amount INT, type STRING);
execute statement set
begin
insert into Transactions values(now(), 10, 20), (now(), 11, 25), (now(), 12, 34);
insert into Payments values(now(), 10, 'debit'), (now(), 11, 'debit'), (now(), 12, 'credit');
insert into Matched
select T.tid, T.amount, P.type
from Transactions T join Payments P ON T.tid = P.tid
where P.ts between T.ts and T.ts + interval '1' minute;
end;
How does primary key selection impact joins?
Primary keys on source tables do not constrain which columns you join on. What matters for sinks is that the sink primary key aligns with the effective key produced by the final join shape.
Understand left-side velocity and update strategy
If the left table has high event velocity and keys do not repeat often, setting a short TTL on the left side keeps state size under control. Use table aliases to configure retention times:
select /*+ STATE_TTL('tx'='120s', 'c'='4d') */
tx_id, account_id, amount, merchant_id, tx_type
from transaction tx
LEFT JOIN account c
on tx.account_id = c.id;
Remark: Running EXPLAIN on the statement above shows the configured TTL.
Join on 1x1 relationship
In Flink SQL, it is currently not possible to efficiently join elements from two tables when the relationship is known to be 1-to-1 (e.g., one transaction to one account, or one shipment to one order) and immediately clear the state upon matching. While standard SQL retains state for subsequent potential matches, immediate state cleanup upon matching can be implemented using the DataStream API.
Windowing / Table Value Functions¶
Windowing Table-Valued Functions cover Tumbling, Hopping, Cumulating, and Session windows. Windows partition the stream into finite buckets for aggregation and other logic. TVFs append window_start, window_end, and window_time columns to each windowed row.
- The
TUMBLEfunction assigns each element to a window of specified window size. Tumbling windows have a fixed size and do not overlap.
How to count the number of different product types per 10 minutes (TUMBLE window)?
Aggregate a stream in a tumbling window. The following query counts distinct product types from the event stream in 10-minute tumbling windows:
SELECT window_start, product_type, count(product_type) as num_ptype
FROM TABLE(
TUMBLE(
TABLE events,
DESCRIPTOR(`$rowtime`),
INTERVAL '10' MINUTES
)
)
GROUP BY window_start, window_end, product_type;
DESCRIPTOR selects the time attribute for windowing (here, Kafka record ingestion time as $rowtime). When the window closes, results are emitted. That bounds how much window state Flink retains—here, roughly by the number of distinct product_type values per window.
You can also use a custom timestamp column from the input table (e.g., transaction_ts TIMESTAMP(3)). In that case, declare a watermark on that column:
DESCRIPTOR(transaction_ts) to be used in the window descriptor function: How to count elements in 10-minute intervals advancing every 5 minutes (HOP window)?
Confluent documentation on window TVFs. For HOP windows, the slide interval controls how often a new hopping window starts:
How to compute accumulated price over a period (CUMULATE window)?
Use a cumulate window: it grows toward a maximum window size and emits partial results at each step. This image summarizes the behavior: 
SinkUpsertMaterializer¶
When operating in upsert mode and processing two update events, a potential issue arises. If input operators for two tables in upsert mode are followed by a join and then a sink operator, update events might arrive at the sink out of order. If the downstream operator's implementation doesn't account for this out-of-order delivery, it can lead to incorrect results.
Flink typically determines the ordering of update history based on the primary key (or upsert keys) through a global analysis in the Flink planner. However, a mismatch can occur between the upsert keys of the join output and the primary key of the sink table. The SinkUpsertMaterializer operator addresses this mapping discrepancy.
This operator maintains a complete list of RowData in its state to correctly process deletion events originating from the source table. However, this approach can lead to significant state size, resulting in increased state access I/O overhead and reduced job throughput. Additionally, the output value for each primary key is always the last (tail) element in the maintained list. It is generally advisable to avoid using SinkUpsertMaterializer whenever possible.
Consider a scenario where 1 million records need to be processed across a small set of 1,000 keys. In this case, SinkUpsertMaterializer would need to store a potentially long list, averaging approximately 1,000 records per key.
To minimize or avoid using SinkUpsertMaterializer:
- Ensure that the partition keys used for deduplication, group aggregation, etc., are identical to the sink table's primary keys.
SinkUpsertMaterializeris unnecessary if retractions already use the same key as the sink primary key. When most rows are soon retracted, the materializer retains far less state.- Utilize Time-To-Live (TTL) to limit state size over time.
- A higher number of distinct values per primary key directly increases the state size of
SinkUpsertMaterializer.
Row pattern recognition¶
How to find the longest period where a stock average price stayed within a threshold (MATCH_RECOGNIZE)?
Create a Datagen connector table to publish StockTicker records to a Kafka topic. See product documentation on CEP patterns with SQL:
create table StockTicker (symbol string, price int, tax int) with ('connector' = 'kafka', ...);
SELECT * FROM StockTicker
MATCH_RECOGNIZE (
partition by symbol
order by rowtime
measures
FIRST(A.rowtime) as start_tstamp,
LAST(A.rowtime) as last_tstamp,
AVG(A.price) as avgPrice
ONE ROW PER MATCH
AFTER MATCH SKIP PAST LAST ROW
PATTERN (A+ B)
DEFINE
A as AVG(A.price) < 15
);
MATCH_RECOGNIZE partitions and orders the stream using the PARTITION BY and ORDER BY clauses, and defines patterns of rows to search for using the PATTERN clause. The logical conditions of the row pattern variables are specified in the DEFINE clause. B is defined implicitly as any event not matching A.
Confluent Cloud Specifics¶
See Flink Confluent Cloud queries documentation.
Each topic maps to a table with metadata columns such as $rowtime, aligned to the Kafka record timestamp. To inspect columns and watermarks, run DESCRIBE EXTENDED table_name;. With watermarking, arriving events are processed roughly in order with respect to $rowtime.
How to map between Kafka record timestamps and table $rowtime?
The Kafka record timestamp maps to $rowtime, a read-only field. Use it to order rows by ingestion time:
How to run Confluent Cloud for Flink?
See the Confluent Cloud Flink notes. In summary: 1) create a stream processing compute pool in the same environment and region as the Kafka cluster, and 2) use the Cloud Console or CLI (confluent flink shell) to run queries.

How to run Confluent Cloud Kafka with local Flink?
The goal is to show how to use a Confluent Cloud cluster and send messages via Flink Faker from a local table into a Kafka topic:

See the scripts and README.
Here is an example Faker definition for Confluent Cloud:
CREATE TABLE `rides` (
`ride_id` STRING,
`driver_id` STRING,
`pickup_location` STRING,
`dropoff_location` STRING,
`pickup_time` TIMESTAMP(3),
`dropoff_time` TIMESTAMP(3),
`distance` DOUBLE,
`fare` DOUBLE,
`payment_type` STRING,
`rating` DOUBLE
) WITH (
'connector' = 'faker',
'rows-per-second' = '4',
'fields.ride_id.expression' = '#{Internet.UUID}',
'fields.driver_id.expression' = '#{numerify ''driver_##''}',
'fields.pickup_location.expression' = '#{Address.city}',
'fields.dropoff_location.expression' = '#{Address.city}',
'fields.pickup_time.expression' = '#{date.past ''5'',''1'',''SECONDS''}',
'fields.dropoff_time.expression' = '#{date.past ''5'',''1'',''SECONDS''}',
'fields.distance.expression' = '#{number.numberBetween ''10'',''100''}',
'fields.fare.expression' = '#{number.numberBetween ''10'',''130''}',
'fields.payment_type.expression' = '#{Options.option ''credit_card'', ''debit_card'', ''cash''}',
'fields.rating.expression' = '#{number.numberBetween ''1'',''5''}'
);
How to read from a topic at specific offsets?
How to create a long-running SQL job with the CLI?
Get or create a service account:
Analyzing Statements¶
How to inspect running Flink statements in Confluent Cloud?
To see which jobs are running, failed, or stopped, use the Confluent Cloud UI (Flink workspace) or the confluent CLI:
Understand the physical execution plan for a SQL query¶
See the EXPLAIN statement or Confluent Flink documentation for how to read the plan:
Indentation indicates data flow, with each operator passing results to its parent.
Review the state size, changelog mode, and upsert keys. Operators transition between changelog modes when different update patterns are needed, such as moving from streaming reads to aggregations.
Pay special attention to data skew when designing your queries. If a particular key value appears much more frequently than others, it can lead to uneven processing where a single parallel instance becomes overwhelmed handling that key’s data. Consider strategies like adding additional dimensions to your keys or pre-aggregating hot keys to distribute the workload more evenly. Whenever possible, configure the primary key to be identical to the upsert key.
Troubleshooting SQL statement running slow¶
How to detect hot keys?
A more advanced statistical query for hot key detection:
WITH key_stats AS (
SELECT
id,
tenant_id,
count(*) as record_count
FROM src_aqem_tag_tag
GROUP BY id, tenant_id
),
distribution_stats AS (
SELECT
AVG(record_count) as mean_count,
STDDEV(record_count) as stddev_count,
PERCENTILE_APPROX(record_count, 0.75) as q3,
PERCENTILE_APPROX(record_count, 0.95) as p95,
PERCENTILE_APPROX(record_count, 0.99) as p99
FROM key_stats
)
SELECT
ks.*,
ds.mean_count,
ds.stddev_count,
-- Z-score calculation for outlier detection
CASE
WHEN ds.stddev_count > 0
THEN (ks.record_count - ds.mean_count) / ds.stddev_count
ELSE 0
END as z_score,
-- Hot key classification
CASE
WHEN ks.record_count > ds.p99 THEN 'EXTREME_HOT'
WHEN ks.record_count > ds.p95 THEN 'VERY_HOT'
WHEN ks.record_count > ds.q3 * 1.5 THEN 'HOT'
ELSE 'NORMAL'
END as hot_key_category
FROM key_stats ks
CROSS JOIN distribution_stats ds
WHERE ks.record_count > ds.mean_count;
Confluent Flink Query Profiler¶
This is a Confluent-specific view of Flink performance, related to the Flink Web UI, for monitoring statement execution.
