Skip to content

Refactor Value cell path functions to fix bugs - #11066

Merged
sholderbach merged 13 commits into
nushell:mainfrom
IanManske:value-cellpath-refactor
Nov 19, 2023
Merged

sholderbach merged 13 commits into
nushell:mainfrom
IanManske:value-cellpath-refactor

Conversation

@IanManske

Copy link
Copy Markdown
Member

Description

Slightly refactors the cell path functions (insert_data_at_cell_path, etc.) for Value to fix a few bugs and ensure consistent behavior. Namely, case (in)sensitivity now applies to lazy records just like it does for regular Records. Also, the insert behavior of insert and upsert now match, alongside fixing a few related bugs described below. Otherwise, a few places were changed to use the Record API.

Tests

Added tests for two bugs:

  • {a: {}} | insert a.b.c 0: before this PR, doesn't create the innermost record c.
  • {table: [[col]; [{a: 1}], [{a: 1}]]} | insert table.col.b 2: before this PR, doesn't add the field b: 2 to each row.

@fdncred

fdncred commented Nov 15, 2023

Copy link
Copy Markdown
Contributor

Nushell has always had problem with updating deeply nested data. I'm hopeful this will fix that, or at least be one good step to fixing it.

Thanks! I'm always excited to see your PRs!!!

@sholderbach sholderbach left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for tackling this!

Great to see more idiomatic rust, the nested insert/assignment etc.

Will mark this as a potentially breaking change as we might still run into some records with duplicated column names.

Comment thread crates/nu-protocol/src/value/mod.rs Resolved
}
}) {
current = found.1.clone();
current = found.1.clone(); // TODO: avoid clone here

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is .into_iter() not an option here?

The existing code is still defensive around finding more than one entry with the same column name. in the future we can probably replace that all with Record::remove()

Edit: As the upsert/update logic makes the assumptions that the first hit is the true hit, you could go for that and we see what starts to burn 😆

@IanManske IanManske Nov 15, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Unfortunately, the records columns are used below in did_you_mean(val.columns(), column_name), so we can't use into_iter here without cloning.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah then an if let Some(val) = record.remove(col) magic could be the workaround, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

There's a predicate for the case insensitivity. So, maybe index_of followed by swap_remove?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Grr sloppy insensitive. In my view that should have no business in any cell-path access but I digress.

Yeah sounds good. Adding remove_index/swap_remove_index to record should be trivial.

Comment thread crates/nu-protocol/src/value/mod.rs Resolved
) -> Result<(), ShellError> {
match cell_path.first() {
Some(path_member) => match path_member {
if let Some((member, path)) = cell_path.split_first() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Smooth!

}
_ => {
let path_member = cell_path.first().expect("there is a first");
[member, path @ ..] => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Chef's kiss! Love how this is both explicit and gets rid of a bunch of triple checking

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Pattern matching is too powerful! 😄

Comment thread crates/nu-protocol/src/value/mod.rs Resolved
@sholderbach sholderbach linked an issue Nov 18, 2023 that may be closed by this pull request

@sholderbach sholderbach left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's get this one in and look for the remaining warts around duplicated column names.

The clone elision with swap_remove etc. we can get done separately pretty soon.

@sholderbach
sholderbach merged commit 12effd9 into nushell:main Nov 19, 2023
@fdncred

fdncred commented Nov 19, 2023

Copy link
Copy Markdown
Contributor

Seems like some of these new things work but this one doesn't. Not sure if that is expected or a bug?

❯ {z: 0} | insert z.z 0
Error: nu::shell::unsupported_input

  × Unsupported input
   ╭─[entry #7:1:1]
 1 │ {z: 0} | insert z.z 0
   ·          ───┬──   ┬
   ·             │     ╰── input type: Int
   ·             ╰── table or record
   ╰────

@amtoine

amtoine commented Nov 20, 2023

Copy link
Copy Markdown
Member

this was one of the examples of the issue that has been closed as completed 😮

@sholderbach

Copy link
Copy Markdown
Member

The insert contract is that it doesn't overwrite any preexisting value. And to add this nested path you need to replace the preexisting int with a record. So this example should never run but the error message is certainly not there yet.

@IanManske
IanManske deleted the value-cellpath-refactor branch November 23, 2023 15:39
hardfau1t pushed a commit to hardfau1t/nushell that referenced this pull request Dec 14, 2023
# Description
Slightly refactors the cell path functions (`insert_data_at_cell_path`,
etc.) for `Value` to fix a few bugs and ensure consistent behavior.
Namely, case (in)sensitivity now applies to lazy records just like it
does for regular `Records`. Also, the insert behavior of `insert` and
`upsert` now match, alongside fixing a few related bugs described below.
Otherwise, a few places were changed to use the `Record` API.

# Tests
Added tests for two bugs:
- `{a: {}} | insert a.b.c 0`: before this PR, doesn't create the
innermost record `c`.
- `{table: [[col]; [{a: 1}], [{a: 1}]]} | insert table.col.b 2`: before
this PR, doesn't add the field `b: 2` to each row.
dmatos2012 pushed a commit to dmatos2012/nushell that referenced this pull request Feb 20, 2024
# Description
Slightly refactors the cell path functions (`insert_data_at_cell_path`,
etc.) for `Value` to fix a few bugs and ensure consistent behavior.
Namely, case (in)sensitivity now applies to lazy records just like it
does for regular `Records`. Also, the insert behavior of `insert` and
`upsert` now match, alongside fixing a few related bugs described below.
Otherwise, a few places were changed to use the `Record` API.

# Tests
Added tests for two bugs:
- `{a: {}} | insert a.b.c 0`: before this PR, doesn't create the
innermost record `c`.
- `{table: [[col]; [{a: 1}], [{a: 1}]]} | insert table.col.b 2`: before
this PR, doesn't add the field `b: 2` to each row.
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.

Nested insert doesn't work

4 participants