Skip to content

headers: rename repeated column names instead of creating duplicates - #19619

Merged
fdncred merged 3 commits into
nushell:mainfrom
cemaliplt:headers-unique-columns
Nov 20, 2026
Merged

fdncred merged 3 commits into
nushell:mainfrom
cemaliplt:headers-unique-columns

Conversation

@cemaliplt

@cemaliplt cemaliplt commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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:

[[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:

[[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.

Cem Ali Polat added 2 commits November 20, 2026 14:59
`headers` produced records with duplicate columns when the first row
repeated a value (e.g. `[[a b]; [x x] [1 2]]`). Reuse the `dedupe_headers`
helper from `from csv`/`from tsv` (nushell#19595) so repeats become `x`, `x_1`, ...
The names are computed once for the header row. Fixes nushell#11020 for `headers`.
Cover int/float header names, a null name colliding with a literal column0, reordered and missing columns mapped by name, and reaching a renamed column by name.

@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

🟢 Minor suggestions

The renaming works on every case I tried on 770401b: repeats, a header row that already has x_1, blank and null cells, int, float and bool names, and rows with reordered or missing columns. Two nits left, the release note and a stale doc comment.

Findings: 0 High severity · 0 Medium severity · 2 Low severity

Open (2)

Notes from my clanker, checked on 770401b. Built the branch and ran each case on the branch binary and on main (651f0e4). CI's root and lint jobs already ran the tests, fmt and clippy on this head and typos passed, so I didn't rerun them.

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

fdncred commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Thanks

@github-actions github-actions Bot added this to the v0.118.0 milestone Nov 20, 2026
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.

rename silently creates columns with non-unique names

2 participants