Skip to content

first and take do the wrong thing for list<binary> input #7121

Closed

Description

@rgwood

Describe the bug

take n currently works like this:

  1. Peek at the first value in the input (important: this can either be the first value in a list or a scalar value passed as input)
  2. If that first value is a binary, return the first n bytes in that binary

This means that take cannot be used to get the first n binaries in a list<binary>. first is affected in much the same way.

How to reproduce

This returns the first byte of a binary:

〉0x[01 23] | take 1
Length: 1 (0x1) bytes | printable whitespace ascii_other non_ascii
00000000:   01

If we wrap the binary in a list, we get the exact same result:

〉[0x[01 23]] | take 1
Length: 1 (0x1) bytes | printable whitespace ascii_other non_ascii
00000000:   01

Expected behavior

[0x[01 23]] | take 1 should return 0x[01 23].

take and first should get the first binaries from a list<binary>, not the first bytes in the first element of the list.

Screenshots

No response

Configuration

key value
version 0.71.1
branch main
commit_hash 817eacc
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.63.0 (4b91a6ea7 2022-08-08)
rust_channel 1.63.0-x86_64-unknown-linux-gnu
cargo_version cargo 1.63.0 (fd9c4297c 2022-07-01)
pkg_version 0.71.1
build_time 2022-11-11 22:41:27 -08:00
build_rust_channel debug
features default, trash, which, zip
installed_plugins

Additional context

No response

Activity

  1. added
    category:inconsistent-behavior
    on Nov 12, 2022
  2. sholderbach commented on Nov 12, 2022

    @sholderbach
    Member

    I think to add to that I think the behavior on other non list types is surprising:

    >  "hello world" | take 5  
    ╭───┬─────────────╮
    │ 0 │ hello world │
    ╰───┴─────────────╯
    >  1 | take 3  
    ╭───┬───╮
    │ 0 │ 1 │
    ╰───┴───╯
    

    My instinct would be that the true scalar might want to error (we should nevertheless provide a convenient way to coerce unknown input (either scalar or list) to a list)
    With the string I might expect a substring or coercion to a list of chars.

    The current behavior for binary landed with nushell/engine-q#284 into engine-q nushell. I haven't checked into the pre 0.59 history.

  3. rgwood commented on Nov 12, 2022

    @rgwood
    ContributorAuthor

    Agreed that the behaviour for other types is surprising. Related: #6941

    I'm working on cleaning some of this up for first and take and it is turning into a yak shave. Will see how far I get.

  4. fdncred commented on Nov 12, 2022

    @fdncred
    Contributor

    One use case I want to be sure not to lose is something like this, that I use ofter to look at BOMs in files.

    open /etc/passwd | into binary | take 100
    Length: 100 (0x64) bytes | printable whitespace ascii_other non_ascii
    00000000:   23 23 0a 23  20 55 73 65  72 20 44 61  74 61 62 61   ##_# User Databa
    00000010:   73 65 0a 23  20 0a 23 20  4e 6f 74 65  20 74 68 61   se_# _# Note tha
    00000020:   74 20 74 68  69 73 20 66  69 6c 65 20  69 73 20 63   t this file is c
    00000030:   6f 6e 73 75  6c 74 65 64  20 64 69 72  65 63 74 6c   onsulted directl
    00000040:   79 20 6f 6e  6c 79 20 77  68 65 6e 20  74 68 65 20   y only when the 
    00000050:   73 79 73 74  65 6d 20 69  73 20 72 75  6e 6e 69 6e   system is runnin
    00000060:   67 0a 23 20                                          g_# 
    
    

    Not that there's a BOM in the password file but the point is to be able to open any file, whether it's binary or text, and look at the first X bytes for deeper analysis.

  5. rgwood commented on Nov 12, 2022

    @rgwood
    ContributorAuthor

    For sure, I'm planning to keep the binary -> binary functionality as-is.

  6. added this to the v0.72 milestone on Nov 14, 2022
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

    category:inconsistent-behavior

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions