A research lake is never finished. Vendors add fields, a volume column becomes 64-bit because a share count overflowed, a new rating appears halfway through the history. The files written last year do not have what the files written today have, yet one query has to read them all. Parquet's answer is that every file carries its own schema.
The idea in plain words
Where we are. The previous topics were about reading less from files that all look the same. This one is about files that do not: the layout of the lake changes over time and the reader must reconcile the versions. It uses the partitioned lake of topic 5, whose folders accumulate files written under different schemas.
Changing the columns or types of a dataset over time while old data stays readable. Adding a column and widening a number type are safe; renaming or dropping columns, or changing what a column means, is where datasets break.
Add a column, read old and new together
Here the old file has three columns and the new one has a fourth, source. unified_schema merges the footers into one schema without reading any data. read_evolved hands that schema to the dataset reader, which reads each file as it is and fills the missing column with nulls for the old rows. Nothing had to be rewritten.
# An old file (3 columns) and a newer one with a fourth column, read togetherpq.write_table(table.slice(0, 1000), "old.parquet")pq.write_table(add_column(table.slice(1000, 5), "source", "vendor-b"), "new.parquet")print(unified_schema(["old.parquet", "new.parquet"]))both = read_evolved(["old.parquet", "new.parquet"])print(both.num_rows, "rows;", both["source"].null_count, "nulls in source")print(both.slice(998, 4).to_pylist()[1:3])
[('symbol', 'string'), ('date', 'date32[day]'), ('close', 'double'), ('source', 'string')]
1005 rows; 1000 nulls in source
[{'symbol': 'AAPL', 'date': datetime.date(2003, 12, 24), 'close': 0.305937, 'source': None}, {'symbol': 'AAPL', 'date': datetime.date(2003, 12, 26), 'close': 0.311343, 'source': 'vendor-b'}]Widening a type
A 32-bit volume is fine until one day it is not. Casting to 64 bits keeps every value and is the safe change, but the files already written still say int32, and a reader will not guess. widen rewrites a file with the column cast and leaves the original untouched, so you can validate the new file before swapping it in. Going the other way, narrowing, can silently lose values.
# A file with float32 closes, rewritten with float64 closes; the original stays as it wassmall = table.slice(0, 1000)small = small.set_column(2, "close", small["close"].cast("float32"))pq.write_table(small, "small.parquet")print("before:", pq.read_schema("small.parquet").field("close").type)print("after :", widen("small.parquet", "big.parquet", "close", "float64"))print("original still:", pq.read_schema("small.parquet").field("close").type)
before: float after : double original still: float
# The danger: two files that disagree on a type cannot be mergedtry: unified_schema(["old.parquet", "small.parquet"]) print("merged: a float32 and a float64 column do not agree, so this line should not print")except Exception as error: print(type(error).__name__ + ":", str(error)[:70])
ArrowTypeError: Unable to merge: Field close has incompatible types: double vs float
Parquet matches columns by name, so renaming px to close in new files makes old files look like they lack close and new files like they lack px. Prefer adding the new name, filling both for a while, and dropping the old one once every reader has moved. Treat a change of meaning (adjusted versus raw prices) as a new column, never as a quiet edit.
What the tasks ask
- 1`add_column`
table.append_column(name, pa.array([value] * table.num_rows)). - 2`unified_schema` and `read_evolved`
pa.unify_schemas([pq.read_schema(p) ...])gives the merged schema; pass it asschema=tods.dataset(paths, format="parquet")and call.to_table(). - 3`widen`Read the table,
set_columnwith the column cast topa.type_for_alias(new_type), writepath_out, returnstr(type)read back from it.
px to close in files written from 2024. What does a reader that merges all the files by name see?Lab · what's graded
4 tasks · 100 pts- Add a column:
add_column25 pts - Merge the schemas:
unified_schema25 pts - Read files of different shapes:
read_evolved25 pts - Widen a type safely:
widen25 pts