Repository navigation
Fix update with closure on nested list - #15789
WindSoilder wants to merge 12 commits into
Conversation
|
I was trying to see if there was any edge cases and found these: The bug persists if the outermost value is a list: {w: {x: [{y: "1"} {y: "2"}]}} | update w.x.y { into float } | to yaml
w:
x:
- y: 1.0
- y: 2.0
[{w: {x: [{y: "1"} {y: "2"}]}}] | update w.x.y { into float } | to yaml
- w:
x:
- - y:
- 1.0
- 2.0
- y:
- 1.0
- 2.0Minimal reproduction: [ [[a]; [1] [2]] ] | update a {into float} | to yaml
- - a:
- 1.0
- 2.0
- a:
- 1.0
- 2.0While this is not a commonly encountered use case, it might end up happening library code: Before: [[a]; [1] [2]] | update $. {|| } | to yaml
- a: 1
- a: 2After: [[a]; [1] [2]] | update $. {|| } | to yaml
Error: × cell path can't be empty
help: This should not happened, please file an issue |
|
Thanks! I didn't think about this, so if outermost value is a list, then follow_cell_path always returns a list too: It's necessary to handle this seperately. |
|
Ok, to handle with nested lists, the implementation tends to be complicated. |
|
@WindSoilder sorry for taking a little too long to review this. For some reason the code kept bothering me and I couldn't understand why. I felt like it was working around the problem rather than solving it. Playing around with issue more, I think the problem wasn't in ;; mut val = [{w: {x: [{y: "1"} {y: "2"}]}}, {w: {x: [{y: "3"} {y: "4"}]}}]
;; $val | to yaml
- w:
x:
- y: '1'
- y: '2'
- w:
x:
- y: '3'
- y: '4'
;; $val.w.x | to yaml
- - y: b
- y: b
- - y: b
- y: b
# Can't read this path
;; $val.w.x.y | to yaml
Error: nu::shell::column_not_found
× Cannot find column 'y'
╭─[entry #34:1:20]
1 │ mut val = [{w: {x: [{y: "1"} {y: "2"}]}}, {w: {x: [{y: "3"} {y: "4"}]}}]
· ─────────┬─────────
· ╰── value originates here
╰────
╭─[entry #37:1:10]
1 │ $val.w.x.y
· ┬
· ╰── cannot find column 'y'
╰────
# But can assign to it?
;; $val.w.x.y = "WHAT"
# Still can't read it
;; $val.w.x.y | to yaml
Error: nu::shell::column_not_found
× Cannot find column 'y'
...
# Let's see what the whole value looks like
;; $val | to yaml
- w:
x:
- y: WHAT
- y: WHAT
- w:
x:
- y: WHAT
- y: WHATI'll try to see if I can find the root cause of this problem, and if it can be solved all at once. |
Sure I understand the feel, because I feel the same :-( I think the reason might lay inside
Thank you for looking into this. |
|
I keep finding more and more edge cases 😔
all cause different behaviours 😭 edit: |
|
I'm going to close this pr, because it contains too much eage cases, and I don't think it solves the problem in right way |
|
I took a swing at fixing this here #18205. Please review and let me know if it fixes the issue. |
<!-- Thanks for contributing to Nushell! Before submitting, please read the contributing guide: https://github.com/nushell/nushell/blob/main/CONTRIBUTING.md This template helps reviewers understand your changes and allows us to generate high-quality release notes. --> ## Description This fixes a bug where update with a nested column path and closure (for example rss_item.pubDate) could evaluate the closure against a projected list and write the same result back to every row. - Switched nested closure updates to a recursive per-cell traversal so closures run once per targeted leaf value. - Preserved existing behavior for index-first paths (for example update 0.a with a closure). <!-- Explain what this PR does and why. This section is intentionally flexible: - Describe the problem - Explain your approach - Include technical details if relevant Good examples: - "In this PR, I fixed..." - "In this PR, I added support for..." - "This change improves X by..." Write as much or as little as needed for reviewers to understand your changes. --> ## User-facing changes (Release notes) This fixes a bug where update with a nested column path and closure (for example rss_item.pubDate) could evaluate the closure against a projected list and write the same result back to every row. ### Before ```nushell { rss_item: [ [ pubDate]; [1773429600], [1774325700], [1775448000] ] } | update rss_item.pubDate { into datetime -f "%s" } | table -e ╭──────────┬──────────────────────────────╮ │ │ ╭─#─┬───────pubDate────────╮ │ │ rss_item │ │ 0 │ ╭───┬──────────────╮ │ │ │ │ │ │ │ 0 │ 2 months ago │ │ │ │ │ │ │ │ 1 │ 2 months ago │ │ │ │ │ │ │ │ 2 │ a month ago │ │ │ │ │ │ │ ╰───┴──────────────╯ │ │ │ │ │ 1 │ ╭───┬──────────────╮ │ │ │ │ │ │ │ 0 │ 2 months ago │ │ │ │ │ │ │ │ 1 │ 2 months ago │ │ │ │ │ │ │ │ 2 │ a month ago │ │ │ │ │ │ │ ╰───┴──────────────╯ │ │ │ │ │ 2 │ ╭───┬──────────────╮ │ │ │ │ │ │ │ 0 │ 2 months ago │ │ │ │ │ │ │ │ 1 │ 2 months ago │ │ │ │ │ │ │ │ 2 │ a month ago │ │ │ │ │ │ │ ╰───┴──────────────╯ │ │ │ │ ╰─#─┴───────pubDate────────╯ │ ╰──────────┴──────────────────────────────╯ ``` ### After ```nushell { rss_item: [ [ pubDate]; [1773429600], [1774325700], [1775448000] ] } | update rss_item.pubDate { into datetime -f "%s" } | table -e ╭──────────┬──────────────────────╮ │ │ ╭─#─┬───pubDate────╮ │ │ rss_item │ │ 0 │ 2 months ago │ │ │ │ │ 1 │ 2 months ago │ │ │ │ │ 2 │ a month ago │ │ │ │ ╰───┴──────────────╯ │ ╰──────────┴──────────────────────╯ ``` ### Before ```nushell { w: { x: [ { y: "1" } { y: "2" } ] } } | update w.x.y { into float } | to yaml w: x: - 'y': - 1.0 - 2.0 - 'y': - 1.0 - 2.0 ``` ### After ```nushell { w: { x: [ { y: "1" } { y: "2" } ] } } | update w.x.y { into float } | to yaml w: x: - 'y': 1.0 - 'y': 2.0 ``` ### Before ```nushell [{w: {x: [{y: "1"} {y: "2"}]}}] | update w.x.y { into float } | to yaml - w: x: - 'y': - 1.0 - 2.0 - 'y': - 1.0 - 2.0 ``` ### After ```nushell [{w: {x: [{y: "1"} {y: "2"}]}}] | update w.x.y { into float } | to yaml - w: x: - 'y': 1.0 - 'y': 2.0 ``` ### Before ```nushell [ [[a]; [1] [2]] ] | update a {into float} | to yaml - - a: - 1.0 - 2.0 - a: - 1.0 - 2.0 ``` ### After ```nushell [ [[a]; [1] [2]] ] | update a {into float} | to yaml - - a: 1.0 - a: 2.0 ``` <!-- This section is used (mostly as-is) for https://www.nushell.sh/blog/ Describe how Nushell behavior changes from a user's perspective. Do NOT describe internal Rust changes here. Write in a release note style, for example: - "Added support for..." - "Fixed an issue where..." - "Nushell now supports..." - "Improved performance of..." If your changes do NOT affect users (internal refactors, cleanup, etc.), just write: - "n/a" - "nan" - or similar This tells us the change should not appear in the changelog. Tips: - Focus on observable behavior - Include examples if helpful - Keep it concise You can: - Write a short paragraph (will appear as a bullet point), OR - Use headings (###) if your change needs more structure Avoid writing things like: - "In this PR, I refactored..." - "This updates internal code..." You may leave this blank until the PR is ready. --> ## Additional notes closes #15726 related #15789 <!-- Optional. Examples: - fixes #123 - closes #456 - related #789 Anything else reviewers should know. Remove this section if not needed. -->
Description
Fixes: #15726
When nushell update a value, instead of checking if the value is a list. We should follow cell path until the last one, and check if that value is a list.
Given the example:
{ w: { x: [ { y: "1" } { y: "2" } ] } } | update w.x.y { into float }, we should followw.xfirst, this gives us[ { y: "1" } { y: "2" } ], which is a list, and we need to update on the value[ {y: 1.0} {y: 2.0} ]After updated, we need to set
w.xto the updated value, so the result will be{ w: { x: [ { y: 1.0 } { y: 2.0 } ] } }User-Facing Changes
Before
After
Tests + Formatting
Added 1 test.
After Submitting
NaN