Skip to content

Commit 3903d36

Browse files
committed
Sort earliest results with asc order
Signed-off-by: Yuanchun Shen <[email protected]>
1 parent 955c713 commit 3903d36

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

core/src/main/java/org/opensearch/sql/calcite/CalciteRelNodeVisitor.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2103,8 +2103,11 @@ private RelNode buildTopCategoriesQuery(
21032103

21042104
// Apply sorting and limit to non-null categories only
21052105
RexNode sortField = context.relBuilder.field("grand_total");
2106+
// For MIN and EARLIEST, top results should be the minimum ones
21062107
sortField =
2107-
aggFunction == BuiltinFunctionName.MIN ? sortField : context.relBuilder.desc(sortField);
2108+
aggFunction == BuiltinFunctionName.MIN || aggFunction == BuiltinFunctionName.EARLIEST
2109+
? sortField
2110+
: context.relBuilder.desc(sortField);
21082111
context.relBuilder.sort(sortField);
21092112
if (limit > 0) {
21102113
context.relBuilder.limit(0, limit);

integ-test/src/yamlRestTest/resources/rest-api-spec/test/issues/4582.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ teardown:
9898
- match: { total: 3 }
9999
- match: { "schema": [{"name": "@timestamp", "type": "timestamp"}, {"name": "severityText", "type": "string"}, {"name": "earliest(@timestamp)", "type": "timestamp"}] }
100100
- match: { "datarows": [
101-
["2024-01-15 10:30:00", "CUSTOM", "2024-01-15 10:30:11.567890123"],
102-
["2024-01-15 10:30:00", "OTHER", "2024-01-15 10:30:04.567890123"],
103-
["2024-01-15 10:30:00", "TRACE", "2024-01-15 10:30:10.567890123"]] }
101+
["2024-01-15 10:30:00", "INFO", "2024-01-15 10:30:04.567890123"],
102+
["2024-01-15 10:30:00", "OTHER", "2024-01-15 10:30:06.567890123"],
103+
["2024-01-15 10:30:00", "WARN", "2024-01-15 10:30:05.567890123"]] }
104104

105105
---
106106
"timechart count aggregation with limit should sum OTHER values":

0 commit comments

Comments
 (0)