Repository navigation
Support passing an empty list to sort, uniq, sort-by, and uniq-by (issue #5957) - #8669
Merged
Merged
Conversation
rgwood
approved these changes
Mar 29, 2023
rgwood
left a comment
Contributor
There was a problem hiding this comment.
Awesome first PR, thanks! This looks good to me; let's just wait to see whether other people agree with the change (I've asked in #design-discussions on Discord).
I tweaked the tests a bit:
- No need to specify
cwdif the test isn't accessing files pipeline()is usually unnecessary- It's more common for our tests to use
to nuonthanto json
(I can appreciate that our existing tests don't always follow these rules, especially 1 and 2)
Contributor
Author
|
Oh, those tests look much simpler, thanks! |
This was referenced Mar 30, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Currently, all four of these commands return a (rather-confusing) spanless error when passed an empty list:
This PR changes these commands to always output
[]if the input is[].I'm not sure what the original logic was here, but in the case of
sortanduniq, I think the current behavior is straightforwardly wrong.sort-byanduniq-byare a bit more complicated, since they currently try to perform some validation that the specified column name is present in the input (see #8667 for problems with this validation, where a possible outcome is removing the validation entirely). When passed[], it's not possible to do any validation because there are no records. This opens up the possibility for situations like the following:I think there's a strong argument that
[]is the best output for these commands as well, since it makes pipelines like$table | filter $condition | sort-by $columnmore predictable. Currently, this pipeline will throw an error iffilterevaluates to[], but work fine otherwise. This makes it difficult to write reliable code, especially since users are not likely to encounter thefilter -> []case in testing (issue #5957). The only workaround is to insert manual checks for an empty result. IMO, this is significantly worse than the "you can typo a column name without getting an error" problem shown above.Other commands that take column arguments (
get,select,rename, etc) already have[] -> [], so there's existing precedent for this behavior.The core question here is "what columns does
[]have"? The current behavior ofsort-byis "no columns", while the current behavior ofselectis "all possible columns". Both answers lead to accepting some likely-buggy code without throwing on error, but in order to do better here we would need something likeValue::Tablethat tracks columns on empty tables.If other people disagree with this logic, I'm happy to split out the
sort-byanduniq-bychanges into another PR.User-Facing Changes
sort,uniq,sort-by, anduniq-bynow return[]instead of throwing an error when input is[].After Submitting
The existing behavior was not documented, and the new behavior is what you would expect by default, so I don't think we need to update documentation.