Skip to content

Fix update with closure on nested list - #15789

Closed
WindSoilder wants to merge 12 commits into
nushell:mainfrom
WindSoilder:update_cell_path
Closed

WindSoilder wants to merge 12 commits into
nushell:mainfrom
WindSoilder:update_cell_path

Conversation

@WindSoilder

@WindSoilder WindSoilder commented May 20, 2025 •

Copy link
Copy Markdown
Contributor

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 follow w.x first, 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.x to the updated value, so the result will be { w: { x: [ { y: 1.0 } { y: 2.0 } ] } }

User-Facing Changes

Before

> { 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

> { w: { x: [ { y: "1" } { y: "2" } ] } } | update w.x.y { into float } | to yaml
w:
  x:
  - y: 1.0
  - y: 2.0

Tests + Formatting

Added 1 test.

After Submitting

NaN

@Bahex

Bahex commented May 23, 2025

Copy link
Copy Markdown
Member

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.0

Minimal reproduction:

[ [[a]; [1] [2]] ] | update a {into float} | to yaml
- - a:
    - 1.0
    - 2.0
  - a:
    - 1.0
    - 2.0

While 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: 2

After:

[[a]; [1] [2]] | update $. {|| } | to yaml
Error:   × cell path can't be empty
  help: This should not happened, please file an issue

@sholderbach sholderbach added the A:cell-path-semantics All around the cell path type and its semantics for access label May 23, 2025
@WindSoilder

WindSoilder commented May 24, 2025 •

Copy link
Copy Markdown
Contributor Author

Thanks! I didn't think about this, so if outermost value is a list, then follow_cell_path always returns a list too:

# following w.x.y                                             
[{w: {x: [{y: "1"} {y: "2"}]}}] -> [[{y: "1"}, {y: "2"}]]
[{w: {x: [{y: "1"} {y: "2"}]}}, {w: {x: [{y: "3"} {y: "4"}]}}] -> [[{y: "1"} {y: "2"}], [{y: "3"}, {y: "4"}]]

It's necessary to handle this seperately.

@WindSoilder
WindSoilder marked this pull request as draft May 26, 2025 22:39
@WindSoilder
WindSoilder marked this pull request as ready for review May 27, 2025 12:08
@WindSoilder

Copy link
Copy Markdown
Contributor Author

Ok, to handle with nested lists, the implementation tends to be complicated.
Because it's necessary to iterate and handle nested elements.

@Bahex

Bahex commented May 31, 2025

Copy link
Copy Markdown
Member

@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 update at all, but just manifesting in its usage.
This should explain what I mean:

;; 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: WHAT

I'll try to see if I can find the root cause of this problem, and if it can be solved all at once.
If I can't find it, we should go ahead with this PR and leave a #FIXME comment for later.

@WindSoilder

WindSoilder commented May 31, 2025 •

Copy link
Copy Markdown
Contributor Author

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.

Sure I understand the feel, because I feel the same :-( I think the reason might lay inside follow_cell_path.
Follow under list and follow under record give us a completely different result.

I'll try to see if I can find the root cause of this problem, and if it can be solved all at once.
If I can't find it, we should go ahead with this PR and leave a #FIXME comment for later.

Thank you for looking into this.

@Bahex

Bahex commented May 31, 2025 •

Copy link
Copy Markdown
Member

I keep finding more and more edge cases 😔

  • increasing the level of list nesting
  • list value input vs stream input
  • closure vs value replacement

all cause different behaviours 😭

edit: upsert has the same problems too....

@WindSoilder

Copy link
Copy Markdown
Contributor Author

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

@fdncred

fdncred commented May 14, 2026

Copy link
Copy Markdown
Contributor

I took a swing at fixing this here #18205. Please review and let me know if it fixes the issue.

fdncred added a commit that referenced this pull request Jun 1, 2026
<!--
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.
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:cell-path-semantics All around the cell path type and its semantics for access

Projects

None yet

Development

Successfully merging this pull request may close these issues.

the "update" command provides wrong $in in subtables

4 participants