-
Notifications
You must be signed in to change notification settings - Fork 180
Enhance sort command in PPL #3934
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
21205ee
91d2480
788ec2f
99aa9ca
d53695b
4c60aa8
cbe65fd
812e8df
333eb8b
791ae01
10656dc
f770033
75f7898
ec7ac6a
7a40f23
8521a13
2a6842e
94ce75a
730cc2b
71c6bd0
bb02bbb
1c62976
fa676d5
97f4aa6
78f6c79
7f47364
1382a06
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 |
|---|---|---|
|
|
@@ -20,11 +20,20 @@ | |
| @EqualsAndHashCode(callSuper = true) | ||
| public class LogicalSort extends LogicalPlan { | ||
|
|
||
| /** Maximum number of results to return after sorting. */ | ||
| private final Integer count; | ||
|
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. Add javadoc.
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. I would prefer if we made this an This makes the contract obvious to anyone using/reading the class. We can also improve the validation at this constructor, like requiring Integer be non-null and non-negative if supplied (allowing us to raise nice validation errors to users, "sort limit cannot be negative."). We can also map 0 to
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. Updated to make the return type of the getter for |
||
|
|
||
| private final List<Pair<SortOption, Expression>> sortList; | ||
|
|
||
| /** Constructor of LogicalSort. */ | ||
| public LogicalSort(LogicalPlan child, List<Pair<SortOption, Expression>> sortList) { | ||
| this(child, 0, sortList); | ||
| } | ||
|
|
||
| /** Constructor of LogicalSort. */ | ||
| public LogicalSort( | ||
| LogicalPlan child, Integer count, List<Pair<SortOption, Expression>> sortList) { | ||
| super(Collections.singletonList(child)); | ||
| this.count = count; | ||
| this.sortList = sortList; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,11 +16,13 @@ Description | |
|
|
||
| Syntax | ||
| ============ | ||
| sort <[+|-] sort-field>... | ||
| sort [count] <[+|-] sort-field>... [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. | ||
| * [+|-]: 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. | ||
| * 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. | ||
penghuo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| Example 1: Sort by one field | ||
|
|
@@ -49,7 +51,7 @@ The example show sort all the document with age field in ascending order. | |
|
|
||
| PPL query:: | ||
|
|
||
| os> source=accounts | sort age | fields account_number, age; | ||
| os> source=accounts | sort 0 age | fields account_number, age; | ||
| fetched rows / total rows = 4/4 | ||
| +----------------+-----+ | ||
| | account_number | age | | ||
|
|
@@ -114,3 +116,74 @@ PPL query:: | |
| | Pyrami | | ||
| | Quility | | ||
| +----------+ | ||
|
|
||
| Example 5: Specify the number of sorted documents to return | ||
| ============================================================ | ||
|
|
||
| The example shows sorting all the document and returning 2 documents. | ||
|
|
||
| PPL query:: | ||
|
|
||
| os> source=accounts | sort 2 age | fields account_number, age; | ||
| fetched rows / total rows = 2/2 | ||
| +----------------+-----+ | ||
| | account_number | age | | ||
| |----------------+-----| | ||
| | 13 | 28 | | ||
| | 1 | 32 | | ||
| +----------------+-----+ | ||
|
|
||
| Example 6: Sort with desc modifier | ||
| =================================== | ||
|
|
||
| The example shows sorting with the desc modifier to reverse sort order. | ||
|
|
||
| PPL query:: | ||
|
|
||
| os> source=accounts | sort age desc | fields account_number, age; | ||
|
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. Include a test cases explain mutile fields reverse case
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. Added as Example 7 |
||
| fetched rows / total rows = 4/4 | ||
| +----------------+-----+ | ||
| | account_number | age | | ||
| |----------------+-----| | ||
| | 6 | 36 | | ||
| | 18 | 33 | | ||
| | 1 | 32 | | ||
| | 13 | 28 | | ||
| +----------------+-----+ | ||
|
|
||
| Example 7: Sort by multiple fields with desc modifier | ||
| ====================================================== | ||
|
|
||
| The example shows sorting by multiple fields using desc, which reverses the sort order for all specified fields. Gender is reversed from ascending to descending, and the descending age sort is reversed to ascending within each gender group. | ||
|
|
||
| PPL query:: | ||
|
|
||
| os> source=accounts | sort gender, -age desc | fields account_number, gender, age; | ||
| fetched rows / total rows = 4/4 | ||
| +----------------+--------+-----+ | ||
| | account_number | gender | age | | ||
| |----------------+--------+-----| | ||
| | 1 | M | 32 | | ||
| | 18 | M | 33 | | ||
| | 6 | M | 36 | | ||
| | 13 | F | 28 | | ||
| +----------------+--------+-----+ | ||
|
|
||
|
|
||
| Example 8: Sort with specifying field type | ||
| ================================== | ||
|
|
||
| The example shows sorting with str() to sort numeric values lexicographically. | ||
|
|
||
| PPL query:: | ||
|
|
||
| os> source=accounts | sort str(account_number) | fields account_number; | ||
| fetched rows / total rows = 4/4 | ||
| +----------------+ | ||
| | account_number | | ||
| |----------------| | ||
| | 1 | | ||
| | 13 | | ||
| | 18 | | ||
| | 6 | | ||
| +----------------+ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,4 +161,60 @@ public void testSortThenHead() throws IOException { | |
| executeQuery(String.format("source=%s | sort age | head 2 | fields age", TEST_INDEX_BANK)); | ||
| verifyOrder(result, rows(28), rows(32)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSortWithCountLimit() throws IOException { | ||
|
Member
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. please add some tests with
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. Added another test with |
||
| JSONObject result = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | sort 3 - account_number | fields account_number", TEST_INDEX_BANK)); | ||
| verifyOrder(result, rows(32), rows(25), rows(20)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSortWithCountZero() throws IOException { | ||
| // count=0 should return all results | ||
| JSONObject result = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | sort 0 account_number | fields account_number", TEST_INDEX_BANK)); | ||
| verifyOrder(result, rows(1), rows(6), rows(13), rows(18), rows(20), rows(25), rows(32)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSortWithDesc() throws IOException { | ||
| JSONObject result = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | sort account_number desc | fields account_number", TEST_INDEX_BANK)); | ||
| verifyOrder(result, rows(32), rows(25), rows(20), rows(18), rows(13), rows(6), rows(1)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSortWithDescMultipleFields() throws IOException { | ||
| JSONObject result = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | sort 4 age, - account_number desc | fields age, account_number", | ||
| TEST_INDEX_BANK)); | ||
| verifyOrder(result, rows(39, 25), rows(36, 6), rows(36, 20), rows(34, 32)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSortWithStrCast() throws IOException { | ||
| JSONObject result = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | sort str(account_number) | fields account_number", TEST_INDEX_BANK)); | ||
| verifyOrder(result, rows(1), rows(13), rows(18), rows(20), rows(25), rows(32), rows(6)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSortWithNumCast() throws IOException { | ||
| JSONObject result = | ||
| executeQuery( | ||
| String.format("source=%s | sort num(bytes) | fields bytes", TEST_INDEX_WEBLOGS)); | ||
| verifyOrder( | ||
| result, rows("1234"), rows("3985"), rows("4085"), rows("4321"), rows("6245"), rows("9876")); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.