Skip to content

Don't allow duplicate column names in Rename - #19196

Merged
fdncred merged 1 commit into
nushell:mainfrom
ymcx:fix-rename-duplicate-column-name
Oct 9, 2026
Merged

fdncred merged 1 commit into
nushell:mainfrom
ymcx:fix-rename-duplicate-column-name

Conversation

@ymcx

@ymcx ymcx commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Description

Rename allows renaming columns of a table with duplicate names, even though duplicate column names are disallowed.

I fixed this by checking the column names for duplicates using a HashSet. Otherwise valid records are discarded and a nu::shell::column_defined_twice error is thrown if there are duplicates.

User-facing changes (Release notes)

Fixed an issue where rename allowed setting duplicate column names on a table

Additional notes

Closes #19195

Copilot AI 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.

🟢 Approval recommended

The centralized validation correctly covers rename results, with focused regression tests for the reported cases.

0 open findings

What changed in this PR

Prevents rename from producing records with duplicate column names.

Changes:

  • Validates renamed records using a HashSet.
  • Returns nu::shell::column_defined_twice for collisions.
  • Adds regression tests for duplicate names.
File Description
crates/​nu-command/​src/​filters/​rename.rs Adds duplicate-column validation and tests.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@fdncred
fdncred merged commit 04a0444 into nushell:main Oct 9, 2026
12 checks passed
@fdncred

fdncred commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Thanks

@fdncred fdncred added the notes:fixes Noted in "Bug fixes" section label Oct 9, 2026
@github-actions github-actions Bot added this to the 1.0 milestone Oct 9, 2026
@fdncred fdncred added the notes:ready Indicates Ready for Release notes label Oct 9, 2026
@weirdan

weirdan commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

I'm curious, how does this work with streams?

@ymcx

ymcx commented Oct 10, 2026

Copy link
Copy Markdown
Contributor Author

I'm curious, how does this work with streams?

Could you be a little more specific? I mean, it's essentially the output of the rename that we're validating here, so it should work in all occasions independent of how it got its input.

@weirdan

weirdan commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Answering my own question, the error is emitted as part of the output stream and is raised when the corresponding element is accessed (or never, if the element is discarded):

[
  {a: 1}
  {a: 1, x: 2} # will generate a duplicate
  {a: 10, c: 20}
]
| generate {|elt, acc=true| 
  $elt | wrap out | insert next { sleep 100ms; true }
}
| rename x
| last # {x: 10, c: 20}

@hustcer hustcer modified the milestones: 1.0, v0.117.0 Oct 31, 2026
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.

Rename allows setting duplicate column names

5 participants