Skip to content

Commit f4ea4ad

Browse files
Store TotalHits and use it to report total in response.
Signed-off-by: Yury-Fridlyand <[email protected]>
1 parent 1b5ab7e commit f4ea4ad

File tree

20 files changed

+158
-62
lines changed

20 files changed

+158
-62
lines changed

core/src/main/java/org/opensearch/sql/executor/ExecutionEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void execute(PhysicalPlan plan, ExecutionContext context,
5454
class QueryResponse {
5555
private final Schema schema;
5656
private final List<ExprValue> results;
57-
57+
private final long total;
5858
private final Cursor cursor;
5959
}
6060

core/src/main/java/org/opensearch/sql/planner/physical/PhysicalPlan.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public ExecutionEngine.Schema schema() {
5050
+ "ProjectOperator, instead of %s", this.getClass().getSimpleName()));
5151
}
5252

53+
public long getTotalHits() {
54+
return getChild().stream().mapToLong(PhysicalPlan::getTotalHits).max().orElse(0);
55+
}
56+
5357
public String toCursor() {
5458
throw new IllegalStateException(String.format("%s is not compatible with cursor feature",
5559
this.getClass().getSimpleName()));

core/src/test/java/org/opensearch/sql/executor/QueryServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ Helper executeSuccess(Split split) {
133133
invocation -> {
134134
ResponseListener<ExecutionEngine.QueryResponse> listener = invocation.getArgument(2);
135135
listener.onResponse(
136-
new ExecutionEngine.QueryResponse(schema, Collections.emptyList(),
136+
new ExecutionEngine.QueryResponse(schema, Collections.emptyList(), 0,
137137
Cursor.None));
138138
return null;
139139
})

core/src/test/java/org/opensearch/sql/executor/streaming/MicroBatchStreamingExecutionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ Helper executeSuccess(Long... offsets) {
170170
ResponseListener<ExecutionEngine.QueryResponse> listener =
171171
invocation.getArgument(2);
172172
listener.onResponse(
173-
new ExecutionEngine.QueryResponse(null, Collections.emptyList(),
173+
new ExecutionEngine.QueryResponse(null, Collections.emptyList(), 0,
174174
Cursor.None));
175175

176176
PlanContext planContext = invocation.getArgument(1);

core/src/test/java/org/opensearch/sql/planner/physical/PhysicalPlanTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@
55

66
package org.opensearch.sql.planner.physical;
77

8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.mockito.Mockito.CALLS_REAL_METHODS;
10+
import static org.mockito.Mockito.mock;
811
import static org.mockito.Mockito.verify;
12+
import static org.mockito.Mockito.when;
913

1014
import java.util.List;
15+
16+
import org.junit.jupiter.api.DisplayNameGeneration;
17+
import org.junit.jupiter.api.DisplayNameGenerator;
1118
import org.junit.jupiter.api.Test;
1219
import org.junit.jupiter.api.extension.ExtendWith;
1320
import org.mockito.Mock;
@@ -16,6 +23,7 @@
1623
import org.opensearch.sql.storage.split.Split;
1724

1825
@ExtendWith(MockitoExtension.class)
26+
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
1927
class PhysicalPlanTest {
2028
@Mock
2129
Split split;
@@ -46,8 +54,25 @@ public List<PhysicalPlan> getChild() {
4654
};
4755

4856
@Test
49-
void addSplitToChildByDefault() {
57+
void add_split_to_child_by_default() {
5058
testPlan.add(split);
5159
verify(child).add(split);
5260
}
61+
62+
@Test
63+
void get_total_hits_from_child() {
64+
var plan = mock(PhysicalPlan.class);
65+
when(child.getTotalHits()).thenReturn(42L);
66+
when(plan.getChild()).thenReturn(List.of(child));
67+
when(plan.getTotalHits()).then(CALLS_REAL_METHODS);
68+
assertEquals(42, plan.getTotalHits());
69+
verify(child).getTotalHits();
70+
}
71+
72+
@Test
73+
void get_total_hits_uses_default_value() {
74+
var plan = mock(PhysicalPlan.class);
75+
when(plan.getTotalHits()).then(CALLS_REAL_METHODS);
76+
assertEquals(0, plan.getTotalHits());
77+
}
5378
}

core/src/testFixtures/java/org/opensearch/sql/executor/DefaultExecutionEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void execute(
3434
result.add(plan.next());
3535
}
3636
QueryResponse response = new QueryResponse(new Schema(new ArrayList<>()), new ArrayList<>(),
37-
Cursor.None);
37+
0, Cursor.None);
3838
listener.onResponse(response);
3939
} catch (Exception e) {
4040
listener.onFailure(e);

legacy/src/main/java/org/opensearch/sql/legacy/plugin/RestSQLQueryAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ private ResponseListener<QueryResponse> createQueryResponseListener(
172172
@Override
173173
public void onResponse(QueryResponse response) {
174174
sendResponse(channel, OK,
175-
formatter.format(new QueryResult(response.getSchema(), response.getResults(), response.getCursor())));
175+
formatter.format(new QueryResult(response.getSchema(), response.getResults(),
176+
response.getCursor(), response.getTotal())));
176177
}
177178

178179
@Override

opensearch/src/main/java/org/opensearch/sql/opensearch/executor/OpenSearchExecutionEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void execute(PhysicalPlan physicalPlan, ExecutionContext context,
5353

5454
Cursor qc = paginatedPlanCache.convertToCursor(plan);
5555

56-
QueryResponse response = new QueryResponse(physicalPlan.schema(), result, qc);
56+
QueryResponse response = new QueryResponse(physicalPlan.schema(), result, plan.getTotalHits(), qc);
5757
listener.onResponse(response);
5858
} catch (Exception e) {
5959
listener.onFailure(e);

opensearch/src/main/java/org/opensearch/sql/opensearch/executor/protector/ResourceMonitorPlan.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ public ExprValue next() {
8383
return delegate.next();
8484
}
8585

86+
@Override
87+
public long getTotalHits() {
88+
return delegate.getTotalHits();
89+
}
90+
8691
@Override
8792
public String toCursor() {
8893
return delegate.toCursor();

opensearch/src/main/java/org/opensearch/sql/opensearch/response/OpenSearchResponse.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ public OpenSearchResponse(SearchHits hits, OpenSearchExprValueFactory exprValueF
7171
*/
7272
public boolean isEmpty() {
7373
return (hits.getHits() == null) || (hits.getHits().length == 0) && aggregations == null;
74-
// TODO TBD ^ ^
74+
}
75+
76+
public long getTotalHits() {
77+
return hits.getTotalHits().value;
7578
}
7679

7780
public boolean isAggregationResponse() {

0 commit comments

Comments
 (0)