Scenario G: Applying CDC changes with versioned upserts

Part of Streaming Transformations › Scenarios.

Goal

Apply captured order changes for a single market.

Transformations used

  1. Versioned upserts: apply captured changes, that is INSERT, UPDATE and soft DELETE, where a DELETE row supersedes earlier versions and is filtered out at query time.
  2. Stream filtering: ingest only records of a particular market.

Source data

Records are streamed in as follows, intentionally out of order:

{"order_id": 9000, "item_count": 0, "market": "ca", "status": "INSERT", "record_version": 1}
{"order_id": 9000, "item_count": 2, "market": "ca", "status": "UPDATE", "record_version": 3}
{"order_id": 9000, "item_count": 1, "market": "ca", "status": "UPDATE", "record_version": 2}
{"order_id": 9000, "item_count": 2, "market": "ca", "status": "DELETE", "record_version": 4}
{"order_id": 9001, "item_count": 0, "market": "de", "status": "INSERT", "record_version": 1}
{"order_id": 9001, "item_count": 2, "market": "de", "status": "UPDATE", "record_version": 3}
{"order_id": 9001, "item_count": 1, "market": "de", "status": "UPDATE", "record_version": 2}
{"order_id": 9001, "item_count": 2, "market": "de", "status": "DELETE", "record_version": 4}
{"order_id": 9002, "item_count": 0, "market": "de", "status": "INSERT", "record_version": 1}
{"order_id": 9002, "item_count": 3, "market": "de", "status": "UPDATE", "record_version": 2}

Note

The records above show only the first changes for orders 9000–9002. Keep streaming order changes after these records rather than stopping. Background merge passes depend on ongoing ingestion, so LAST_DEDUP reaches the “After merge” state only while the stream continues. LATEST_DEDUP returns the current state of orders 9001 and 9002 right away.

Target table

The PIPE lands filtered orders in the Interactive target table:

CREATE INTERACTIVE TABLE orders_de (
  order_id NUMBER NOT NULL,
  item_count NUMBER NOT NULL,
  market VARCHAR,
  status VARCHAR,
  record_version NUMBER NOT NULL,
  CONSTRAINT pk_order_id
    PRIMARY KEY (order_id) INITIALLY DEFERRED
)
VERSION BY record_version
CLUSTER BY (order_id);

Transformation

The PIPE filters the records by market.

-- Pipe with filter
CREATE OR REPLACE PIPE orders_de_pipe AS
  COPY INTO orders_de (order_id, item_count, market, status, record_version) FROM (
    SELECT s.$1:order_id::NUMBER,
           s.$1:item_count::NUMBER,
           s.$1:market::VARCHAR,
           s.$1:status::VARCHAR,
           s.$1:record_version::NUMBER
    FROM TABLE(DATA_SOURCE(TYPE => 'STREAMING')) s
    WHERE s.$1:market::VARCHAR = 'de');

Result

Before merge

Directly after ingestion and before the first merge the following results are returned.

LAST_DEDUP

SELECT order_id, item_count, market, status, record_version
  FROM orders_de DEDUP_OPTION = LAST_DEDUP
  WHERE status <> 'DELETE';

No result. LAST_DEDUP shows the latest version only after a merge pass.

LATEST_ALL

SELECT order_id, item_count, market, status, record_version
  FROM orders_de DEDUP_OPTION = LATEST_ALL
  WHERE status <> 'DELETE';
ORDER_IDITEM_COUNTMARKETSTATUSRECORD_VERSION
90010deINSERT1
90011deUPDATE2
90012deUPDATE3
90020deINSERT1
90023deUPDATE2

Note

The order of results shown is not guaranteed because the example query has no ORDER BY.

LATEST_DEDUP

SELECT order_id, item_count, market, status, record_version
  FROM orders_de DEDUP_OPTION = LATEST_DEDUP
  WHERE status <> 'DELETE';
ORDER_IDITEM_COUNTMARKETSTATUSRECORD_VERSION
90023deUPDATE2

After merge

After a merge all three queries, that is LAST_DEDUP, LATEST_ALL and LATEST_DEDUP, return the same result, as long as no additional rows have been ingested since the last merge pass.

ORDER_IDITEM_COUNTMARKETSTATUSRECORD_VERSION
90023deUPDATE2

Soft-deletes

Versioned upserts don’t physically delete rows. A delete arrives as a new row version with status = 'DELETE', and it supersedes earlier versions like any other change. Each query therefore filters out DELETE rows with WHERE status <> 'DELETE'. This is why order 9001, whose highest version is the DELETE, doesn’t appear in the deduplicated results.

Considerations

  1. Standard Snowflake tables and managed Iceberg tables are not currently supported.
  2. A table with versioned upserts can’t be the source of an insert-triggered materialized view, a dynamic table, or a table stream.
  3. Merge passes run as ingestion continues. If streaming stops, the most recent commits might stay invisible to LAST_DEDUP; use LATEST_DEDUP to read them.

See also

See Versioned upserts and Stream filtering for more information on the transformations used in this scenario.


Previous: Scenario F: Privacy-safe ingestion · Next: Monitoring & Troubleshooting