Sub topic 10
Reading versions, changes and filtered data
This chapter assumes you can load a shared table with a profile file and delta_sharing.load_as_pandas, and that you know what a share, schema and table are. It covers three ways to read less than the whole table: a specific version, a stream of row-level changes, and a filtered subset.
Why not reload the whole table
A full-table read downloads every data file the recipient is allowed to see. That is fine for small tables. For a large table that changes daily, it repeats work and cost. Delta Sharing gives the recipient three tools to avoid this:
- Version or timestamp reads, which return the table as it was at a point in time.
- Change data feed reads, which return only the rows that changed.
- Predicate and limit hints, which let the server skip files that cannot match.
Reading a version or timestamp
Every commit to a Delta table creates a new version number. If the provider has shared the table history, a recipient can ask for a given version or the state at a timestamp. Without history sharing, the server accepts only the latest version.
import delta_sharing
table_url = "config.share#my_share.my_schema.my_table"
df_v1 = delta_sharing.load_as_pandas(table_url, version=1)
print(df_v1.head())The timestamp argument works the same way. It takes a string such as "2024-01-01T00:00:00Z" and returns the latest version at or before that time. Use version when you track exact commits, and timestamp when you think in calendar terms.
Reading changes with change data feed
Change data feed (CDF) records row-level inserts, updates and deletes for each version. The provider must enable it on the Delta table and share the table with history. If either is missing, the request fails.
import delta_sharing
table_url = "config.share#my_share.my_schema.my_table"
changes = delta_sharing.load_table_changes_as_pandas(table_url, starting_version=1)
print(changes.head())The result has the table columns plus metadata columns:
| Column | Meaning |
|---|---|
| `_change_type` | `insert`, `update_preimage`, `update_postimage` or `delete` |
| `_commit_version` | The table version that made the change |
| `_commit_timestamp` | When that version was committed |
An update appears as two rows: the old values (update_preimage) and the new values (update_postimage). Filter on _change_type before applying changes to a downstream copy.
A common pattern is to store the last _commit_version you processed, then start the next read from that value plus one.
Filtering with hints
Delta Sharing supports predicateHints and limitHint in the request. They are hints, not guarantees. The server may use them to return fewer files, but the files it returns can still contain non-matching rows. Always apply the real filter in your own code.
With the Spark connector, a filter on a partition column or a statistic-backed column is pushed down where possible:
df = spark.read.format("deltaSharing").load("config.share#my_share.my_schema.my_table")
df.filter("order_date >= '2024-01-01'").show()Common errors
- Reading changes on a table without CDF. The server rejects the request. Ask the provider to enable change data feed and share history.
- Requesting a version that has been cleaned up. Old versions disappear after
VACUUMor log retention. Start from a newer version, or reload the full table. - Assuming hints filter rows. They filter files. Re-apply the predicate.
- Missing the first rows after a gap. If you start at a version later than the last one you processed, you skip changes. Keep the saved version strictly in sync.
Exercise
A recipient saved _commit_version = 12 after its last run. Which starting_version should the next call to load_table_changes_as_pandas use, and why might the call still fail?
Show answer
Use starting_version=13, because version 12 was already processed. The call can fail if the provider has not enabled change data feed on the table, has not shared its history, or if version 13 has been removed by retention cleanup.