Repository navigation
headers: rename repeated column names instead of creating duplicates - #19619
Merged
Merged
Conversation
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
reviewed
Nov 20, 2026
Contributor
There was a problem hiding this comment.
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
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.
Contributor
|
Thanks |
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.



Description
Fixes #11020.
If the first row repeats a value,
headersbuilt 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:With non-string headers the old build even drops a column:
headersnow renames repeats with a_Nsuffix (x,x_1,x_2), the same wayfrom csv/from tsvdo since #19595. I reused thededupe_headershelper from that PR (madepub(crate), no new algorithm), so the two commands behave the same:[x x x_1]givesx, x_2, x_1.column0, ...) first, then deduped, so a literalcolumn0next to a blank becomescolumn0_1.[{a: x, b: x}, {b: 2, a: 1}, {a: 5}] | headersgives[{x_1: 2, x: 1}, {x: 5}].renameis unchanged on purpose: there the user types the names, so a repeat is most likely a mistake and an error is the right answer. Therenamepart of #11020 was already handled in #19196; this PR covers the remainingheaderspart.User-facing changes (Release notes)
headersno longer creates duplicate column names. Repeated names in the first row get a_Nsuffix (x,x_1, ...), so column names can change fromx, xtox, x_1. This matchesfrom csvandfrom tsv.xnow holds the first column's value and the later ones becomex_1,x_2. Before,get xreturned the last one, so a script that readsheaders | get xon 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 column1gave[2]and now gives[1].Additional notes
nu!tests incrates/nu-command/tests/commands/headers.rs(repeats, existing_1suffix, blank and null names colliding withcolumn0, int/float names, reordered and missing columns, reaching the renamed column by name) and anexamples()entry. I checked every expected output against a build of main, and main gives the wrong result for each.cargo test -p nu-command --test tests -- headersgives60 passed; 0 failed;cargo test -p nu-command --lib -- headers from::csv from::tsvpasses;cargo fmt --all --checkandcargo clippy -p nu-command --all-targets -- -D warnings -D clippy::unwrap_usedare clean.headers | lengthintimeit): 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-cellposition()lookup inreplace_headers, which I left alone.