Deduplicate rows with ROW_NUMBER
Keep each key's first row or its latest row with ROW_NUMBER() … = 1, set how long a key is remembered, and know when each row is written and what the plan guarantees.
A dedupe keeps one row for each key of a stream. It is a SQL statement with ROW_NUMBER() OVER (PARTITION BY … ORDER BY …) = 1. In ascending order of the event time, it keeps each key's first row, such as the first delivery of each event that a queue sends more than once. In descending order, it keeps each key's latest row, such as each customer's current profile. This page explains how to write both, when each row is written, how long a key is remembered, and what the plan reports. Write a SQL transform explains statements in general. Dedupe in the SQL reference lists every rule.
Before you start
- You can write a SQL transform, as Write a SQL transform shows.
- The stream has a column that holds the time of each row, such as
created_at, and you set it as the stream'seventTime. Event time and watermarks explains event time and the watermark. - The columns in
PARTITION BYidentify a row, such as an event ID or a customer ID. - A first-row dedupe with a retention gets the guarantee
exactly_once_withinover a stream that can repeat rows and is not read withfull_refresh, into a destination that stages and applies or writes each commit in a transaction, and not merged into a destination that stages and applies. The defaultdelivery.requirerefuses that guarantee. The output lists the guarantee for each case. The first example setsdelivery.requirefor it. Accept a dedupe's retention explains the choices.
Keep each key's first row
A queue can deliver one event more than once. To write each event once, keep the first row of each event_id:
source:
ref: events-queue
streams:
- name: events
eventTime: created_at
watermark: { delay: 30s }
dedupe: { retention: 24h }
delivery:
require: exactly_once_withinINSERT INTO events_once
SELECT event_id, kind, amount, created_at
FROM events
QUALIFY row_number() OVER (PARTITION BY event_id ORDER BY created_at) = 1;PARTITION BYnames the key, hereevent_id. The statement selects everyPARTITION BYcolumn, which is the output's primary key.ORDER BYis the stream'seventTimecolumn, and nothing else. Ascending order keeps the first row.- Compare the number
= 1inQUALIFY, or in theWHEREof aSELECTthat reads it. dedupe.retentionis how long, in event time, the dedupe remembers a key after its first row. It is required, and has no default.delivery.require: exactly_once_withinaccepts that a key is written again by a row at or after its first row's time plus the retention. Without it, the plan refuses the pipeline if the source can repeat rows.
Set the retention
Set source.streams[].dedupe.retention to one of these:
| Retention | What the dedupe does | What the plan reports |
|---|---|---|
A duration, such as 24h | It forgets a key once the watermark passes the key's first row's time plus the retention. The key's next row is then written again, as a new first row. | exactly_once_within, with the retention in its reason, over a stream that can repeat rows and is not read with full_refresh, into a destination that stages and applies or writes each commit in a transaction, unless the output is merged into a destination that stages and applies. The default delivery.require refuses it. The output has the other cases. |
unbounded | It remembers every key. Its state grows with the number of keys. | exactly_once, and that the state grows with the number of keys, over a stream that is not read with full_refresh, into a destination that stages and applies or writes each commit in a transaction. The output has the other cases. |
Choose a duration that is longer than the time over which the source can repeat a row. A queue that redelivers a message within an hour needs at least 1h. If the plan finds a first-row dedupe over a stream that sets no retention, it has a problem, and openetl apply refuses the pipeline:
problems:
transform.sql:1:1: the dedupe writing "events_once", ROW_NUMBER() … = 1 in ascending order of the event time, keeps each key's first row, and stream "events" sets no dedupe.retention, how long it remembers a key: set source.streams[].dedupe.retention to a duration of event time, such as 24h, after which a key's next row is written again, or to unbounded, to remember every keyWhen the first row is written
A row of a key that the dedupe has not kept waits until the watermark passes its event time. Until then, an earlier row of the key can still come. When the watermark passes it, the earliest row of the key is written, in the next epoch that the pipeline commits. A row that comes out of order, within the watermark's delay, never makes a later row win.
A row of a key that the dedupe keeps, with a time less than the retention after the key's first row, is a repeat. The dedupe drops it as it comes, and does not keep it in state.
A row with a time at or after the key's first row's time plus the retention is not a repeat, even while the key is still kept. It waits for the watermark as a row of a new key does. By the time the watermark passes its time, the watermark has passed the end of the retention, so the key is forgotten. The earliest such row is then written, as the key's next first row. Each later one is a repeat if its time is less than the retention after that row. For example, with retention: 10s, a key whose first row is at 12:00:25 and the watermark at 12:00:30:
| A row of the key at | What the dedupe does |
|---|---|
| 12:00:31 | Drops it as a repeat, as 12:00:31 is less than 10 seconds after 12:00:25 |
| 12:00:35 | Keeps it in state, and writes it once the watermark passes 12:00:35 |
Late rows
A row is late when the watermark has passed its event time before the row comes:
- If its key is kept, the late row is a repeat, and the dedupe drops it.
- If its key is not kept, the late row may have been the key's first row, which the output cannot get now. The engine quarantines it with the code
late. This applies also to a row of a key that the dedupe forgot before the row came. In the example above, without the row at 12:00:35, a row at 12:00:34 that comes after the watermark passes 12:00:35 is quarantined. - A row whose event time is null is quarantined with the code
event_time.
A dedupe takes no watermark.lateness: the plan refuses lateness on a stream that only a dedupe or OVER reads. To take rows that come out of order, set a longer watermark.delay.
Keep each key's latest row
To keep each customer's current profile, keep the latest row of each customer_id:
INSERT INTO latest_profiles
SELECT customer_id, email, plan, updated_at FROM (
SELECT *, row_number() OVER (PARTITION BY customer_id ORDER BY updated_at DESC) AS rn
FROM profile_changes
) AS t
WHERE rn = 1;- Descending order keeps the latest row.
- The output is written as upserts keyed by the
PARTITION BYcolumns, so it needsdestination.write: merge. Withoutdestination.write, the output is merged where the destination can merge. On another write mode, or a destination that cannot merge, the plan has a problem. - A row is written as it comes, if it is later than the key's latest row. An older row that comes after a newer one never replaces it.
- A latest-row dedupe takes no retention. It keeps each key's latest time for good, so its state grows with the number of keys. The plan reports
exactly_once, and that cost. A stream that setsdedupe.retentionand has no first-row dedupe over it is a problem in the plan.
A filter after the latest row
A filter beside rn = 1 runs on each key's latest row. The output holds a key's row only where its latest row passes the filter:
INSERT INTO paying_profiles
SELECT customer_id, email, plan, updated_at FROM (
SELECT *, row_number() OVER (PARTITION BY customer_id ORDER BY updated_at DESC) AS rn
FROM profile_changes
) AS t
WHERE rn = 1 AND plan <> 'free';If a customer's latest row has plan = 'free', and the output can have the customer's row, the dedupe writes a delete of the customer's key. The delete has the key's columns, and nulls in the others, or their type's zero where a column cannot be null. destination.deletes says how the destination applies it. If the filter cannot evaluate a row, the engine quarantines it with the code statement, and the output keeps the row it had.
The dedupe keeps, for each key it has seen, whether the output has the key's row. When the filter drops a key's latest row, the stream's read decides whether it writes the delete:
| Read | Key whose row the dedupe wrote last | Key whose delete the dedupe wrote last | Key it has not seen, or saw only in quarantined rows |
|---|---|---|---|
full_refresh | Writes the delete | Writes nothing: the dedupe already wrote a delete for the key | Writes the delete: each sync's read starts with no state, and the output can have the key's row from an earlier sync |
incremental or stream | Writes the delete | Writes nothing | Writes nothing: its state holds every key since its state began |
For example, with the filter amount < 50, a key's rows with the amounts 20, 90 and 95, which come one after another in that order of time, write an insert, then one delete, then nothing for 95.
A delete of a key that the table does not have does nothing with destination.deletes: hard. With soft, the default, the official Postgres and Iceberg destinations add a row for the key with _openetl_deleted true and nulls in its other columns. So over full_refresh with soft, each sync writes such a row for every key whose latest row the filter drops, also for a key whose rows never passed the filter.
Over incremental or stream, a reset or an edit that gives the dedupe a new ID starts its state empty. Then a key whose row the output has from before, and whose latest row the filter now drops, keeps that row. The row stays until a row of the key passes the filter again, or until you delete it from the table. To remove such rows, delete from the table each key whose latest row in the source fails the filter.
Only a row later than every row that the dedupe wrote of a key is written, and the merge keeps a key's row with the greatest _openetl_seq, the event time. So a key's row ends as its latest row has it: a delete that an earlier row gave never replaces the latest row.
Rows with the same time
Rows of one key with the same event time go by their record IDs (_openetl_id). A first-row dedupe keeps the least, and of rows with one ID, the least by the bytes that the engine encodes their values to, which is not the order of the values. A latest-row dedupe keeps the greatest, and of rows with one ID, the greatest by a 64-bit hash of their values, which is not the order of the values. The order depends only on the rows, not on the order in which they arrive. So a retry, or a read of the rows again, keeps the same row. If the order of such rows matters to you, give them event times that differ.
The output
| Dedupe | Rows | Write modes | _openetl_id | _openetl_seq |
|---|---|---|---|---|
| First row | Each key's first row, once for each retention, as an insert | Append, merge, or overwrite with full_refresh | The input row's | Its event time |
| Latest row | Each key's latest row, as an insert, then updates, and a delete where a filter drops it | Merge | The input row's | Its event time |
The dedupe drops a row that the source repeats after a failure, as it drops any repeat. Into a destination that delivers and acknowledges, the output is at_least_once. Into a destination that stages and applies or writes each commit in a transaction, the plan reports the output's guarantee from the read, the retention and the write mode:
- A stream read with
full_refresh. Each full read starts with no keys kept, so every sync writes every key again. This applies to every dedupe, whatever its retention and whatever the stream's replay. Overwritten, the output isexactly_once. Merged into a destination that stages and applies, it iseffectively_once. Otherwise, appended included, it isat_least_once. - Another read of a stream that can repeat rows, with a duration. The guarantee is
exactly_once_within, and the reason names the retention. A row of a key at or after its first row's time plus the retention can be written as the key's next first row. If the output is written withmergeinto a destination that stages and applies, it iseffectively_once, as such a row updates its key's row. - Another read of a stream that can repeat rows, with
unbounded, or a latest-row dedupe. The guarantee isexactly_once.
The default delivery.require, exactly_once, refuses exactly_once_within, and the plan has a problem:
problems:
delivery.require: stream "events_once" gets exactly_once_within (a dedupe's output over superset inputs, which remembers a key 24h0m0s of event time from its first row, and writes a repeat after that again), below exactly_once: to accept that a key is written again by a row 24h0m0s or more of event time after its first row, set delivery.require: exactly_once_within; to remember every key from then on, set source.streams[events].dedupe.retention: unbounded (a key already forgotten stays forgotten)To accept the retention, set require: exactly_once_within, as Accept a dedupe's retention shows. The plan then reports:
streams:
events_once append exactly_once_within a dedupe's output over superset inputs, which remembers a key 24h0m0s of event time from its first row, and writes a repeat after that againA stream that its source replays exactly, which openetl discover shows as exact, returns after a restore exactly the records after the saved position. Over such a stream, unless it is read with full_refresh, into a destination that stages and applies or writes each commit in a transaction, the output is exactly_once whatever the retention, and the default require accepts it. The topic in Try it with Kafka is an example. The plan's reasons lists the guarantees and their reasons.
Across syncs
A scheduled pipeline keeps the dedupe's state in its checkpoint between syncs:
full_refresh. Each sync reads the whole stream, and starts with no state. A first-row dedupe writes every key's first row at the end of the read, so an appended output gets every key's rows again in each sync. A latest-row dedupe writes, in each sync, an upsert of every key's latest row that passes its filter, and a delete of every key whose latest row the filter drops, so the merged output loses a key whose latest row left the filter since the last sync. A key that the source no longer has keeps its row, as with any stream read withfull_refreshand merged.incrementalby the event time, orstream. The kept keys go on in the next sync, so a key written in one sync is a repeat in the next.incrementalby another cursor. Only a latest-row dedupe takes this read. The watermark moves to the end of input when each sync's read ends. The dedupe still keeps each key's latest time, record ID and hash, as it does over any read butfull_refresh. So a row no later than its key's latest from an earlier sync is a repeat in the next sync.
The plan refuses a first-row dedupe over a stream read with incremental by another cursor: each sync would start its keys again, and write each key's first row again. A latest-row dedupe takes any read, as it compares each row with its key's latest. A stream read with cdc is refused, as it is for OVER.
Where the state is kept
A first-row dedupe keeps each kept key with its first row's time, and the rows that wait for the watermark. A latest-row dedupe keeps each key's latest time, record ID and a hash of its values. The state is kept where a group window's is (Where the state is kept), with the same checkpoints, files and retention. It also uses the same disk cache, and the lanes write it to files early if it passes their part of the job's memory. So an unbounded retention's keys, or many keys within a long one, can outgrow the job's memory. If the state still does not fit, the run fails with RESOURCE_EXHAUSTED, and the message says to run the pipeline with a larger resources.size.
On workers, a dedupe sends each row to the worker that owns its key's key group, as OVER does. That worker keeps the key's state and writes its rows.
Change the statement
A dedupe keeps its state under an ID that the plan shows, in the state section of transform.sql:
1:1 events_once dedupe-811f1c1cde3a7c1a new dedupe first by created_at on event_id; remembers a key 24h0m0s of event time from its first row: exactly_once_within 24h0m0s of event time; starts emptyOther names for columns, columns that pass through, filters after the dedupe and the retention keep the ID. Another PARTITION BY, order, event-time column or filter before the dedupe gives a new ID, and the new dedupe's state starts empty: a first-row dedupe writes each key's next row again. Changing a dedupe lists the edits.
A changed retention applies to every key that the dedupe keeps, once the new version runs. A key that the old retention forgot stays forgotten. Keys kept while the retention was unbounded stay kept for good after a change to a duration. To forget them, reset the stream that the statement reads:
Warning
A reset reads the stream again for every statement that reads it. An appended output then gets the stream's rows again. Read streams again from the start has what the destination gets.
openetl reset --stream events PIPELINETry it with Kafka
This walk-through writes each payment of a Kafka topic once, however often a producer sends it within an hour. It writes them to Postgres, continuously. The pipeline is examples/kafka-running-total-dedupe.yaml. It also has the running count of Compute running totals with OVER, and that walk-through's first steps make the topics and apply it. Do those steps first.
The record's key is the payment's ID, and the record's timestamp is the stream's eventTime. The stream sets the retention to 1 hour:
- name: payments
eventTime: _kafka_timestamp
dedupe: { retention: 1h }INSERT INTO payments_once
SELECT _kafka_key AS payment_id, _kafka_partition AS part, _kafka_offset AS payment_offset,
_kafka_timestamp AS paid_at
FROM payments
QUALIFY row_number() OVER (PARTITION BY _kafka_key ORDER BY _kafka_timestamp) = 1;-
Send four records, a second apart, of which two repeat a payment:
for id in p-1 p-2 p-1 p-1; do echo "$id:{}"; sleep 1; done | kafka-console-producer.sh --bootstrap-server 127.0.0.1:9092 --topic payments \ --property parse.key=true --property key.separator=: -
Wait a minute, until the topic's idle partitions move the watermark past the records (Quiet splits).
-
Read the table:
psql "$DSN" -c 'SELECT payment_id, paid_at FROM kafka_sql.payments_once ORDER BY paid_at'It has two rows:
p-1, at the time of its first record, andp-2. The dedupe dropped the two repeats ofp-1. -
Send
p-1again. It is a repeat for 1 hour of event time after its first record, so the table still has one row of it. A record ofp-1an hour or more after the first is written again, as the key's next first row.
The keys that the dedupe keeps are in the pipeline's checkpoint. So a repeat that comes after a restart of the pipeline, or after a failure, is dropped too.
Watch it
The console's SQL tab shows, under At the last checkpoint, what the statement did in the last epoch: its watermark, the keys it keeps, the rows it took in and wrote, the repeats it dropped and its late rows. The same numbers are the openetl.operator.* metrics (Monitoring):
openetl.operator.rows.repeatscounts the rows that the dedupe dropped. Over time, the rows taken in are the rows written, the repeats, the late rows, the rows that the output quarantined and the rows that wait for the watermark. An epoch can write rows that came in an earlier epoch, so compare totals over time, not one epoch's numbers. A sudden rise in repeats can mean that a producer sends rows again.openetl.operator.windowsis the keys that the dedupe keeps. With a retention, it levels off at about the keys of one retention. Withunbounded, or for a latest-row dedupe, it grows with the number of keys, and so doesopenetl.operator.state.memory.openetl.operator.rows.writtenwithopenetl.opdeletecounts the deletes that a latest-row dedupe with a filter after it writes. It counts writes, not keys. The dedupe writes a delete each time a key's row leaves the filter. Over a stream read withfull_refresh, a key that the read has not written yet counts as having a row, so each sync writes at least one delete for every key whose latest row the filter drops.openetl.operator.rows.latecounts late rows that the engine quarantined. If it grows, set a longerwatermark.delay. A latest-row dedupe has no late rows.openetl.operator.watermark.lagis how far the watermark was behind the time of the commit. A first-row dedupe writes a row once the watermark passes its time, so a lag that grows means that its rows wait longer. A latest-row dedupe writes rows as they come, whatever the watermark.
Compute running totals with OVER
Give each row a running total, a moving average or the previous row's value with SQL's OVER, and know when each row is written, what happens to late rows, and where the state is kept.
Run a job on several pods
Spread a large job over workers with resources.workers, read what openetl plan says about it, and know what happens when one of its pods fails.