Sub topic 5
Comparing versions to see what changed
This chapter assumes you can read DESCRIBE HISTORY output and run VERSION AS OF queries, both covered earlier. You should also know basic SQL set operations and PySpark DataFrames. Here you will use time travel to find which rows changed between two versions of main.sales.orders.
Why compare versions
DESCRIBE HISTORY tells you what operation ran and how many rows it touched. It does not tell you which rows were affected. When an audit question arrives ("which orders changed after the nightly job?"), you need the rows themselves. Because each version is a readable snapshot, you can compare two snapshots with ordinary set operations.
Rows added or changed since a version
EXCEPT returns rows from the first query that do not appear in the second. Comparing the current table to version 5 gives every row that is new or was modified since then:
SELECT * FROM main.sales.orders
EXCEPT
SELECT * FROM main.sales.orders VERSION AS OF 5;An updated row appears here because its new column values differ from the old row. The comparison covers whole rows, so a change to any column makes the row show up.
Rows removed or changed in the other direction
Reverse the two queries to see rows that existed in version 5 but are gone now. These are deleted rows, plus the old values of rows that were updated:
SELECT * FROM main.sales.orders VERSION AS OF 5
EXCEPT
SELECT * FROM main.sales.orders;Run both directions to get a full picture. An updated row appears in both results: its new form in the first and its old form in the second.
| Query direction | Rows it returns |
|---|---|
| Current `EXCEPT` version 5 | Inserted rows and new values of updated rows |
| Version 5 `EXCEPT` current | Deleted rows and old values of updated rows |
Separating inserts, deletes, and updates
If the table has a key column, such as order_id, you can classify each change by joining the two snapshots. This query labels every difference between version 5 and the current table:
WITH old AS (
SELECT * FROM main.sales.orders VERSION AS OF 5
),
new AS (
SELECT * FROM main.sales.orders
)
SELECT
COALESCE(new.order_id, old.order_id) AS order_id,
CASE
WHEN old.order_id IS NULL THEN 'inserted'
WHEN new.order_id IS NULL THEN 'deleted'
ELSE 'updated'
END AS change_type
FROM new
FULL OUTER JOIN old ON new.order_id = old.order_id
WHERE old.order_id IS NULL
OR new.order_id IS NULL
OR NOT (new.status <=> old.status AND new.amount <=> old.amount);The <=> operator is null-safe equality. A plain = returns NULL when either side is NULL, which would hide changes to or from a NULL value. Replace status and amount with the columns you want to track.
The same comparison in PySpark
The versionAsOf reader option loads a past snapshot into a DataFrame. exceptAll is the DataFrame equivalent of EXCEPT ALL:
current = spark.read.table("main.sales.orders")
old = spark.read.option("versionAsOf", 5).table("main.sales.orders")
added_or_changed = current.exceptAll(old)
removed_or_old = old.exceptAll(current)
print("Added or changed:", added_or_changed.count())
print("Removed or old values:", removed_or_old.count())
added_or_changed.show(10)Both DataFrames must have the same columns in the same order. If a schema change happened between the versions, the set operations fail or give misleading results. Select a shared column list before comparing.
Choosing the versions to compare
Pick the version numbers from DESCRIBE HISTORY. To audit one commit, compare version N with version N-1. To audit a time window, compare the version at the start with the version at the end.
Cost and limits
- Each query reads two full snapshots. On a large table this can be slow, so filter both sides with the same
WHEREclause, for example on a date partition, before comparing. - The old version must still be readable. If
VACUUMhas deleted the data files for version 5, the query fails. Retention is covered in later chapters. - For regular change tracking over many commits, comparing snapshots by hand does not scale. Change Data Feed, covered at the end of this tutorial, records row-level changes directly.
Exercise
You compare version 8 of a table to the current version with SELECT * FROM current EXCEPT SELECT * FROM current VERSION AS OF 8. The result has 3 rows. A colleague says three rows were inserted. What else could explain them, and what second query would help you find out?
Show answer
Some of the 3 rows could be updated rows, because an updated row has different column values and so appears as a new row. Deleted rows are not visible in this direction. Run the reverse query (version 8 EXCEPT current) to see the old values of updated rows and any deleted rows. Better, join the two snapshots on a key column to label each row as inserted, deleted, or updated.
Exercise
Why might the PySpark comparison current.exceptAll(old) fail after a ALTER TABLE ... ADD COLUMNS commit that happened between version 5 and the current version?
Show answer
The two DataFrames now have different schemas, and set operations require the same number and compatible types of columns. Select the same list of columns from both DataFrames before calling exceptAll.