Skip to content

Commit 899ef01

Browse files
committed
add Test coverage
Signed-off-by: Kai Huang <ahkcs@amazon.com>
1 parent 8c3f5f0 commit 899ef01

4 files changed

Lines changed: 57 additions & 4 deletions

File tree

docs/user/interfaces/endpoint.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ Description
275275

276276
PPL also supports the ``fetch_size`` parameter, but with different semantics from SQL. In PPL, ``fetch_size`` limits the number of rows returned in a single, complete response. **PPL does not support cursor-based pagination** — no cursor is returned and there is no way to fetch additional pages. The value of ``fetch_size`` should be greater than ``0``. In absence of ``fetch_size`` or a value of ``0``, it will use the system default behavior (no limit). The effective upper bound is governed by the ``plugins.query.size_limit`` cluster setting (defaults to ``index.max_result_window``, which is 10000).
277277

278+
``fetch_size`` can be specified either as a URL parameter or in the JSON request body. If both are provided, the JSON body value takes precedence.
279+
278280
+--------------------+-------------------------------------+------------------------------------+
279281
| Aspect | SQL ``fetch_size`` | PPL ``fetch_size`` |
280282
+====================+=====================================+====================================+
@@ -285,7 +287,7 @@ PPL also supports the ``fetch_size`` parameter, but with different semantics fro
285287
| Can fetch more? | Yes (with cursor) | No (single response) |
286288
+--------------------+-------------------------------------+------------------------------------+
287289

288-
Example
290+
Example 1: JSON body
289291
-------
290292

291293
PPL query::
@@ -295,6 +297,15 @@ PPL query::
295297
"query" : "source = accounts | fields firstname, lastname | where age > 20"
296298
}'
297299

300+
Example 2: URL parameter
301+
-------
302+
303+
PPL query::
304+
305+
>> curl -H 'Content-Type: application/json' -X POST localhost:9200/_plugins/_ppl?fetch_size=5 -d '{
306+
"query" : "source = accounts | fields firstname, lastname | where age > 20"
307+
}'
308+
298309
Result set::
299310

300311
{

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,34 @@ public void testFetchSizeLargerThanDataset() throws IOException {
123123
assertEquals(7, dataRows.length());
124124
}
125125

126+
@Test
127+
public void testFetchSizeAtSystemLimit() throws IOException {
128+
// fetch_size at the default system limit (10000) should work without error
129+
JSONObject result =
130+
executeQueryWithFetchSize(String.format("source=%s", TEST_INDEX_BANK), 10000);
131+
JSONArray dataRows = result.getJSONArray("datarows");
132+
// Bank index has 7 documents, so we get all of them
133+
assertEquals(7, dataRows.length());
134+
}
135+
136+
@Test
137+
public void testFetchSizeExceedingSystemLimitIsCapped() throws IOException {
138+
// fetch_size > system limit (10000) is accepted but capped by LogicalSystemLimit
139+
JSONObject result =
140+
executeQueryWithFetchSize(String.format("source=%s", TEST_INDEX_BANK), 10001);
141+
JSONArray dataRows = result.getJSONArray("datarows");
142+
// Bank index has 7 documents, result is capped by system limit but dataset is smaller
143+
assertEquals(7, dataRows.length());
144+
}
145+
146+
@Test
147+
public void testNegativeFetchSizeReturnsAllResults() throws IOException {
148+
// Negative fetch_size is treated as "no limit" (same as 0)
149+
JSONObject result = executeQueryWithFetchSize(String.format("source=%s", TEST_INDEX_BANK), -1);
150+
JSONArray dataRows = result.getJSONArray("datarows");
151+
assertEquals(7, dataRows.length());
152+
}
153+
126154
@Test
127155
public void testFetchSizeOne() throws IOException {
128156
JSONObject result =

plugin/src/main/java/org/opensearch/sql/plugin/request/PPLQueryRequestFactory.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,14 @@ private static PPLQueryRequest parsePPLRequestFromPayload(RestRequest restReques
8888
// Support fetch_size as a URL parameter if not already in the JSON body
8989
if (!jsonContent.has(QUERY_PARAMS_FETCH_SIZE)
9090
&& restRequest.params().containsKey(QUERY_PARAMS_FETCH_SIZE)) {
91-
jsonContent.put(
92-
QUERY_PARAMS_FETCH_SIZE,
93-
Integer.parseInt(restRequest.params().get(QUERY_PARAMS_FETCH_SIZE)));
91+
try {
92+
jsonContent.put(
93+
QUERY_PARAMS_FETCH_SIZE,
94+
Integer.parseInt(restRequest.params().get(QUERY_PARAMS_FETCH_SIZE)));
95+
} catch (NumberFormatException e) {
96+
throw new IllegalArgumentException(
97+
"Invalid fetch_size parameter: must be a valid integer", e);
98+
}
9499
}
95100
PPLQueryRequest pplRequest =
96101
new PPLQueryRequest(

ppl/src/test/java/org/opensearch/sql/ppl/domain/PPLQueryRequestTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ public void testGetFetchSizeReturnsZeroWhenJsonContentIsNull() {
7777
assertEquals(0, request.getFetchSize());
7878
}
7979

80+
@Test
81+
public void testGetFetchSizeHandlesExplicitNull() {
82+
JSONObject json = new JSONObject();
83+
json.put("query", "source=t");
84+
json.put("fetch_size", JSONObject.NULL);
85+
PPLQueryRequest request = new PPLQueryRequest("source=t", json, "/_plugins/_ppl");
86+
assertEquals(0, request.getFetchSize());
87+
}
88+
8089
@Test
8190
public void testGetFetchSizeWithLargeValue() {
8291
JSONObject json = new JSONObject("{\"query\": \"source=t\", \"fetch_size\": 15000}");

0 commit comments

Comments
 (0)