Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor drop columns to fix issues #10903

Merged
merged 13 commits into from Nov 9, 2023

Conversation

IanManske
Copy link
Member

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.

@IanManske IanManske marked this pull request as ready for review October 31, 2023 20:33
Copy link
Member

@sholderbach sholderbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the refactor!

I am not sure what the expectations of folks are for jagged tables, so would be interested what folks expect. (In most of those cases reject is probably preferrable for practical purposes)

Comment on lines +98 to +120
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
"
));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotta admit I find both of those behaviors absolutely cursed based on what the user sees with the default table output.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol, I agree. Perhaps we could:

  • drop the first columns? Then it would be skip columns instead of drop columns
  • compute the set of all columns (via nu_engine::column::get_columns). Unfortunately, this means we would have to collect the ListStream.

crates/nu-command/src/filters/drop/column.rs Outdated Show resolved Hide resolved
crates/nu-command/src/filters/drop/column.rs Outdated Show resolved Hide resolved
.input_output_types(vec![(Type::Table(vec![]), Type::Table(vec![]))])
.input_output_types(vec![
(Type::Table(vec![]), Type::Table(vec![])),
(Type::Record(vec![]), Type::Record(vec![])),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Good catch!

sholderbach added a commit to sholderbach/nushell that referenced this pull request Nov 8, 2023
Matches the general behavior of `Vec::drain` or
`indexmap::IndexMap::drain`:
- Drop the remaining elements (implementing the unstable `keep_rest()`
would not be compatible with something like `indexmap`)
- No `AsRef<[T]>` or `Drain::as_slice()` behavior as this would make
layout assumptions.
- `Drain: DoubleEndedIterator`

Found useful in nushell#10903
sholderbach added a commit that referenced this pull request Nov 8, 2023
Matches the general behavior of `Vec::drain` or
`indexmap::IndexMap::drain`:
- Drop the remaining elements (implementing the unstable `keep_rest()`
would not be compatible with something like `indexmap`)
- No `AsRef<[T]>` or `Drain::as_slice()` behavior as this would make
layout assumptions.
- `Drain: DoubleEndedIterator`

Found useful in #10903
sholderbach added a commit to sholderbach/nushell that referenced this pull request Nov 8, 2023
Compatible with `Vec::truncate` and `indexmap::IndexMap::truncate`

Found useful in nushell#10903 for `drop column`
sholderbach added a commit that referenced this pull request Nov 8, 2023
# Description
Compatible with `Vec::truncate` and `indexmap::IndexMap::truncate`

Found useful in #10903 for `drop column`

# Tests + Formatting
Doctest with the relevant edge-cases
Copy link
Member

@sholderbach sholderbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rerunning the failed job.

I think I don't want to block this based on the behavior on tables with holes for now.
Great that this PR unlocks records and moves us to the sane API.

We can close #6846 with this and keep #10902 open I think.

I think I would like to bias us to stream by default so we may have to live with the weird behavior. We could consider adding a flag that collects the table so you can get sane truncation for tables with sparse columns.

@sholderbach sholderbach merged commit 33a7bc4 into nushell:main Nov 9, 2023
19 checks passed
@IanManske IanManske deleted the drop-column-refactor branch November 10, 2023 17:12
hardfau1t pushed a commit to hardfau1t/nushell that referenced this pull request Dec 14, 2023
Matches the general behavior of `Vec::drain` or
`indexmap::IndexMap::drain`:
- Drop the remaining elements (implementing the unstable `keep_rest()`
would not be compatible with something like `indexmap`)
- No `AsRef<[T]>` or `Drain::as_slice()` behavior as this would make
layout assumptions.
- `Drain: DoubleEndedIterator`

Found useful in nushell#10903
hardfau1t pushed a commit to hardfau1t/nushell that referenced this pull request Dec 14, 2023
# Description
Compatible with `Vec::truncate` and `indexmap::IndexMap::truncate`

Found useful in nushell#10903 for `drop column`

# Tests + Formatting
Doctest with the relevant edge-cases
hardfau1t pushed a commit to hardfau1t/nushell that referenced this pull request Dec 14, 2023
# Description
This PR refactors `drop columns` and fixes issues nushell#10902 and nushell#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.
dmatos2012 pushed a commit to dmatos2012/nushell that referenced this pull request Feb 20, 2024
Matches the general behavior of `Vec::drain` or
`indexmap::IndexMap::drain`:
- Drop the remaining elements (implementing the unstable `keep_rest()`
would not be compatible with something like `indexmap`)
- No `AsRef<[T]>` or `Drain::as_slice()` behavior as this would make
layout assumptions.
- `Drain: DoubleEndedIterator`

Found useful in nushell#10903
dmatos2012 pushed a commit to dmatos2012/nushell that referenced this pull request Feb 20, 2024
# Description
Compatible with `Vec::truncate` and `indexmap::IndexMap::truncate`

Found useful in nushell#10903 for `drop column`

# Tests + Formatting
Doctest with the relevant edge-cases
dmatos2012 pushed a commit to dmatos2012/nushell that referenced this pull request Feb 20, 2024
# Description
This PR refactors `drop columns` and fixes issues nushell#10902 and nushell#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants