Skip to content

from csv/tsv: rename repeated header names instead of silently dropping data - #19595

Merged
fdncred merged 4 commits into
nushell:mainfrom
cemaliplt:fix/from-csv-duplicate-headers
Nov 18, 2026
Merged

fdncred merged 4 commits into
nushell:mainfrom
cemaliplt:fix/from-csv-duplicate-headers

Conversation

@cemaliplt

@cemaliplt cemaliplt commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #19537.

from csv / from tsv with 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:

"name,name\nAlice,Bob" | from csv | to csv
# name,name
# Bob,Bob

This PR renames repeated header names in from_delimited_stream (shared by from csv and from tsv): each repeat gets a _N suffix (name, name_1, name_2, ...). The suffix skips any name the header row already uses, so a real name_1 column keeps its name. Every column now loads:

"name,name\nAlice,Bob" | from csv | to nuon
# [[name, "name_1"]; [Alice, Bob]]

Blank header cells are now named by position (column2, column3, ...) before the rename, the same way headers names 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 Record or a shared helper in nu-protocol) is left as its own follow-up.

User-facing changes (Release notes)

  • from csv and from tsv now rename repeated header names with a _N suffix (name, name_1, ...) instead of silently keeping only the last value. The suffix skips names the header row already uses.
  • With --flexible, generated columnN names for extra fields no longer collide with a header column of the same name.
  • Blank header cells in from csv and from tsv are now named by position (column1, column2, ...), the same way headers names them. Before, every blank cell was named "".

Additional notes

  • Tests: new unit tests in from/csv.rs and from/tsv.rs; cargo test -p nu-command --lib -- from::csv from::tsv and cargo test -p nu-command --test tests -- csv tsv open pass; cargo fmt --all -- --check and cargo clippy -p nu-command --all-targets -- -D warnings -D clippy::unwrap_used are clean.
  • AI disclosure: the code was drafted with an AI coding agent (GLM-5.3 via OpenCode) and reviewed and tested by me; this description was also written with AI help.
  • Repeated headers were an error in the first version of this PR; switched to renaming after review.

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]>

@fdncred fdncred left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 High severity · 2 Medium severity · 2 Low severity

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]>
@cemaliplt

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review! I pushed ff18a98 with all four:

  • Blank header cells are now named by position (column2, column3), same as headers, before the duplicate check. A lone blank cell also becomes columnN now, as you said.
  • The header set is behind an Arc, so the per-row clone is cheap again.
  • The help text now suggests --noheaders | skip 1.
  • The help names both columns, e.g. "Columns 0 and 2 (counting from 0) are both named name". The labels still both point at the command since the data has no spans.

Added a test for the multiple-blank-headers case too.

@fdncred fdncred left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 High severity · 0 Medium severity · 1 Low severity

Open (1)

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.

@cemaliplt

Copy link
Copy Markdown
Contributor Author

Good catch, thanks. I added that line to the release notes in the PR description, and updated the --noheaders hint there to --noheaders | skip 1 so it matches the new help text.

@fdncred fdncred left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 High severity · 0 Medium severity · 0 Low severity

Open (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]>
@cemaliplt

Copy link
Copy Markdown
Contributor Author

Makes sense, renaming is friendlier than failing the whole file. I pushed 024b754 with your dedupe_headers in place of the check:

  • "name,name\nAlice,Bob" now gives [[name, name_1]; [Alice, Bob]], and a real name_1 column keeps its name (name,name,name_1 gives name, name_2, name_1).
  • The two test_duplicate_headers_error tests are now rename tests, plus one for the existing-suffix case and one with --flexible.
  • Updated the description and release notes to match.

csv/tsv unit tests (11), --test tests -- csv tsv open (195), fmt and clippy with CI's flags all pass locally.

@fdncred fdncred left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 High severity · 1 Medium severity · 1 Low severity

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]>
@cemaliplt cemaliplt changed the title from csv/tsv: error on duplicate header names instead of silently dropping data from csv/tsv: rename repeated header names instead of silently dropping data Nov 18, 2026
@cemaliplt

Copy link
Copy Markdown
Contributor Author

Thanks! Pushed 33a46df with your linear dedupe_headers. The 10,000-copy header now loads in about 0.15 s in a debug build here, and the names come out the same (name,name,name_1,name still gives name, name_2, name_1, name_3). Tests, fmt and clippy pass. I also changed the PR title to the one you suggested.

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!

@fdncred

fdncred commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

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.

@fdncred fdncred added notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes labels Nov 18, 2026
@fdncred
fdncred merged commit b85118c into nushell:main Nov 18, 2026
14 checks passed
@fdncred

fdncred commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Thanks

@github-actions github-actions Bot added this to the v0.118.0 milestone Nov 18, 2026
@cemaliplt

Copy link
Copy Markdown
Contributor Author

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.

fdncred pushed a commit that referenced this pull request Nov 20, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

from csv silently drops values when a header name repeats

2 participants