scottlan0113 opened a new pull request, #20366: URL: https://github.com/apache/pinot/pull/20366
## Summary A GROUP BY without ORDER BY can return incomplete groups. Each server stops adding groups once it has `LIMIT` of them, so different servers can keep different groups. A group the broker returns can then miss the values from the servers that dropped it. See #8039 and item 2 of #11706. With this PR, the single-stage broker adds `ORDER BY <all group-by expressions> ASC` to such a query before sending it to the servers. Then every segment, every server and the broker keep the same smallest keys, so each returned group is complete (as long as no segment hits `numGroupsLimit`), and the result is the same on every run. The rewrite only happens when the servers will use the sorted combine for the query: no HAVING, and a `LIMIT` below `sortAggregateLimitThreshold` (10,000 by default, or the query option). These queries are left as written: - queries that already have an ORDER BY - GROUPING SETS / ROLLUP / CUBE (the servers keep all their groups) - queries with HAVING, or with a `LIMIT` at or above the threshold. Ordering them would make them use more memory and run slower (numbers below). An earlier PR, #8758, added the same rewrite behind a broker config, off by default, and was closed. The concern there was the cost for queries without ORDER BY. This PR rewrites only the queries that take the sorted combine, where the rewritten query was faster at LIMIT 10 and about the same at LIMIT 1,000 (numbers below). How it works: - `OrderByGroupKeysRewriter` (pinot-common) does the rewrite. It is not in the default rewriter list. The broker calls it in `BaseSingleStageBrokerRequestHandler.compileRequest`, so materialized view definitions, the multi-stage engine and queries sent straight to a server don't change. - It reads `sortAggregateLimitThreshold` the same way the servers do. If the value is invalid, the query is left as is and the servers reject it like today. - It runs before the materialized view rewrite, so that rewrite sees the ORDER BY as if the user wrote it. ## Behavior change - For a GROUP BY without ORDER BY, without HAVING and with a `LIMIT` below the threshold, the rows now come back sorted by the group keys, and they are the smallest keys instead of whatever groups the servers built first. - `accurateGroupByWithoutOrderBy` (#15844) no longer applies to these queries in the single-stage engine, since they now have an ORDER BY. It still applies to the queries left as written. - EXPLAIN for these queries shows the sort in `BROKER_REDUCE`. ## Performance One server, existing combine, 16 segments of 200,000 rows, about 815,000 groups, `SELECT d1, SUM(m1) FROM t GROUP BY d1 ...`. Two JVMs (four for LIMIT 1,000). Each number is against the query as written in the same JVMs. | Query | Rewritten? | Heap at the peak | Wall | |---|---|---|---| | LIMIT 10 (the default) | yes | 0.72-0.73x (8.9-9.0 vs 12.3 MB) | 0.44x (11.4 vs 25.7-25.9 ms) | | LIMIT 1,000 | yes | 0.67-1.05x (7.9-13.1 vs 11.7-12.5 MB) | 0.95-1.09x (13.9-15.8 vs 14.2-14.6 ms) | | LIMIT 10,000, if ordered | no | 2.54-2.85x (33.9-35.3 vs 11.9-13.9 MB) | 8.6x (114-123 vs 13.3-14.3 ms) | | LIMIT 10,000, if ordered with `sortAggregateLimitThreshold` above the `LIMIT` | no | 2.82-2.84x (33.2-33.5 vs 11.7-11.9 MB) | 2.33-2.48x (32.3-35.4 vs 13.9-14.2 ms) | | LIMIT 100,000, if ordered | no | 6.35-6.37x (198.0-198.6 vs 31.2 MB) | 55-59x (1,205-1,251 vs 20.5-22.7 ms) | | LIMIT 100,000, if ordered with `sortAggregateLimitThreshold` above the `LIMIT` | no | 7.81-8.21x (243.7-256.1 vs 31.2 MB) | 3.57-3.74x (79.4-82.0 vs 21.2-23.0 ms) | | HAVING, LIMIT 1,000, if ordered | no | 9.7-10.5x (124.1-124.3 vs 11.9-12.8 MB) | 82-93x (1,174-1,227 vs 13.2-14.3 ms) | Raising the threshold above the `LIMIT` makes the ordered query use the sorted combine. That is faster, but it uses about the same memory at LIMIT 10,000 and more at 100,000. Each task builds a sorted table of up to `LIMIT` groups for its segment before merging, while the query as written keeps about `LIMIT` groups in one shared table. Every ordered run returned the `LIMIT` smallest keys with complete values. Without HAVING, 7 of the 210 runs of the query as written returned a group with incomplete values, on this single server. ## Tests - `OrderByGroupKeysRewriterTest`: which queries get rewritten (`LIMIT` against the default and a set threshold, HAVING, an invalid threshold), and that the result is the same as writing the ORDER BY yourself. - `GroupByWithoutOrderByQueriesTest`: two servers that build different groups first. With the rewrite, every returned group has values from both servers. Without it, each has values from only one. - `BaseSingleStageBrokerRequestHandlerTest#testGroupByWithoutOrderByIsOrderedOnGroupKeys`: the query sent to the servers and the query the materialized view rewrite sees both have the ORDER BY. A written ORDER BY is kept. A threshold set at the `LIMIT` leaves the query as written. - `OfflineTimestampIndexIntegrationTest#testGroupBySse`: the EXPLAIN plan now shows the sort in `BROKER_REDUCE`. ## Validation - Rebased on master `4b3acc7721`. The four test classes pass (3, 1, 37 and 8 tests). - spotless, license and checkstyle pass on pinot-common, pinot-core, pinot-broker and pinot-integration-tests. No compiler warnings on the added lines. ## Labels `bugfix` and `release-notes`, since the rows a GROUP BY without ORDER BY returns change. -- 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]
