Skip to content

Commit 71c6bd0

Browse files
committed
add tests for desc
Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>
1 parent 730cc2b commit 71c6bd0

File tree

7 files changed

+55
-7
lines changed

7 files changed

+55
-7
lines changed

core/src/main/java/org/opensearch/sql/ast/tree/Sort.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ public class Sort extends UnresolvedPlan {
3131

3232
private final List<Field> sortList;
3333

34-
/** Sort Constructor without count. */
3534
public Sort(List<Field> sortList) {
3635
this(null, sortList);
3736
}
3837

39-
/** Sort Constructor. */
4038
public Sort(Integer count, List<Field> sortList) {
4139
this.count = count;
4240
this.sortList = sortList;

docs/user/ppl/cmd/sort.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Syntax
1919
sort [count] <[+|-] sort-field>... [desc|d]
2020

2121

22-
* count: optional. The number of results to return. **Default:** returns all results. Specifying a count of 0 also returns all results.
22+
* 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.
2323
* [+|-]: 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.
2424
* 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.
2525
* [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.

integ-test/src/test/java/org/opensearch/sql/ppl/SortCommandIT.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,28 @@ public void testSortWithCountZero() throws IOException {
177177
JSONObject result =
178178
executeQuery(
179179
String.format(
180-
"source=%s | sort 0 - account_number | fields account_number", TEST_INDEX_BANK));
181-
verifyOrder(result, rows(32), rows(25), rows(20), rows(18), rows(13), rows(6), rows(1));
180+
"source=%s | sort 0 account_number | fields account_number", TEST_INDEX_BANK));
181+
verifyOrder(result, rows(1), rows(6), rows(13), rows(18), rows(20), rows(25), rows(32));
182182
}
183183

184184
@Test
185-
public void testSortWithDescSuffix() throws IOException {
185+
public void testSortWithDesc() throws IOException {
186186
JSONObject result =
187187
executeQuery(
188188
String.format(
189189
"source=%s | sort account_number desc | fields account_number", TEST_INDEX_BANK));
190190
verifyOrder(result, rows(32), rows(25), rows(20), rows(18), rows(13), rows(6), rows(1));
191191
}
192192

193+
@Test
194+
public void testSortWithDescMultipleFields() throws IOException {
195+
JSONObject result =
196+
executeQuery(
197+
String.format(
198+
"source=%s | sort 4 age, - account_number desc | fields age, account_number", TEST_INDEX_BANK));
199+
verifyOrder(result, rows(39, 25), rows(36, 6), rows(36, 20), rows(34, 32));
200+
}
201+
193202
@Test
194203
public void testSortWithStrCast() throws IOException {
195204
JSONObject result =
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"calcite": {
3+
"logical": "LogicalSystemLimit(fetch=[10000], type=[QUERY_SIZE_LIMIT])\n LogicalProject(age=[$8])\n LogicalSort(sort0=[$17], dir0=[ASC-nulls-first])\n LogicalProject(account_number=[$0], firstname=[$1], address=[$2], balance=[$3], gender=[$4], city=[$5], employer=[$6], state=[$7], age=[$8], email=[$9], lastname=[$10], _id=[$11], _index=[$12], _score=[$13], _maxscore=[$14], _sort=[$15], _routing=[$16], $f17=[SAFE_CAST($8)])\n CalciteLogicalIndexScan(table=[[OpenSearch, opensearch-sql_test_index_account]])\n",
4+
"physical": "EnumerableCalc(expr#0..1=[{inputs}], age=[$t0])\n EnumerableLimit(fetch=[10000])\n EnumerableSort(sort0=[$1], dir0=[ASC-nulls-first])\n EnumerableCalc(expr#0=[{inputs}], expr#1=[SAFE_CAST($t0)], proj#0..1=[{exprs}])\n CalciteEnumerableIndexScan(table=[[OpenSearch, opensearch-sql_test_index_account]], PushDownContext=[[PROJECT->[age]], OpenSearchRequestBuilder(sourceBuilder={\"from\":0,\"timeout\":\"1m\",\"_source\":{\"includes\":[\"age\"],\"excludes\":[]}}, requestedTotalSize=2147483647, pageSize=null, startFrom=0)])\n"
5+
}
6+
}

integ-test/src/test/resources/expectedOutput/ppl/explain_sort_desc_push.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{
99
"name": "OpenSearchIndexScan",
1010
"description": {
11-
"request": "OpenSearchQueryRequest(indexName=opensearch-sql_test_index_account, sourceBuilder={\"from\":0,\"size\":10000,\"timeout\":\"1m\",\"_source\":{\"includes\":[\"age\"],\"excludes\":[]},\"sort\":[{\"age\":{\"order\":\"desc\",\"missing\":\"_last\"}}, {\"firstname.keyword\":{\"order\":\"asc\",\"missing\":\"_first\"}}]}, needClean=true, searchDone=false, pitId=null, cursorKeepAlive=null, searchAfter=null, searchResponse=null)"
11+
"request": "OpenSearchQueryRequest(indexName=opensearch-sql_test_index_account, sourceBuilder={\"from\":0,\"size\":10000,\"timeout\":\"1m\",\"_source\":{\"includes\":[\"age\",\"firstname\"],\"excludes\":[]},\"sort\":[{\"age\":{\"order\":\"desc\",\"missing\":\"_last\"}},{\"firstname.keyword\":{\"order\":\"asc\",\"missing\":\"_first\"}}]}, needClean=true, searchDone=false, pitId=*, cursorKeepAlive=1m, searchAfter=null, searchResponse=null)"
1212
},
1313
"children": []
1414
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"root": {
3+
"name": "ProjectOperator",
4+
"description": {
5+
"fields": "[age]"
6+
},
7+
"children": [
8+
{
9+
"name": "SortOperator",
10+
"description": {
11+
"sortList": {
12+
"cast_to_double(age)": {
13+
"sortOrder": "ASC",
14+
"nullOrder": "NULL_FIRST"
15+
}
16+
}
17+
},
18+
"children": [
19+
{
20+
"name": "OpenSearchIndexScan",
21+
"description": {
22+
"request": "OpenSearchQueryRequest(indexName=opensearch-sql_test_index_account, sourceBuilder={\"from\":0,\"size\":10000,\"timeout\":\"1m\"}, needClean=true, searchDone=false, pitId=*, cursorKeepAlive=1m, searchAfter=null, searchResponse=null)"
23+
},
24+
"children": []
25+
}
26+
]
27+
}
28+
]
29+
}
30+
}

ppl/src/test/java/org/opensearch/sql/ppl/utils/PPLQueryDataAnonymizerTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ public void testSortCommandWithCount() {
142142
assertEquals("source=t | sort 5 f1", anonymize("source=t | sort 5 f1"));
143143
}
144144

145+
@Test
146+
public void testSortCommandWithDesc() {
147+
assertEquals("source=t | sort f1", anonymize("source=t | sort f1 desc"));
148+
}
149+
145150
@Test
146151
public void testEvalCommand() {
147152
assertEquals("source=t | eval r=abs(f)", anonymize("source=t | eval r=abs(f)"));

0 commit comments

Comments
 (0)