Skip to content

Commit 6db8d80

Browse files
authored
Add asc keyword to sort command (opensearch-project#4113)
* add asc keyword to sort Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com> * add tests and update docs Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com> * remove a Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com> * add a keyword Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com> * update doc Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com> * empty Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com> * fix formatting Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com> * empty Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com> --------- Signed-off-by: Ritvi Bhatt <ribhatt@amazon.com>
1 parent cc9c8d9 commit 6db8d80

6 files changed

Lines changed: 176 additions & 4 deletions

File tree

docs/user/ppl/cmd/sort.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ Description
1616

1717
Syntax
1818
============
19-
sort [count] <[+|-] sort-field>... [desc|d]
19+
sort [count] <[+|-] sort-field>... [asc|a|desc|d]
2020

2121

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.
22+
* 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.
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.
25-
* [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.
25+
* [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.
2626

2727

2828
Example 1: Sort by one field

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalcitePPLSortIT.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,100 @@ public void testSortDate() throws IOException {
234234
rows("Virginia", "2018-08-19 00:00:00"),
235235
rows("Dale", "2018-11-13 23:33:20"));
236236
}
237+
238+
@Test
239+
public void testSortWithAscKeyword() throws IOException {
240+
JSONObject result =
241+
executeQuery(
242+
String.format(
243+
"source=%s | sort account_number asc | fields account_number, firstname",
244+
TEST_INDEX_BANK));
245+
verifySchema(result, schema("account_number", "bigint"), schema("firstname", "string"));
246+
verifyDataRowsInOrder(
247+
result,
248+
rows(1, "Amber JOHnny"),
249+
rows(6, "Hattie"),
250+
rows(13, "Nanette"),
251+
rows(18, "Dale"),
252+
rows(20, "Elinor"),
253+
rows(25, "Virginia"),
254+
rows(32, "Dillard"));
255+
}
256+
257+
@Test
258+
public void testSortWithAKeyword() throws IOException {
259+
JSONObject result =
260+
executeQuery(
261+
String.format(
262+
"source=%s | sort account_number a | fields account_number, firstname",
263+
TEST_INDEX_BANK));
264+
verifySchema(result, schema("account_number", "bigint"), schema("firstname", "string"));
265+
verifyDataRowsInOrder(
266+
result,
267+
rows(1, "Amber JOHnny"),
268+
rows(6, "Hattie"),
269+
rows(13, "Nanette"),
270+
rows(18, "Dale"),
271+
rows(20, "Elinor"),
272+
rows(25, "Virginia"),
273+
rows(32, "Dillard"));
274+
}
275+
276+
@Test
277+
public void testSortWithDescKeyword() throws IOException {
278+
JSONObject result =
279+
executeQuery(
280+
String.format(
281+
"source=%s | sort account_number desc | fields account_number, firstname",
282+
TEST_INDEX_BANK));
283+
verifySchema(result, schema("account_number", "bigint"), schema("firstname", "string"));
284+
verifyDataRowsInOrder(
285+
result,
286+
rows(32, "Dillard"),
287+
rows(25, "Virginia"),
288+
rows(20, "Elinor"),
289+
rows(18, "Dale"),
290+
rows(13, "Nanette"),
291+
rows(6, "Hattie"),
292+
rows(1, "Amber JOHnny"));
293+
}
294+
295+
@Test
296+
public void testSortWithCount() throws IOException {
297+
JSONObject result =
298+
executeQuery(
299+
String.format(
300+
"source=%s | sort 3 account_number | fields account_number, firstname",
301+
TEST_INDEX_BANK));
302+
verifySchema(result, schema("account_number", "bigint"), schema("firstname", "string"));
303+
verifyDataRowsInOrder(result, rows(1, "Amber JOHnny"), rows(6, "Hattie"), rows(13, "Nanette"));
304+
}
305+
306+
@Test
307+
public void testSortWithStrCast() throws IOException {
308+
309+
JSONObject result =
310+
executeQuery(
311+
String.format(
312+
"source=%s | sort STR(account_number) | fields account_number", TEST_INDEX_BANK));
313+
verifyDataRowsInOrder(
314+
result, rows(1), rows(13), rows(18), rows(20), rows(25), rows(32), rows(6));
315+
}
316+
317+
@Test
318+
public void testSortWithAutoCast() throws IOException {
319+
JSONObject result =
320+
executeQuery(
321+
String.format("source=%s | sort AUTO(age) | fields firstname, age", TEST_INDEX_BANK));
322+
verifySchema(result, schema("firstname", "string"), schema("age", "int"));
323+
verifyDataRowsInOrder(
324+
result,
325+
rows("Nanette", 28),
326+
rows("Amber JOHnny", 32),
327+
rows("Dale", 33),
328+
rows("Dillard", 34),
329+
rows("Hattie", 36),
330+
rows("Elinor", 36),
331+
rows("Virginia", 39));
332+
}
237333
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,40 @@ public void testSortWithNumCast() throws IOException {
217217
verifyOrder(
218218
result, rows("1234"), rows("3985"), rows("4085"), rows("4321"), rows("6245"), rows("9876"));
219219
}
220+
221+
@Test
222+
public void testSortWithAsc() throws IOException {
223+
JSONObject result =
224+
executeQuery(
225+
String.format(
226+
"source=%s | sort account_number asc | fields account_number", TEST_INDEX_BANK));
227+
verifyOrder(result, rows(1), rows(6), rows(13), rows(18), rows(20), rows(25), rows(32));
228+
}
229+
230+
@Test
231+
public void testSortWithA() throws IOException {
232+
JSONObject result =
233+
executeQuery(
234+
String.format(
235+
"source=%s | sort account_number a | fields account_number", TEST_INDEX_BANK));
236+
verifyOrder(result, rows(1), rows(6), rows(13), rows(18), rows(20), rows(25), rows(32));
237+
}
238+
239+
@Test
240+
public void testSortWithAscMultipleFields() throws IOException {
241+
JSONObject result =
242+
executeQuery(
243+
String.format(
244+
"source=%s | sort age, account_number asc | fields age, account_number",
245+
TEST_INDEX_BANK));
246+
verifyOrder(
247+
result,
248+
rows(28, 13),
249+
rows(32, 1),
250+
rows(33, 18),
251+
rows(34, 32),
252+
rows(36, 6),
253+
rows(36, 20),
254+
rows(39, 25));
255+
}
220256
}

ppl/src/main/antlr/OpenSearchPPLLexer.g4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ AS: 'AS';
7171
BY: 'BY';
7272
SOURCE: 'SOURCE';
7373
INDEX: 'INDEX';
74+
A: 'A';
75+
ASC: 'ASC';
7476
D: 'D';
7577
DESC: 'DESC';
7678
DATASOURCES: 'DATASOURCES';

ppl/src/main/antlr/OpenSearchPPLParser.g4

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ dedupCommand
161161
;
162162

163163
sortCommand
164-
: SORT (count = integerLiteral)? sortbyClause (DESC | D)?
164+
: SORT (count = integerLiteral)? sortbyClause (ASC | A | DESC | D)?
165165
;
166166

167167
reverseCommand
@@ -1241,6 +1241,8 @@ keywordsCanBeId
12411241
| EXISTS
12421242
| SOURCE
12431243
| INDEX
1244+
| A
1245+
| ASC
12441246
| DESC
12451247
| DATASOURCES
12461248
| FROM

ppl/src/test/java/org/opensearch/sql/ppl/parser/AstBuilderTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,42 @@ public void testSortCommandWithMultipleFieldsAndDesc() {
516516
exprList(argument("asc", booleanLiteral(true)), argument("type", nullLiteral())))));
517517
}
518518

519+
@Test
520+
public void testSortCommandWithAsc() {
521+
assertEqual(
522+
"source=t | sort f1 asc",
523+
sort(
524+
relation("t"),
525+
field(
526+
"f1",
527+
exprList(argument("asc", booleanLiteral(true)), argument("type", nullLiteral())))));
528+
}
529+
530+
@Test
531+
public void testSortCommandWithA() {
532+
assertEqual(
533+
"source=t | sort f1 a",
534+
sort(
535+
relation("t"),
536+
field(
537+
"f1",
538+
exprList(argument("asc", booleanLiteral(true)), argument("type", nullLiteral())))));
539+
}
540+
541+
@Test
542+
public void testSortCommandWithMultipleFieldsAndAsc() {
543+
assertEqual(
544+
"source=t | sort f1, f2 asc",
545+
sort(
546+
relation("t"),
547+
field(
548+
"f1",
549+
exprList(argument("asc", booleanLiteral(true)), argument("type", nullLiteral()))),
550+
field(
551+
"f2",
552+
exprList(argument("asc", booleanLiteral(true)), argument("type", nullLiteral())))));
553+
}
554+
519555
@Test
520556
public void testEvalCommand() {
521557
assertEqual(

0 commit comments

Comments
 (0)