﻿# Dataset compaction: prove logical record equivalence when every file checksum changes

Dataset compaction: prove logical record equivalence when every file checksum changes. A practical data-quality guide with failure mechanisms, validation steps, acceptance evidence and primary references.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/dq-compaction-logical-record-equivalence/
Language: en

Compaction rewrites small dataset files into a different physical layout. Their checksums change even when the intended records do not. Accepting the rewrite therefore requires a logical comparison, while keeping the new physical manifest for integrity and retrieval. Neither equal row counts nor equal sets of distinct values establish record equivalence alone.

## Pin both readable dataset states

Record the source snapshot and candidate snapshot, their readers and schema projection before comparing. The [Iceberg specification](https://iceberg.apache.org/spec/) distinguishes immutable physical files and table metadata that determines visible state. Read through the appropriate table interface when delete files or other table semantics apply. Scanning every underlying data file as ordinary Parquet can reintroduce deleted rows. The [snapshot and schema guide](/journal/data-guide-iceberg-snapshot-schema-evolution/) explains why the selected schema also belongs to the comparison identity.

## Define the canonical record

Choose fields, types, null policy and any significant metadata before generating fingerprints. A string representation that omits type or conflates null with an empty string can make different examples compare equal. Preserve sequence position when it is part of the model input; an order-insensitive multiset is then insufficient. Decide explicitly how floating-point special values and equivalent encodings are treated instead of inheriting whichever equality a convenient export happens to provide.

## Compare multiplicities and values

Use keyed record multisets, or an equivalent complete comparison, to retain duplicate counts. A unique entity key must be validated rather than assumed; legitimate repeated observations need an occurrence rule. Compare schema and canonical values as well as keys. The [record identity guide](/journal/data-guide-hudi-incremental-record-keys/) explains why entity updates differ from appending another observation. That distinction prevents compaction from silently deduplicating a training example that was intentionally represented more than once.

## Keep writer behavior observable

The [Arrow dataset guide](https://arrow.apache.org/docs/python/dataset.html) describes scanning, partitioning and writing datasets into new files. Record the projection, partition interpretation and writer configuration used for the candidate. Directory names can contribute partition values that are not stored in every physical file. Reopen the written candidate through its actual reader before comparison. Inspect the complete selected manifest, not only the files returned by a convenient directory sample or a path that still contains leftovers from an earlier attempt.

## Challenge the equivalence checker

Build a small source containing a repeated record, a null, a type-sensitive value and a logically deleted row where the table format supports it. Rewrite the valid state, then create separate invalid candidates that drop a duplicate, change a value or resurrect the deleted row. Require the checker to accept the intended rewrite and reject each mutation with localized differences. This constructed fixture supplies acceptance criteria; no measured production compaction result is implied.

## Retain logical and physical evidence together

Store input/candidate snapshots, canonicalization rules, per-key differences, multiplicity verdict and new file checksums. Keep the source available until acceptance and rollback obligations are satisfied. A logical match does not establish identical scan speed, row ordering or future retention. Conversely, changed physical checksums are expected after rewriting and do not alone prove content corruption. The accepted result is equivalence under a declared record contract, plus an independently inspectable physical manifest for the exact candidate that passed it.
