From 3ba5a7ed93e9fcb6cb1e9b6be22868385aa60b1c Mon Sep 17 00:00:00 2001 From: Simeon Widdis Date: Thu, 10 Oct 2024 13:17:48 -0700 Subject: [PATCH] Add additional sorts to other locations Signed-off-by: Simeon Widdis --- .../org/opensearch/sql/legacy/executor/ElasticHitsExecutor.java | 2 ++ .../org/opensearch/sql/legacy/executor/join/ElasticUtils.java | 1 + .../org/opensearch/sql/legacy/query/DefaultQueryAction.java | 1 + .../query/planner/physical/node/pointInTime/PointInTime.java | 1 + .../sql/legacy/query/planner/physical/node/scroll/Scroll.java | 1 + 5 files changed, 6 insertions(+) diff --git a/legacy/src/main/java/org/opensearch/sql/legacy/executor/ElasticHitsExecutor.java b/legacy/src/main/java/org/opensearch/sql/legacy/executor/ElasticHitsExecutor.java index 2b80575e1e..5facc27202 100644 --- a/legacy/src/main/java/org/opensearch/sql/legacy/executor/ElasticHitsExecutor.java +++ b/legacy/src/main/java/org/opensearch/sql/legacy/executor/ElasticHitsExecutor.java @@ -19,6 +19,7 @@ import org.opensearch.common.unit.TimeValue; import org.opensearch.search.SearchHits; import org.opensearch.search.builder.PointInTimeBuilder; +import org.opensearch.search.sort.SortOrder; import org.opensearch.sql.legacy.domain.Select; import org.opensearch.sql.legacy.esdomain.LocalClusterState; import org.opensearch.sql.legacy.exception.SqlParseException; @@ -70,6 +71,7 @@ public SearchResponse getResponseWithHits( boolean ordered = select.isOrderdSelect(); if (!ordered) { request.addSort(DOC_FIELD_NAME, ASC); + request.addSort("_id", SortOrder.ASC); } // Set PIT request.setPointInTime(new PointInTimeBuilder(pit.getPitId())); diff --git a/legacy/src/main/java/org/opensearch/sql/legacy/executor/join/ElasticUtils.java b/legacy/src/main/java/org/opensearch/sql/legacy/executor/join/ElasticUtils.java index 7b6228a3d2..b19b8e27c8 100644 --- a/legacy/src/main/java/org/opensearch/sql/legacy/executor/join/ElasticUtils.java +++ b/legacy/src/main/java/org/opensearch/sql/legacy/executor/join/ElasticUtils.java @@ -39,6 +39,7 @@ public static SearchResponse scrollOneTimeWithHits( boolean ordered = originalSelect.isOrderdSelect(); if (!ordered) { scrollRequest.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC); + scrollRequest.addSort("_id", SortOrder.ASC); } SearchResponse responseWithHits = scrollRequest.get(); // on ordered select - not using SCAN , elastic returns hits on first scroll diff --git a/legacy/src/main/java/org/opensearch/sql/legacy/query/DefaultQueryAction.java b/legacy/src/main/java/org/opensearch/sql/legacy/query/DefaultQueryAction.java index 9877b17a8f..d005f64ca9 100644 --- a/legacy/src/main/java/org/opensearch/sql/legacy/query/DefaultQueryAction.java +++ b/legacy/src/main/java/org/opensearch/sql/legacy/query/DefaultQueryAction.java @@ -110,6 +110,7 @@ public void checkAndSetScroll() { boolean ordered = select.isOrderdSelect(); if (!ordered) { request.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC); + request.addSort("_id", SortOrder.ASC); } // Request also requires PointInTime, but we should create pit while execution. } else { diff --git a/legacy/src/main/java/org/opensearch/sql/legacy/query/planner/physical/node/pointInTime/PointInTime.java b/legacy/src/main/java/org/opensearch/sql/legacy/query/planner/physical/node/pointInTime/PointInTime.java index 9ddbde2d29..d20dbc66c7 100644 --- a/legacy/src/main/java/org/opensearch/sql/legacy/query/planner/physical/node/pointInTime/PointInTime.java +++ b/legacy/src/main/java/org/opensearch/sql/legacy/query/planner/physical/node/pointInTime/PointInTime.java @@ -42,6 +42,7 @@ protected void loadFirstBatch() { request .getRequestBuilder() .addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC) + .addSort("_id", SortOrder.ASC) .setSize(pageSize) .setTimeout(TimeValue.timeValueSeconds(timeout)) .setPointInTime(new PointInTimeBuilder(pitId)) diff --git a/legacy/src/main/java/org/opensearch/sql/legacy/query/planner/physical/node/scroll/Scroll.java b/legacy/src/main/java/org/opensearch/sql/legacy/query/planner/physical/node/scroll/Scroll.java index 5019e9cde8..687af673ec 100644 --- a/legacy/src/main/java/org/opensearch/sql/legacy/query/planner/physical/node/scroll/Scroll.java +++ b/legacy/src/main/java/org/opensearch/sql/legacy/query/planner/physical/node/scroll/Scroll.java @@ -40,6 +40,7 @@ protected void loadFirstBatch() { request .getRequestBuilder() .addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC) + .addSort("_id", SortOrder.ASC) .setSize(pageSize) .setScroll(TimeValue.timeValueSeconds(timeout)) .get();