Skip to content

feat(loader): add support for gzip-compressed CSV and TSV files - #7

Merged
IzanVil merged 3 commits into
IzanVil:mainfrom
jabrailkhalil:feat/gzip-csv
Sep 8, 2026
Merged

IzanVil merged 3 commits into
IzanVil:mainfrom
jabrailkhalil:feat/gzip-csv

Conversation

@jabrailkhalil

Copy link
Copy Markdown
Contributor

Closes #2.

Changes

Adds support for .csv.gz and .tsv.gz files in load_frame.

What changed in loader.py

  • Added import gzip
  • Extended CSV_EXTENSIONS with .csv.gz and .tsv.gz
  • Added _file_suffix(path) helper that returns compound suffixes like .csv.gz correctly (since Path.suffix would return just .gz)
  • load_frame now uses _file_suffix() for dispatch
  • csv_delimiter uses _file_suffix() so .tsv.gz is correctly treated as tab-delimited without sniffing
  • _sample_lines now opens .gz files via gzip.open in text mode — this is the key fix that ensures delimiter sniffing reads the decompressed text, not raw compressed bytes

Tests added ( ests/test_loader.py)

  • est_load_csv_gz_comma_delimited — comma-delimited .csv.gz loads and types columns correctly
  • est_load_csv_gz_semicolon_delimited — semicolon delimiter detected inside compressed file
  • est_load_tsv_gz_tab_delimited — .tsv.gz uses tab without sniffing
  • est_csv_gz_and_plain_csv_produce_same_profile — .csv.gz and its plain equivalent produce identical schemas

@IzanVil

IzanVil commented Sep 8, 2026

Copy link
Copy Markdown
Owner

Thank you for this — and for going after the part that was actually hard.

_sample_lines was the trap in this issue, and you found it, understood why it
mattered and fixed it correctly. Opening through gzip.open in text mode is
exactly right, and the test that compares a .csv.gz against its plain
equivalent is the one I most wanted to see. Handling .tsv.gz without sniffing
is a nice touch that I did not ask for.

Two things block it, and one is worth knowing about even though it is not
yours to fix.

1. The feature reaches the CLI and nothing else

.csv.gz goes into SUPPORTED_EXTENSIONS, but four other places compare that
set against Path.suffix — and Path("data.csv.gz").suffix is .gz, which is
never in the set:

Where What happens
datasemver/integrations/dvc.py:199 is_dataset returns False, so a DVC-tracked .csv.gz is silently skipped
datasemver_web/backend/history.py:65 The dashboard lists it under "ignored"
datasemver_web/backend/main.py (_suffix_of) The dashboard rejects the upload — and the error lists .csv.gz among the supported extensions while refusing it
scripts/run_datasemver_on_pr.py:180 Writes the base file as base.gz, and the diff then fails on an unsupported extension

_file_suffix is the right idea. Could it move out of loader.py as something
public — dataset_suffix() — and be used by all five call sites rather than
only the loader? That also removes the duplication between the hardcoded
(".csv.gz", ".tsv.gz") tuple and CSV_EXTENSIONS, which would otherwise drift
the first time someone adds .json.gz.

2. Lint

import gzip as _gzip sits partway down tests/test_loader.py, which is E402
and fails the Quality job. Moving it to the top with the other imports is all it
needs.

CI has not actually run on this yet — fork workflows wait for approval here, so
the runs are sitting at action_required. I will approve them once the lint is
fixed, and then we will both be able to see the suite rather than guess at it.

3. Not yours, but we should not merge without deciding

Your PR made me measure something. An ordinary repetitive CSV compresses about
344:1 — 281 KB in, 99 MB out. The dashboard caps uploads at 25 MB measured
on the compressed bytes, so accepting .csv.gz there turns that cap into
roughly 8 GB of memory, and a deliberately crafted file goes much further.

Right now bug 1 accidentally hides this, because the dashboard refuses these
files anyway. That is luck, not a defence: fixing the suffix without noticing
this opens it.

I do not think this belongs in your PR. My suggestion is that you fix 1 and 2,
and leave the dashboard out of dataset_suffix() for now with a comment saying
why, and I will open a separate issue for a decompressed-size guard. Happy to
hear it differently if you would rather take it on.

Also

The issue asked for the new extensions in both READMEs and in docs/tutorial.md
wherever the other formats are listed. Small, but it is how someone finds out
the feature exists.


None of this is a criticism of your judgement — the first two are things you
could only know by having lived in this codebase, which is what review is for.
Ask here if any of it is unclear; a question is faster than a wrong guess.


@jabrailkhalil

Copy link
Copy Markdown
Contributor Author

Implemented the review feedback in commit �319d90: public dataset_suffix() is now shared by loader, DVC, dashboard history/upload, and the PR script; compressed suffixes are preserved for fetched files; gzip import was moved to the test imports; regression tests and format documentation were added. Local validation: 161 passed, 2 skipped with the focused suite, and Ruff passed.

@jabrailkhalil

Copy link
Copy Markdown
Contributor Author

Сейчас заякорил ветку на HEAD base и отпишитесь, пожалуйста, на запуск после sync; изменений по багфиксам больше не добавлял в этот PR.

@IzanVil

IzanVil commented Sep 8, 2026

Copy link
Copy Markdown
Owner

This is good, and in two places it is better than what I asked for.

dataset_suffix() deriving the compound suffixes from SUPPORTED_EXTENSIONS
instead of a hardcoded tuple is the right call — it removes the duplication I
only flagged as a smell, and sorting by length so .csv.gz wins over .csv is
the detail that makes it correct rather than nearly correct. And you found two
things I had not: the stem calculation in history.py, which would have stopped
customers_v1.csv.gz matching the version pattern, and the hardcoded extension
tuple in the PR script, which means it now analyses Excel too.

I ran the whole suite rather than a focused subset, since CI has not been able
to on this branch:

  • ruff check, ruff format, mypy — all pass
  • 564 passed, 1 skipped, up from 556 on main
  • Coverage 98.76%, unchanged from the baseline

One change, and it is my fault the scope grew

I asked you to leave the dashboard out of this and then did not say clearly
enough where the line was. _suffix_of in datasemver_web/backend/main.py is
the one place that should stay as it was.

Here is why, measured against your branch through the real upload path:

dashboard limit: 25.0 MB
file uploaded: 0.25 MB ← 1% of the limit
decompressed: 86 MB ← 344x
response: HTTP 200
peak RSS: 138 MB -> 656 MB

A quarter of a megabyte took half a gigabyte of memory and the dashboard
accepted it. A file at the actual limit would be a hundred times that. The cap
is measured on the compressed bytes, so admitting .csv.gz there turns a 25 MB
ceiling into no ceiling at all.

So: could you revert just that one line back to Path(lowered).suffix, with a
short comment saying the upload path deliberately does not follow
dataset_suffix() until there is a decompressed-size guard?

history.py should keep dataset_suffix() — I thought about whether it
needed the same treatment and it does not. Those files are placed in the
datasets directory by whoever runs the dashboard, who could already drop a 50 GB
CSV there. The upload endpoint is the one taking input from someone who is not
the operator, and that is the only one where the cap means anything.

After that

That is the last thing. I will open a separate issue for the decompressed-size
guard on uploads and link it here — it is a dashboard problem that exists
independently of gzip, and it should not hold up a PR that closes #2 cleanly.

Push that one revert and I will approve the workflow runs so we both see CI
green, then merge. Thank you for this — the review turnaround was faster and
more thorough than I expected, and the two things you fixed that I did not ask
about are the reason this is in better shape than the issue described.

@jabrailkhalil

Copy link
Copy Markdown
Contributor Author

Implemented final blocker: kept upload dispatch on Path.suffix in datasemver_web/backend/main.py (with decompressed-size guard TODO comment), added regression test in tests/test_web.py, commit db1f7e9. Ready for your approval to proceed.

@IzanVil

IzanVil commented Sep 8, 2026

Copy link
Copy Markdown
Owner

This is ready. Merging it.

The last change is exactly the right size: the import gone, _suffix_of back on Path.suffix, and a comment that says why rather than what — which is the version of that comment that survives someone reading it in six months. Keeping dataset_suffix() in history.py was the half that was easy to get wrong, and you got it right.

I ran the full suite rather than a subset, and re-ran the measurement:

before this commit after
same 0.25 MB upload HTTP 200 HTTP 400
peak RSS 656 MB 142 MB
  • ruff check, ruff format, mypy — all pass
  • 565 passed, 1 skipped, up from 556 on main
  • Coverage 98.76%, unchanged
  • .csv.gz still loads through the library with the delimiter detected inside the compressed text, which was the whole point

The regression test you added is the part I appreciate most. It stops someone — including me — from "fixing" that line back without reading why it is there.

Two things I am taking off your hands

Your fix left SUPPORTED_EXTENSIONS and what the dashboard accepts as different sets, so /api/meta now advertises .csv.gz in the file picker and the upload endpoint then refuses it. That is a consequence of how I directed you, not of anything you did, and it belongs in the dashboard rather than in a PR that closes #2.

I would rather not move the goalposts a fourth time on a PR that has already answered three rounds of review properly.

Thank you

Three things here were better than what I asked for: deriving dataset_suffix() from SUPPORTED_EXTENSIONS instead of a hardcoded tuple, the stem calculation in history.py that I had not thought about, and replacing the hardcoded extension tuple in the PR script — which means it now analyses workbooks too.

This is a good first contribution to the project, and the review turnaround was faster and more careful than I had any right to expect. If you want another, #3 and #4 are open and neither is bigger than this was.