fix(csv): preserve leading-zero ID columns on read; schema-stable atomic stream writer - #393
Merged
Merged
Conversation
…mic stream writer Leading zeros (#228): `freshdata clean`, `freshdata stream` and `fd.clean_csv` read CSV input with pandas type inference, so "02134" was already the integer 2134 before the pipeline's preserve_leading_zeros handling could see it. A new internal helper, `_csv_io.leading_zero_dtypes`, reads a bounded sample (10,000 rows; the first chunk for streaming) as text and returns {column: str} for columns whose values all parse as numbers and include zero-padded values, using the dtype step's existing `_has_leading_zero_ids` detector. All three read paths use it; streaming passes the same mapping to every chunk. The scan is skipped when the caller sets `read_csv_kwargs` dtype/converters or preserve_leading_zeros=False, and falls back to plain inference if the sample cannot be read, so the real read reports its own error. Stream writer (#248, part a): `_BatchWriter` assumed every batch matched the first one. CSV batches were appended by position, so a batch without an anomaly-flag column shifted the other flags under the wrong header. Parquet raised mid-stream on an int64 -> double change and left a truncated file. The first batch now fixes the layout: later CSV batches are reindexed to its columns, later Parquet tables are cast (safe=True) to its schema, and a new column or a failed cast raises ValueError. Output is written to a sibling `<output>.partial`, moved onto the output path with os.replace on success and removed on failure. That leaves an existing output untouched and no partial file behind. `stream` and `stream-kafka` both use this commit/abort flow. Closes #228 Refs #248 (part a)
Contributor
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
FreshData benchmark report —
|
| fixture | n_rows | n_cols | p50 s | p95 s | peak MB | repair % | false-repair % | preserve % | trust | monotonic | export % |
|---|
Authored-code reduction (Metric 6)
This was referenced Sep 15, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Builds on #351 (merged); rebased onto
main.Summary
Leading zeros in CSV input (#228).
freshdata clean,freshdata streamandfd.clean_csvread CSV with pandas type inference, so02134became2134beforepreserve_leading_zeroscould act. A new internal helper,freshdata._csv_io.leading_zero_dtypes, reads a bounded sample (10,000 rows; the first chunk when streaming) as text and returns{column: str}for numeric-looking columns with zero-padded values, using the dtype step's existing_has_leading_zero_idsdetector. All three read paths use it, and streaming passes the same mapping to every chunk. The scan is skipped whenread_csv_kwargssetsdtype/convertersorpreserve_leading_zeros=False. Decimal (0.25), negative, all-zero and scientific-notation columns stay numeric.Schema-stable, atomic stream output (#248, part a). The first batch now fixes the output layout. Later CSV batches are reindexed to its columns, so a missing flag column is written empty instead of shifting values. Later Parquet tables are cast (
safe=True) to its schema. An unexpected new column or a failed cast raises a clearValueError. Output is written to a sibling<output>.partialand moved onto the output path withos.replaceon success, or deleted on failure, so a failed run leaves neither a truncated file nor a.partialbehind.Behaviour changes:
123, not123.0).freshdata streamexits 1 without writing output when a later batch adds columns or cannot be cast to the first Parquet schema.Tests
tests/test_csv_leading_zeros.py: CLIclean, multi-chunk CLIstream(and that every chunk gets the same dtype mapping), andfd.clean_csvkeep"02134"/"007"; unpadded numeric columns stay numeric; explicitdtype/convertersandpreserve_leading_zeros=Falseskip the scan;read_csv_kwargs(e.g.sep) are honoured by the scan.tests/test_stream_writer_schema.py(pyarrow viaimportorskip): dtype-flipping batches give a well-formed CSV and a readable Parquet with the first schema; a new column (CSV and Parquet) and an uncastable batch raise and leave no output and no.partial(an existing output is untouched); the freshdata stream output breaks when chunk dtypes change: misaligned CSV, partial Parquet #248 CLI reproductions now succeed with no.partialleft.Verification
ruff check .: passedmypy src/freshdata: no issuespytest -m "not online and not large"on the rebased branch: Python 3.12 / pandas 2.3.3: 5146 passed, 13 skipped; Python 3.9 / pandas 1.5.3: 5142 passed, 17 skippedCloses #228
Refs #248 (part a)