A training table is refreshed every week, and each model records which version it trained on. Write the function that compares two versions of the table by primary key and gives each version a content hash that does not depend on row order.
Implement diff_snapshots(old_rows, new_rows, key). Each snapshot is a list of dicts (rows). Every row has the column key, its value is unique within a snapshot, and all key values are of one type. Values are strings, ints, floats, booleans or None.
Return a dict with:
added: keys present only in the new snapshot, sorted.removed: keys present only in the old snapshot, sorted.modified: for each key in both snapshots whose row changed, a pair (key, sorted list of changed columns), sorted by key. A column is changed when it is missing from one of the two rows, or when the two values differ in value or in type (so 3 to 3.0 and 1 to True are changes, although Python's == calls them equal).schema: a dict with added, removed and retyped column lists, each sorted. A snapshot's columns are all column names that appear in any of its rows; a column's type set is the set of type(v).__name__ over its non-None values. retyped holds columns in both snapshots whose type sets differ.old_hash, new_hash: the first 12 hex characters of the content hash of each snapshot, defined as hashlib.sha256(blob.encode("utf-8")).hexdigest() where blob = json.dumps(rows_sorted_by_key, sort_keys=True, separators=(",", ":")).same_content: whether the two hashes are equal.