Refactor drop columns to fix issues (#10903)

# Description
This PR refactors `drop columns` and fixes issues #10902 and #6846.
Tables with "holes" are now handled consistently, although still
somewhat awkwardly. That is, the columns in the first row are used to
determine which columns to drop, meaning that the columns displayed all
the way to the right by `table` may not be the columns actually being
dropped. For example, `[{a: 1}, {b: 2}] | drop column` will drop column
`a` instead of `b`. Before, this would give a list of empty records.

# User-Facing Changes
`drop columns` can now take records as input.
This commit is contained in:
Ian Manske
2023-11-09 12:51:46 +00:00
committed by GitHub
parent cd75640a90
commit 33a7bc405f
2 changed files with 152 additions and 111 deletions

View File

@ -93,3 +93,45 @@ fn fail_on_non_iterator() {
assert!(actual.err.contains("command doesn't support"));
}
#[test]
fn disjoint_columns_first_row_becomes_empty() {
let actual = nu!(pipeline(
"
[{a: 1}, {b: 2, c: 3}]
| drop column
| columns
| to nuon
"
));
assert_eq!(actual.out, "[b, c]");
}
#[test]
fn disjoint_columns() {
let actual = nu!(pipeline(
"
[{a: 1, b: 2}, {c: 3}]
| drop column
| columns
| to nuon
"
));
assert_eq!(actual.out, "[a, c]");
}
#[test]
fn record() {
let actual = nu!("{a: 1, b: 2} | drop column | to nuon");
assert_eq!(actual.out, "{a: 1}");
}
#[test]
fn more_columns_than_record_has() {
let actual = nu!("{a: 1, b: 2} | drop column 3 | to nuon");
assert_eq!(actual.out, "{}");
}