-
Notifications
You must be signed in to change notification settings - Fork 181
Add asc keyword to sort command #4113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6a161fd
1c27280
96cda7d
9f5eae5
7dcd9cf
617090c
787621b
6c54b41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,13 +16,13 @@ Description | |
|
|
||
| Syntax | ||
| ============ | ||
| sort [count] <[+|-] sort-field>... [desc|d] | ||
| sort [count] <[+|-] sort-field>... [asc|a|desc|d] | ||
|
|
||
|
|
||
| * count: optional. The number of results to return. **Default:** returns all results. Specifying a count of 0 or less than 0 also returns all results. | ||
| * count (Since 3.3): optional. The number of results to return. **Default:** returns all results. Specifying a count of 0 or less than 0 also returns all results. | ||
| * [+|-]: optional. The plus [+] stands for ascending order and NULL/MISSING first and a minus [-] stands for descending order and NULL/MISSING last. **Default:** ascending order and NULL/MISSING first. | ||
| * sort-field: mandatory. The field used to sort. Can use ``auto(field)``, ``str(field)``, ``ip(field)``, or ``num(field)`` to specify how to interpret field values. | ||
| * [desc|d]: optional. Reverses the sort results. If multiple fields are specified, reverses order of the first field then for all duplicate values of the first field, reverses the order of the values of the second field and so on. | ||
| * [asc|a|desc|d] (Since 3.3): optional. asc/a keeps the sort order as specified. desc/d reverses the sort results. If multiple fields are specified with desc/d, reverses order of the first field then for all duplicate values of the first field, reverses the order of the values of the second field and so on. **Default:** asc. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Consider rewording to 'sorts in ascending order' for clarity.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The behavior for asc is it will keep whatever the specified field sort order was, so for |
||
|
|
||
|
|
||
| Example 1: Sort by one field | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -234,4 +234,100 @@ public void testSortDate() throws IOException { | |
| rows("Virginia", "2018-08-19 00:00:00"), | ||
| rows("Dale", "2018-11-13 23:33:20")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSortWithAscKeyword() throws IOException { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Consider adding negative test cases:
|
||
| JSONObject result = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | sort account_number asc | fields account_number, firstname", | ||
| TEST_INDEX_BANK)); | ||
| verifySchema(result, schema("account_number", "bigint"), schema("firstname", "string")); | ||
| verifyDataRowsInOrder( | ||
| result, | ||
| rows(1, "Amber JOHnny"), | ||
| rows(6, "Hattie"), | ||
| rows(13, "Nanette"), | ||
| rows(18, "Dale"), | ||
| rows(20, "Elinor"), | ||
| rows(25, "Virginia"), | ||
| rows(32, "Dillard")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSortWithAKeyword() throws IOException { | ||
| JSONObject result = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | sort account_number a | fields account_number, firstname", | ||
| TEST_INDEX_BANK)); | ||
| verifySchema(result, schema("account_number", "bigint"), schema("firstname", "string")); | ||
| verifyDataRowsInOrder( | ||
| result, | ||
| rows(1, "Amber JOHnny"), | ||
| rows(6, "Hattie"), | ||
| rows(13, "Nanette"), | ||
| rows(18, "Dale"), | ||
| rows(20, "Elinor"), | ||
| rows(25, "Virginia"), | ||
| rows(32, "Dillard")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSortWithDescKeyword() throws IOException { | ||
| JSONObject result = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | sort account_number desc | fields account_number, firstname", | ||
| TEST_INDEX_BANK)); | ||
| verifySchema(result, schema("account_number", "bigint"), schema("firstname", "string")); | ||
| verifyDataRowsInOrder( | ||
| result, | ||
| rows(32, "Dillard"), | ||
| rows(25, "Virginia"), | ||
| rows(20, "Elinor"), | ||
| rows(18, "Dale"), | ||
| rows(13, "Nanette"), | ||
| rows(6, "Hattie"), | ||
| rows(1, "Amber JOHnny")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSortWithCount() throws IOException { | ||
| JSONObject result = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | sort 3 account_number | fields account_number, firstname", | ||
| TEST_INDEX_BANK)); | ||
| verifySchema(result, schema("account_number", "bigint"), schema("firstname", "string")); | ||
| verifyDataRowsInOrder(result, rows(1, "Amber JOHnny"), rows(6, "Hattie"), rows(13, "Nanette")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSortWithStrCast() throws IOException { | ||
|
|
||
| JSONObject result = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | sort STR(account_number) | fields account_number", TEST_INDEX_BANK)); | ||
| verifyDataRowsInOrder( | ||
| result, rows(1), rows(13), rows(18), rows(20), rows(25), rows(32), rows(6)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSortWithAutoCast() throws IOException { | ||
| JSONObject result = | ||
| executeQuery( | ||
| String.format("source=%s | sort AUTO(age) | fields firstname, age", TEST_INDEX_BANK)); | ||
| verifySchema(result, schema("firstname", "string"), schema("age", "int")); | ||
| verifyDataRowsInOrder( | ||
| result, | ||
| rows("Nanette", 28), | ||
| rows("Amber JOHnny", 32), | ||
| rows("Dale", 33), | ||
| rows("Dillard", 34), | ||
| rows("Hattie", 36), | ||
| rows("Elinor", 36), | ||
| rows("Virginia", 39)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -158,7 +158,7 @@ dedupCommand | |
| ; | ||
|
|
||
| sortCommand | ||
| : SORT (count = integerLiteral)? sortbyClause (DESC | D)? | ||
| : SORT (count = integerLiteral)? sortbyClause (ASC | A | DESC | D)? | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we consider the precedence if someone accidentally specifies both asc and desc? The grammar allows it but behavior might be undefined.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just checked using both asc and desc will result in an error
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got it. Thanks for confirming that. |
||
| ; | ||
|
|
||
| reverseCommand | ||
|
|
@@ -1201,6 +1201,8 @@ keywordsCanBeId | |
| | EXISTS | ||
| | SOURCE | ||
| | INDEX | ||
| | A | ||
| | ASC | ||
| | DESC | ||
| | DATASOURCES | ||
| | FROM | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have these covered in example section?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! just added 'Since 3.3' in this PR but added examples in previous PR