Deduplication
Pattern. Keep the first (or best) row per key inside the batch. The SQL processor operates per batch, so deduplication is exact within a batch; across batches, pair it with an idempotent sink or a keyed job operator.
The query
SELECT event_id, first_value(user) AS user, first_value(amount) AS amount
FROM (
SELECT event_id, user, amount,
row_number() OVER (PARTITION BY event_id ORDER BY amount DESC) AS rn
FROM flow
) ranked
WHERE rn = 1
PARTITION BY event_id is the dedup key; ORDER BY picks which duplicate
survives (amount DESC keeps the largest — use the event's own ordering
column for "first wins").
Input and output
Input (three rows, e-1 duplicated):
| event_id | user | amount |
|---|---|---|
| e-1 | a | 10 |
| e-2 | a | 12 |
| e-1 | a | 10 |
Output (two rows):
| event_id | user | amount |
|---|---|---|
| e-2 | a | 12 |
| e-1 | a | 10 |
Runnable example
examples/sql_dedup.yaml
(manifest name sql-dedup) wires memory → json_to_arrow → sql → arrow_to_json → stdout. Run it with arkflow --config examples/sql_dedup.yaml.
Boundaries
- Per-batch scope. The
sqlprocessor sees oneMessageBatchat a time. Duplicates spread across batches are not removed here — for cross-batch exactly-once dedup use an idempotent sink (UPSERT, point id) or a stateful Job operator. - Column pruning.
first_value(col)per carried column keeps the row's other values consistent with the winner; selecting raw columns directly is only valid when they are functionally dependent on the key.