Skip to content

Add uniq command - #1132

Merged
sophiajt merged 13 commits into
nushell:masterfrom
notryanb:command-uniq
Dec 31, 2019
Merged

sophiajt merged 13 commits into
nushell:masterfrom
notryanb:command-uniq

Conversation

@notryanb

@notryanb notryanb commented Dec 28, 2019 •

Copy link
Copy Markdown
Contributor

This PR adds a uniq command to nushell. The usage of this command is similar to uniq in Bash, except instead of filtering out repeated lines of text, it filters out repeated rows from the pipeline. The current implementation looks to be the only command that does not have optional parameters.

References Issue: #956

Usage

Given file test.csv

first_name,last_name,rusty_at,type
Andrés,Robalino,10/11/2013,A
Andrés,Robalino,10/11/2013,A
Jonathan,Turner,10/12/2013,B
Yehuda,Katz,10/11/2013,A

Command

open test.csv | uniq

Output

━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━
 # │ first_name │ last_name │ rusty_at   │ type
───┼────────────┼───────────┼────────────┼──────
 0 │ Andrés     │ Robalino  │ 10/11/2013 │ A
 1 │ Jonathan   │ Turner    │ 10/12/2013 │ B
 2 │ Yehuda     │ Katz      │ 10/11/2013 │ A
━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━

Command

open test.csv | pick rusty_at type | uniq

Output

━━━┯━━━━━━━━━━━━┯━━━━━━
 # │ rusty_at   │ type
───┼────────────┼──────
 0 │ 10/11/2013 │ A
 1 │ 10/12/2013 │ B
━━━┷━━━━━━━━━━━━┷━━━━━━

Questions (all answered)

  • I have not seen another command that omits arguments/parameters. I'm not sure the best way to implement this as I've had to keep in some "dead code". If there are some useful modifiers to add to uniq, I can add them in this PR.
  • I'm unsure if this would be better implemented as a plugin due to the fact it doesn't have optional parameters. It feels like a command because it is row filtering like the where command.

TODO

  • Add some more tests to the test-suite. I'm curious to see how this will work when a column has a nested table. I'll need to add some nested json tests.
  • Write documentation

@gitpod-io

gitpod-io Bot commented Dec 28, 2019 •

Copy link
Copy Markdown

@sophiajt

Copy link
Copy Markdown
Contributor

The sys command doesn't (yet), so you could maybe use the signature as an example.

I think it should be a command. Agreed that because it's a fundamental table operation, it should be a command

@andrasio

Copy link
Copy Markdown
Member

Very cool Ryan.

Please check the wrap command.

Today it works both on values as well as rows.

Perhaps you could support uniq to work on simple values too? In that case ls | get name | uniq would work as well and could be handy too.

@notryanb

Copy link
Copy Markdown
Contributor Author

@jonathandturner I ended up using autoview as a template because sys is a plugin.
@andrasio By "values", do you mean something along the lines of ls | get type | uniq? This already works via this PR and I've added a separate test to distinguish between pick and get.

I added some documentation and I think this is almost ready to go. I'll try to mess around with how this behaves with nested data structures today and write a few more tests.

@notryanb

Copy link
Copy Markdown
Contributor Author

@jonathandturner I've added a new fixture that sets up some very basic nested json along with some tests.

@notryanb notryanb changed the title WIP - Command uniq Add uniq command Dec 29, 2019
@notryanb

Copy link
Copy Markdown
Contributor Author

There is still one lingering issue that I'd like to solve before merging this.

Consider the following data structure: [ {"a": "a", "b": "b" }, { "b" : "b", "a" : "a"} ]
While the columns look the same in table format, hashing these two objects creates two unique rows. Ideally we want these two objects to be duplicates and uniq should return only one.

Currently, you can get around this with a pipeline like echo '[ {"a": "a", "b": "b" }, { "b" : "b", "a" : "a"} ]' | from-json | to-csv | from-csv | uniq, however, this will not be feasible when dealing with large deeply nested datasets.

I think we can solve this by sorting the keys/columns in data structures before they are hashed, but I'll need to play around with it further. A number of tests need to be written for edge cases like this.

@notryanb

Copy link
Copy Markdown
Contributor Author

@jonathandturner I ended up doing what we discussed and sorted the Dictionary entries by key. The test uniq fixture now is composed of more deeply nested json with "out of order" keys.

@sophiajt

Copy link
Copy Markdown
Contributor

Cool, looks good. Thanks!

@sophiajt
sophiajt merged commit f37f29b into nushell:master Dec 31, 2019
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.

3 participants