Repository navigation
Add unsparsify --from-header to fill ragged short rows from the input header - #2308
Open
Assertive-xxh wants to merge 1 commit into
Open
Assertive-xxh wants to merge 1 commit into
Assertive-xxh wants to merge 1 commit into
Conversation
…ut header The CSV reader has left ragged short rows short since johnkerl#1428, and unsparsify could not see the input header, so the header's trailing columns were lost when the first data line was short. See issue johnkerl#2286. The CSV, CSV-lite, TSV, PPRINT, and markdown readers now put the header's field names into the record context, and `unsparsify --from-header` fills missing keys from there, streaming. Also correct the `--ragged` help text and record-heterogeneity.md, which still said that short rows are filled for CSV. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This branch has not been deployed
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.
Fixes #2286.
Since #1428 the CSV reader leaves ragged short rows short, and filling them is left to
unsparsify. Butunsparsifycan't see the input header: without-fit only knows the keys that occur in some record, and-f a,b,cneeds the names typed out. So when the first data line is the short one, the trailing columns of the header are lost:This adds
unsparsify --from-header, which the issue suggests as one way out:That is the output you gave for this input in #1418. The option is opt-in: what
--raggeddoes without it is unchanged, as decided in #1428.How it works: the CSV, CSV-lite, TSV, PPRINT, and markdown readers put the field names of the header line into the record context (
Context.HeaderFieldNames), the same way the JSON reader passesJSONHadBracketsto the JSONL writer.unsparsify --from-headerfills the missing keys from there. It is streaming, and it can be combined with-fand--fill-with. The names are stored after--dedupe-field-namesrenaming, so they match the record keys. Input formats without a header line are left as they are.The slice is built once per header line, not per record.
mlr --icsv --ojson caton a 2M-line, 92 MB CSV took the same time before and after (medians of 1428 ms and 1424 ms over 10 interleaved runs, Windows amd64).I also corrected the
--raggedhelp text andrecord-heterogeneity.md, which still said that short rows are filled for CSV.Tests:
test/cases/verb-unsparsify-from-header/: the input from CSV to JSONL: wrong conversion? #1418 above, the four-column input from the issue,--implicit-csv-header, several files with different headers,--fill-with,-ftogether with--from-header, repeated header names, JSON input, and TSV, CSV-lite, PPRINT, and markdown input.PutReferenceMaybeDedupeproduces.go test ./pkg/...passes.golangci-lintv2.12.2 is clean withGOOS=linux.mlr regtestpasses on Linux (WSL, amd64): 4833 cases. On Windows under Git Bash 62 cases fail, the same number and in the same 11 case directories as onmain.Two things about the generated files. My groff is 1.23 and re-wraps all of
manpage.txt, so I kept your layout and put the two changed blocks in by hand;mlr.1is generated. And I could not regenerate three of the docs pages here (they needcreachand similar), but they don't show this help text. You may want to re-runmake dev.The option name and whether it should combine with
-fare yours to decide; I'm happy to change either.Written with AI assistance (Claude); I have reviewed the change.
🤖 Generated with Claude Code