Skip to content

Cell paths: foo.1 is not always equal to 1.foo #7498

Closed

Description

@rgwood

Describe the bug

image

How to reproduce

  1. Run [ { foo: "bar"}, { } ] | get foo.1, it returns Nothing
  2. Run [ { foo: "bar"}, { } ] | get 1.foo, it fails with a ShellError

Expected behavior

If a cell path has 2 members and one member is a number but the other is a column name, the order of those members should not matter. I'm not sure exactly what the behaviour should be here, but I do think it's desirable for the behaviour to be consistent.

I lean toward making both return a ShellError, but I'm not sure if there are some situations where that would be problematic (dealing with JSON files maybe?). Please chime in if you have an opinion or are aware of important scenarios we should be considering.

Screenshots

No response

Configuration

key value
version 0.72.2
branch main
commit_hash b7572f1
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.65.0 (897e37553 2022-11-02)
rust_channel 1.65.0-x86_64-unknown-linux-gnu
cargo_version cargo 1.65.0 (4bc8f24d3 2022-10-20)
pkg_version 0.72.2
build_time 2022-12-07 16:34:42 -08:00
build_rust_channel debug
features database, default, trash, which, zip
installed_plugins

Additional context

No response

Activity

  1. webbedspace commented on Dec 16, 2022

    @webbedspace
    Contributor

    What we really should have is nullable paths like foo.0? or a.b?.c (semantics the same as other languages that have it, like Javascript - a.b? turns into null and ignores the rest of the path if b doesn't exit). That way, stuff like this can be a ShellError by default but null if you'd prefer it that way.

  2. added
    A:type-system
    semantics
    on Dec 16, 2022
  3. rgwood commented on Dec 18, 2022

    @rgwood
    ContributorAuthor

    Agreed. I think I might know how to implement optional chaining like that in Nu, I'm going to do some experiments this week.

  4. added a commit that references this issue on Dec 19, 2022
  5. webbedspace commented on Jan 1, 2023

    @webbedspace
    Contributor

    I've been reconsidering the original example closely:

    • Run [ { foo: "bar"}, { } ] | get foo.1, it returns Nothing

    • Run [ { foo: "bar"}, { } ] | get 1.foo, it fails with a ShellError

    Because tables are "actually" lists, 1.foo correctly retrieves { } from it. The .1 access then errors.
    But what get foo does is produce a list of each value's foo property.
    And here's the kicker: if it cannot retrieve that property, it defaults to null.
    Importantly, this is different to how select works.

    〉[ { foo: "bar"}, {} ] | select foo | to nuon
    Error: nu::shell::column_not_found (link)
    
      × Cannot find column
       ╭─[entry #7:1:1]
     1 │ [ { foo: "bar"}, {} ] | select foo | to nuon
       ·                  ─┬            ─┬─
       ·                   │             ╰── cannot find column 'foo'
       ·                   ╰── value originates here
       ╰────
    

    The fact that this works differently for select makes me think that it should just be a ShellError.

  6. rgwood commented on Jan 1, 2023

    @rgwood
    ContributorAuthor

    The fact that this works differently for select makes me think that it should just be a ShellError.

    Are you saying that get foo should behave like select and return a ShellError if any record in the list does not have a foo column? If so I agree - select and get should be consistent in their behaviour.

    On a related note, #7002 attempted to address this but I don't think it got things quite right; get foo now behaves differently from .foo:

    〉[{foo: 'bar'} {}].foo
    ╭───┬─────╮
    │ 0 │ bar │
    │ 1 │     │
    ╰───┴─────╯
    〉[{foo: 'bar'} {}] | get foo
    Error: nu::shell::column_not_found (link)
    
      × Cannot find column
       ╭─[entry #5:1:1]
     1 │ [{foo: 'bar'} {}] | get foo
       · ────────┬────────   ─┬─
       ·         │            ╰── value originates here
       ·         ╰── cannot find column 'Empty cell'
       ╰────
    
  7. added a commit that references this issue on Jan 2, 2023
    65d0b5b
  8. added this to the v0.74 milestone on Jan 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:type-systemsemantics

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions