Repository navigation
Description
Describe the bug
rename currently still allows you to create records (and thus tables) where a column name appears more than once.
The display through table will display only the last column for a table but will show all elements with the same key for a record.
How to reproduce
ls | rename a a a aOR
ls | rename --column {name: a, type: a, size: a, modified: a}╭─#──┬──────a───────╮
│ 0 │ a year ago │
│ 1 │ 3 weeks ago │
│ 2 │ an hour ago │
│ 3 │ a day ago │
and then to see that the record kept all columns
ls | rename a a a a | get 0╭───┬────────────────────╮
│ a │ CODE_OF_CONDUCT.md │
│ a │ file │
│ a │ 3.4 KB │
│ a │ a year ago │
╰───┴────────────────────╯
For extra fun:
ls | rename --block { "a" }Expected behavior
Records and tables should have unique column names so they can be cleanly addressed by cell paths and internal operations can make safe assumptions about the column names.
Open for debate:
Should rename always error either up front or when trying to write the new record:
e.g. having hetoregeneous record that should be converted to a common schema:
[
{last_name: "Curie"},
{surname: "Lovelace"},
{family_name: "Hopper"},
] | rename --columns {surname: last_name, family_name: last_name} Also we could have modes that allow you to either keep the first or last column getting assigned the non-unique name. (This may be of concern for the closure case)
Another option would be to implicitly add a suffix following a particular schema.
Configuration
| key | value |
|---|---|
| version | 0.86.0 |
| branch | |
| commit_hash | |
| build_os | linux-x86_64 |
| build_target | x86_64-unknown-linux-gnu |
| rust_version | rustc 1.71.1 (eb26296b5 2023-08-03) |
| rust_channel | 1.71.1-x86_64-unknown-linux-gnu |
| cargo_version | cargo 1.71.1 (7f1d04c00 2023-07-29) |
| build_time | 2023-10-18 13:38:18 +02:00 |
| build_rust_channel | release |
| allocator | mimalloc |
| features | default, sqlite, trash, which, zip |
| installed_plugins |
Activity
IanManske commentedon Nov 10, 2023
Looking over the other commands, I think
headersalso has this problem.amtoine commentedon Nov 10, 2023
my two cents: it should always throw an error rather than assume things and do things silently on behalf of the user 🤔
otherwise it looks like Python doing conversions all over the place for you so that you get the least amount of errors...
i like errors 😋
sholderbach commentedon Nov 13, 2023
@amtoine erroring ahead of time or when the specific rename causes a problem?
See the example using the latter behavior.
We practically have to implement something to error out at run time so we can deal with the closure case.
amtoine commentedon Nov 13, 2023
i'd say, if we know the shape of the data at parse-time, e.g.
lsis atable<name: string, type: string, size: filesize, modified: date>, we now at parse-time thatrename a aorrename {name: a, size: a}does not make sense and it probably a mistake => it should error out at parse-time imowhen we either don't know the shape of the input data or
renameoperated on a closure, then an error at runtime could be the only thing we can do, right? 🤔do you mean this?
[ {last_name: "Curie"}, {surname: "Lovelace"}, {family_name: "Hopper"}, ] | rename --columns {surname: last_name, family_name: last_name}i'm not even sure what to expect there, merging the
$.surnameand$.family_namecolumns?not a fan of that
Lock out new direct construction of `Record`
Lock out new direct construction of `Record` (#11414)
Lock out new direct construction of `Record` (nushell#11414)
kronberger-droid commentedon Nov 14, 2026
renamerefuses duplicate names since #19196.headersstill creates them on 0.117:[[a b]; [x x] [1 2]] | headers | to nuongives[{x: 1, x: 2}].headers: rename repeated column names instead of creating duplicates (n…