Repository navigation
from csv/tsv: rename repeated header names instead of silently dropping data - #19595
Conversation
A repeated header name built a record with duplicate keys, and every later name-based lookup (get, to csv, table) silently used the last one. Follow rename/split column and return ColumnDefinedTwice, pointing to --noheaders. Also keep --flexible filler names (columnN) from reusing a name the header already has. Fixes nushell#19537 Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Review overview
🟡 Changes recommended
Repeated header names now error, and the --flexible filler names skip names the header already uses. Both work on the branch. Two things to fix before merge: CSV exports with more than one blank header cell no longer open, and every row now clones the header set, which made a 200-column file parse about 30% slower in a debug build.
Findings: 0
Open (4)
Notes from my clanker, checked on c461eb7. Built the branch and ran each case on it and on 0.117.1 (651f0e4). Timed the per-row clone with a trial Arc patch on debug builds. CI is green on this head (clippy and tests on Linux, macOS and Windows), so I didn't rerun the tests locally.
- Name blank header cells by position (columnN), like `headers`, so files with several unlabeled columns still load. - Share the header set behind an Arc so cloning `columns` per row is cheap. - Help text: suggest `--noheaders | skip 1` and name the two column indices. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks for the detailed review! I pushed ff18a98 with all four:
Added a test for the multiple-blank-headers case too. |
There was a problem hiding this comment.
Review overview
🟡 Changes recommended
All four threads from my first review are fixed on ff18a98, and I resolved them. One new low below: the release notes don't mention that blank header cells get new names.
Findings: 0
Notes from my clanker, checked on ff18a98. Built the branch and reran the earlier probe plus new cases against main (651f0e4). cargo test -p nu-command --lib -- formats::from::csv formats::from::tsv (9 passed), cargo test -p nu-command --test tests -- csv tsv open (195 passed, 3 ignored), clippy on nu-command with CI's lint flags, fmt and typos are all clean. Timed open wide.csv | length (200 columns x 20k rows, debug builds, idle CPU, median of 3): 819 ms on the PR base, 826 ms on ff18a98. CI on this head is waiting for approval.
|
Good catch, thanks. I added that line to the release notes in the PR description, and updated the |
There was a problem hiding this comment.
Review overview
🟡 Changes recommended
An answer to the error-vs-rename question from the PR body and #19537: I'd rather rename repeated headers.
Findings: 1
Notes from my clanker, checked on ff18a98. Built the rename below on top of ff18a98 and ran each case on it and on main (651f0e4). Clippy with CI's lint flags and fmt are clean on it; of the from csv/from tsv unit tests, only the two test_duplicate_headers_error tests fail, as expected.
Repeated header names now get a _N suffix (name, name_1, ...) that skips names the header row already uses, so every column loads instead of the file failing to open. Tests updated for the rename. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Makes sense, renaming is friendlier than failing the whole file. I pushed 024b754 with your
csv/tsv unit tests (11), |
There was a problem hiding this comment.
Review overview
🟡 Changes recommended
Both open threads are fixed on 024b754, and I resolved them. One new medium below, in the dedupe_headers I suggested, and one new low about the PR title.
Findings: 0
Open (2)
Notes from my clanker, checked on 024b754. Built the branch and reran the earlier probes plus new rename cases against main (651f0e4). cargo test -p nu-command --lib -- formats::from::csv formats::from::tsv (11 passed), cargo test -p nu-command --test tests -- csv tsv open (195 passed, 3 ignored), clippy on nu-command with CI's lint flags, fmt and typos are all clean. Timings are single runs; the release ones come from the head's dedupe_headers copied into a standalone crate. CI on this head is waiting for approval.
Restarting the suffix search at _1 for every repeat made k copies of one name cost about k^2/2 probes (10,000 copies took ~13 s in a debug build, now 0.15 s). Output names are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks! Pushed 33a46df with your linear One small request: if you notice anything else, could you send it all in one review? I'm happy to fix everything, it's just easier for me to handle it in one go. Thanks again for all the careful testing! |
|
The way it works is i review a pr and then recheck it to see if there was anything missed. if there is, it's a new review posting new findings. There are still a few minor things but we can skip them for now and see if they bubble up as issues later. |
|
Thanks |
|
That makes sense, thanks for explaining. I really appreciate how quickly and carefully you've reviewed this. Doing it all in one go is just a bit easier for me, but if this is how you work, that's no problem at all. I only wanted to ask politely. If any of the remaining minor things turn up as issues later, or anything new comes up, I'd be glad to help with those too. |
…19619) ## Description Fixes #11020. If the first row repeats a value, `headers` built a record with two columns of the same name. A name-based lookup then sees only one of them, so the other value is silently lost: ```nu [[a b]; [x x] [1 2]] | headers | get x_1 # before: Error: nu::shell::column_not_found # after: [2] ``` With non-string headers the old build even drops a column: ```nu [[a b c]; [1 1.0 1] [1 2 3]] | headers | columns # before: ["1", "1.0"] # after: ["1", "1.0", "1_1"] ``` `headers` now renames repeats with a `_N` suffix (`x`, `x_1`, `x_2`), the same way `from csv` / `from tsv` do since #19595. I reused the `dedupe_headers` helper from that PR (made `pub(crate)`, no new algorithm), so the two commands behave the same: - A name the header row already uses keeps it: `[x x x_1]` gives `x, x_2, x_1`. - Blank and null cells are still named by position (`column0`, ...) first, then deduped, so a literal `column0` next to a blank becomes `column0_1`. - The names are computed once for the header row. Rows with reordered or missing columns still map by name: `[{a: x, b: x}, {b: 2, a: 1}, {a: 5}] | headers` gives `[{x_1: 2, x: 1}, {x: 5}]`. `rename` is unchanged on purpose: there the user types the names, so a repeat is most likely a mistake and an error is the right answer. The `rename` part of #11020 was already handled in #19196; this PR covers the remaining `headers` part. ## User-facing changes (Release notes) - `headers` no longer creates duplicate column names. Repeated names in the first row get a `_N` suffix (`x`, `x_1`, ...), so column names can change from `x, x` to `x, x_1`. This matches `from csv` and `from tsv`. - When a name repeats, `x` now holds the first column's value and the later ones become `x_1`, `x_2`. Before, `get x` returned the last one, so a script that reads `headers | get x` on such a table now gets a different value without an error. A blank cell named by position flips the same way: `[[a b]; [column1 ''] [1 2]] | headers | get column1` gave `[2]` and now gives `[1]`. ## Additional notes - Tests: 8 new `nu!` tests in `crates/nu-command/tests/commands/headers.rs` (repeats, existing `_1` suffix, blank and null names colliding with `column0`, int/float names, reordered and missing columns, reaching the renamed column by name) and an `examples()` entry. I checked every expected output against a build of main, and main gives the wrong result for each. - Run on my machine: `cargo test -p nu-command --test tests -- headers` gives `60 passed; 0 failed`; `cargo test -p nu-command --lib -- headers from::csv from::tsv` passes; `cargo fmt --all --check` and `cargo clippy -p nu-command --all-targets -- -D warnings -D clippy::unwrap_used` are clean. - Performance (debug builds, 20000 rows x 200 columns with 50 distinct header names, `headers | length` in `timeit`): main 5.81 s / 5.76 s / 5.71 s, this PR 5.73 s / 6.02 s. Within noise; the dedupe runs once per call. Most of that time is the existing per-cell `position()` lookup in `replace_headers`, which I left alone. - I used AI assistance (Claude Code) while writing the code, the tests and this description. I read the diff, understand how it works, and checked the before/after outputs above myself; the tests listed above ran on my machine. --------- Co-authored-by: Cem Ali Polat <[email protected]>



Description
Fixes #19537.
from csv/from tsvwith a repeated header name built a record with duplicate keys. Every later name-based lookup (get,to csv, table rendering) then resolved the name to the last entry, so the earlier column was silently lost:This PR renames repeated header names in
from_delimited_stream(shared byfrom csvandfrom tsv): each repeat gets a_Nsuffix (name,name_1,name_2, ...). The suffix skips any name the header row already uses, so a realname_1column keeps its name. Every column now loads:Blank header cells are now named by position (
column2,column3, ...) before the rename, the same wayheadersnames them, so files with several unlabeled columns still open.It also fixes a related case: with
--flexible, the generated filler names (columnN) for extra fields could collide with a header that is already called e.g.column2. Filler names now skip names the header already uses.This is the narrow fix for the CSV/TSV path. The broader idea from the #19362 review (a checked push on
Recordor a shared helper innu-protocol) is left as its own follow-up.User-facing changes (Release notes)
from csvandfrom tsvnow rename repeated header names with a_Nsuffix (name,name_1, ...) instead of silently keeping only the last value. The suffix skips names the header row already uses.--flexible, generatedcolumnNnames for extra fields no longer collide with a header column of the same name.from csvandfrom tsvare now named by position (column1,column2, ...), the same wayheadersnames them. Before, every blank cell was named"".Additional notes
from/csv.rsandfrom/tsv.rs;cargo test -p nu-command --lib -- from::csv from::tsvandcargo test -p nu-command --test tests -- csv tsv openpass;cargo fmt --all -- --checkandcargo clippy -p nu-command --all-targets -- -D warnings -D clippy::unwrap_usedare clean.