shihchengwei-lab commented on PR #20401:
URL: https://github.com/apache/pinot/pull/20401#issuecomment-7230456199

   `HAVING` can exhaust the retained candidates before `OFFSET` is applied, so 
this PR can still return an empty page while a matching group exists.
   
   On `9b55211fd176037851c50b892bf2024cd8bda50a`, I reduced two intermediate 
tables containing keys 1–5001. Keys below 5000 had `COUNT(*) = 2`; keys 5000 
and 5001 had `COUNT(*) = 1`. For:
   
   ```sql
   SELECT k, COUNT(*) AS c FROM test GROUP BY k
   HAVING COUNT(*) = 1 ORDER BY k LIMIT 1 OFFSET 1
   ```
   
   the expected row is `5001`, but the PR returned an empty page. With 
`minGroupTrimSize = 5000`, [`candidateLimit` becomes 2 while the `HAVING` path 
still retains at most `trimSize` 5000 
groups](https://github.com/apache/pinot/blob/9b55211fd176037851c50b892bf2024cd8bda50a/pinot-core/src/main/java/org/apache/pinot/core/util/GroupByUtils.java#L623-L636).
 Sorting by `k` therefore drops key 5001 before [the reducer skips one matching 
row for 
`OFFSET`](https://github.com/apache/pinot/blob/9b55211fd176037851c50b892bf2024cd8bda50a/pinot-core/src/main/java/org/apache/pinot/core/query/reduce/GroupByDataTableReducer.java#L204-L225).
   
   The same case on base `d197e0b5fc4013dc79ebd37039d09a2d958280c2` returned 
`5000` because base ignored `OFFSET`. The candidate-trim limitation therefore 
predates this PR, but the post-`HAVING` pagination described here remains 
incomplete: this case changes from the wrong row to an empty page with no error.
   
   <details>
   <summary>Minimal reproduction</summary>
   
   Apply this test to `GroupByOffsetTest`:
   
   ```java
   @Test
   public void testOffsetAfterHavingSurvivesDefaultCandidateTrim() throws 
Exception {
     try (QueryThreadContext ignored = QueryThreadContext.openForSseTest();
         ExecutorService executor = Executors.newSingleThreadExecutor()) {
       QueryContext query = QueryContextConverterUtils.getQueryContext(
           "SELECT k, COUNT(*) AS c FROM test GROUP BY k "
               + "HAVING COUNT(*) = 1 ORDER BY k LIMIT 1 OFFSET 1");
       DataSchema schema = new DataSchema(
           new String[]{"k", 
query.getAggregationFunctions()[0].getResultColumnName()},
           new ColumnDataType[]{ColumnDataType.INT, ColumnDataType.LONG});
       ServerRoutingInstance first = new ServerRoutingInstance("localhost", 1, 
TableType.OFFLINE);
       ServerRoutingInstance second = new ServerRoutingInstance("localhost", 2, 
TableType.OFFLINE);
       Map<ServerRoutingInstance, DataTable> results = Map.of(
           first, tableRange(schema, 1, 2500), second, tableRange(schema, 2501, 
5001));
   
       BrokerResponseNative response = new BrokerResponseNative();
       new GroupByDataTableReducer(query).reduceAndSetResults("test", schema, 
results, response,
           new DataTableReducerContext(executor, 1, 10000, 0, 5000, 16), null);
   
       List<Object[]> rows = response.getResultTable().getRows();
       assertEquals(rows.size(), 1);
       assertEquals(rows.get(0), new Object[]{5001, 1L});
     }
   }
   
   private static DataTable tableRange(DataSchema schema, int firstKey, int 
lastKey) throws Exception {
     DataTableBuilder builder = 
DataTableBuilderFactory.getDataTableBuilder(schema);
     for (int key = firstKey; key <= lastKey; key++) {
       builder.startRow();
       builder.setColumn(0, key);
       builder.setColumn(1, key >= 5000 ? 1L : 2L);
       builder.finishRow();
     }
     return DataTableFactory.getDataTable(builder.build().toBytes());
   }
   ```
   
   Run:
   
   ```text
   ./mvnw -pl pinot-core -am 
'-Dtest=GroupByOffsetTest#testOffsetAfterHavingSurvivesDefaultCandidateTrim' 
'-Dsurefire.failIfNoSpecifiedTests=false' test
   ```
   
   PR head fails with `expected [1] but found [0]`; base fails with `expected 
[5001] but found [5000]`.
   
   </details>
   
   Could the broker avoid trimming before `HAVING`, or otherwise retain enough 
matching candidates to satisfy `OFFSET + LIMIT`?
   
   AI assisted with the code review, reproduction test design, and wording. I 
ran the reducer case against both commits above.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to