# CSV recovery: a missing value is not an empty string

Status: source-reviewed native research; publication supplement prepared.
No independent peer review or deployment is claimed by this packet.
Initial observations use CPython 3.12.3; exact interpreter, SQLite source ID,
binary/script hashes and all typed rows are in results.json. The follow-up in
CROSS_READER.md rereads these same exports and rewrites on 3.12.3 and 3.13.16.

## Finding

A CSV can parse successfully, keep every row and survive byte-identical
read/write cycles while losing the distinction between SQL NULL and empty text.
In this designed fixture, ordinary quoting changes a missing value into an
empty string. The resulting SQLite database passes integrity_check, yet
COUNT(value) increases from 11 to 12. The experiment is not a corruption-rate
estimate and does not identify a new Python defect.

The useful distinction is **where information disappears**:
the writer can erase it before archival; the reader can discard a distinction
still present in the file; a later rewrite can then make that loss permanent.
A checksum of the exported file says nothing about the first transition.

## What was executed

Twelve deliberately chosen optional-text records contain one None, one empty
string, literal backslash-N / NA / NULL, zero and leading-zero text, comma, quote,
embedded CRLF, a space and a Unicode character. Six policies are each run with
strict=False and strict=True: 12 native paths, not 12 independent samples.

Each path uses the standard CSV writer and reader, preserves its exact UTF-8
wire text and SHA-256, binds restored values into a fresh in-memory SQLite
table, reads every value with typeof(), runs integrity_check, and rewrites the
CSV. No PostgreSQL server, network failure, disk fault or power loss is involved.

| Policy on measured CPython 3.12.3 | SQL NULL | Empty strings | Changed row | Rewritten CSV identical |
|---|---:|---:|---|---|
| QUOTE_MINIMAL | 0 | 2 | missing | yes |
| QUOTE_ALL | 0 | 2 | missing | yes |
| QUOTE_NOTNULL | 0 | 2 | missing | no |
| QUOTE_STRINGS | 0 | 2 | missing | no |
| Naive backslash-N replacement | 2 | 1 | literal-marker | yes |
| Explicit is_null column | 1 | 1 | none | yes |

Both strict settings produce these outcomes. Every path returns 12 records and
integrity_check=ok. The machine-generated full table is results-table.md.
It is important that unchanged CSV bytes are observed **after serialization**,
not a hash match against the typed source database.

## Three loss boundaries

### 1. Export can be many-to-one

The default writer maps None and empty text to indistinguishable field contents
in the tested multi-column layout. QUOTE_ALL also quotes both as empty text.
The initial typed difference is already gone. A later null-aware reader cannot
infer which empty field originally meant missing.

### 2. Reader version is part of the recovery contract

QUOTE_NOTNULL and QUOTE_STRINGS writers retain the lexical distinction in this
fixture. However, Python 3.12 readers do not implement the corresponding null
interpretation. This is explicitly documented by Python, not inferred from the
option names. The [3.12 documentation](https://docs.python.org/3.12/library/csv.html)
and upstream [issue113732](https://github.com/python/cpython/issues/113732)
identify the boundary.

The pinned [3.12.3 parser](https://github.com/python/cpython/blob/f6650f9ad73359051f3e558c2431a109bc016664/Modules/_csv.c#L573)
saves strings and only enables numeric conversion for QUOTE_NONNUMERIC.
The pinned [3.13.0 parser](https://github.com/python/cpython/blob/60403a5409ff2c3f3b07dd2ca91a7a3e096839c7/Modules/_csv.c#L605)
tracks whether a field was quoted and maps an unquoted empty field to None in
the two null-aware modes. This explains the mechanism; the initial capture did not
execute Python 3.13. The subsequent cross-reader capture now tests 3.13.16. The distribution-built 3.12.3 executable is hashed but is
not asserted to be an unmodified build of the upstream tag.

Strict parsing checks syntax, not the optional-text preservation contract.
Changing runtime versions also does not make QUOTE_STRINGS a universal type
archive: its documented numeric conversion uses floats. This study deliberately
does not test numeric-object serialization.

### 3. Normalization can destroy evidence still present in the original

The direct input left,,"" contains distinct unquoted and quoted empty fields.
The measured 3.12 null-aware reads return two empty strings. Rewriting that
result with QUOTE_NOTNULL quotes both. Keep the original bytes until typed
recovery checks pass; replacing them with a successfully normalized CSV can
discard the only remaining lexical distinction.

PostgreSQL 17 COPY CSV documents a quoting-sensitive null convention:
an unquoted null marker versus a quoted matching data value, with FORCE_NULL
and FORCE_NOT_NULL changing interpretation. Therefore a generic parse/rewrite
stage cannot be assumed transparent to COPY. This is a source-based implication,
not a PostgreSQL execution result.
[PostgreSQL COPY](https://www.postgresql.org/docs/17/sql-copy.html)

## Positive control and its limits

The explicit-column control uses id,is_null,value. A missing value is encoded
with is_null=1 and an empty payload; non-null text uses is_null=0, including
empty text and the literal marker. The decoder rejects unknown flags, duplicate
IDs, unexpected column counts and nonempty payloads marked null. Full keyed
value comparison still matters: swapping the flags of the missing and empty
rows preserves both aggregate counts while changing record meaning.

This is an illustrative optional-text contract, not a proposed general storage
standard or authentication mechanism. Bind the flag, payload, record identity,
schema and format version together when defining a real archive format.
A declared marker is another option only if valid data cannot collide with it
or an escaping convention preserves the distinction. The naive marker control
intentionally omits that protection. W3C CSVW metadata can declare null strings
and requirements; it cannot reconstruct erased distinctions.
[CSVW primer, sections 3.11 and 3.13](https://www.w3.org/TR/tabular-data-primer/)

## Practical acceptance rule

Validate the intended consumer, runtime and null convention, then compare
record identities and typed values. Check null and empty counts separately,
but do not stop there. Keep original bytes, schema and producer/consumer
versions. Test literal null markers and embedded syntax. Passing row counts,
parser strictness, database integrity and exported-file hashes are useful,
but each answers a different question.

## Reproduction

No package installation or external service is needed. From this directory:

    python3 observe.py --verify results.json
    python3 -m unittest -v test_observe.py test_packet.py
    python3 -O -m unittest -v test_observe.py test_packet.py

Exact verification intentionally requires the captured interpreter/module and
experiment hashes. The original observe.py and its tests are frozen 3.12
evidence, not a portable 3.13 capture command: their unquoted auxiliary token
raises ValueError under QUOTE_STRINGS on 3.13. Use the separate cross_reader.py
for another reader build, without overwriting retained evidence:

    python3 cross_reader.py --verify cross-reader-312.json
    python3.13 cross_reader.py --verify cross-reader-313.json
    python3.13 -m unittest -v test_cross_reader.py

CROSS_READER.md documents the native comparison and exact build requirements.
The supplementary script preserves the original bytes, records the auxiliary
error separately and explicitly closes its SQLite connections.

capture_sources.py retrieves six primary sources and records access time, status,
length, SHA-256 and source locator. It refuses to overwrite receipts. Full
upstream pages/code are not redistributed. Rights remain with their holders;
this packet makes no new licensing decision.

## Corpus placement

Extend the existing
[backup verification analysis](https://hesela.com/analyses/backup-verification-is-not-a-restore-test/)
with the prepared CSV null-fidelity subsection near its reader-policy discussion.
Link to existing
[Parquet read coverage](https://hesela.com/analyses/parquet-checksums-read-coverage/)
for the separate issue of which bytes are actually examined.
Do not add a competing general backup article or a glossary entity for every
quoting option. This packet adds a tested optional-text contract absent from
the searched corpus; it does not replace existing SQLite identity, HDF5 or
Parquet findings.
