scottlan0113 commented on code in PR #20366: URL: https://github.com/apache/pinot/pull/20366#discussion_r4711737909
########## pinot-common/src/main/java/org/apache/pinot/sql/parsers/rewriter/OrderByGroupKeysRewriter.java: ########## @@ -0,0 +1,90 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pinot.sql.parsers.rewriter; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.apache.pinot.common.request.Expression; +import org.apache.pinot.common.request.PinotQuery; +import org.apache.pinot.common.utils.config.QueryOptionsUtils; +import org.apache.pinot.common.utils.request.RequestUtils; +import org.apache.pinot.spi.utils.CommonConstants.Server; +import org.apache.pinot.sql.parsers.CalciteSqlParser; + + +/// Orders a GROUP BY query without an ORDER BY on all its group-by expressions, ascending, when the servers then merge +/// it in the sorted combine: without HAVING, and with a `LIMIT` below `sortAggregateLimitThreshold`, as in +/// `QueryContext.shouldSortAggregateUnderSafeTrim()`. +/// +/// Without an ORDER BY, a server stops adding groups once it holds enough of them for the result, so two servers can +/// keep different groups, and a group the broker returns can miss the values of the servers that dropped it. Ordered on +/// the group keys, every segment, server and the broker keep the smallest keys, so the servers keep the same groups and +/// the result is the same on every run. Without HAVING the trim is safe: each segment keeps only `LIMIT` groups, and +/// each returned group is complete as long as no segment reaches `numGroupsLimit`. +/// +/// Not rewritten: +/// - a query that already has an ORDER BY; +/// - a GROUPING SETS, ROLLUP or CUBE query: the servers keep all its groups, so none of them misses values; +/// - a query with HAVING, or with a `LIMIT` at or above `sortAggregateLimitThreshold`: the servers would not take the +/// sorted combine, and in the other combines the ordered query keeps more groups than the query as written and sorts +/// them, which costs more memory and time. +/// +/// This rewriter is not in [QueryRewriterFactory#DEFAULT_QUERY_REWRITERS_CLASS_NAMES]. The single-stage broker applies +/// it to the query it sends to the servers, so materialized view definitions and the multi-stage engine are unchanged. +/// +/// E.g. +/// SELECT col1, SUM(col2) FROM foo GROUP BY col1 LIMIT 10 +/// --> SELECT col1, SUM(col2) FROM foo GROUP BY col1 ORDER BY col1 LIMIT 10 +/// +/// This rewriter is stateless and thread-safe; each invocation mutates its own query. +public class OrderByGroupKeysRewriter implements QueryRewriter { + + @Override + public PinotQuery rewrite(PinotQuery pinotQuery) { + if (pinotQuery.getGroupByListSize() == 0 || pinotQuery.getOrderByListSize() > 0 + || pinotQuery.getGroupingSets() != null || pinotQuery.getHavingExpression() != null + || !isLimitBelowSortAggregateLimitThreshold(pinotQuery)) { + return pinotQuery; + } + List<Expression> groupByList = pinotQuery.getGroupByList(); + List<Expression> orderByList = new ArrayList<>(groupByList.size()); + for (Expression groupBy : groupByList) { + // Copied, as the parser builds a written ORDER BY, so that a later rewrite of one list does not change the other + orderByList.add(RequestUtils.getFunctionExpression(CalciteSqlParser.ASC, groupBy.deepCopy())); + } + pinotQuery.setOrderByList(orderByList); + return pinotQuery; + } + + /// Reads `sortAggregateLimitThreshold` as the servers read it. An invalid value leaves the query unchanged, for the + /// servers to reject. + private static boolean isLimitBelowSortAggregateLimitThreshold(PinotQuery pinotQuery) { + Map<String, String> queryOptions = pinotQuery.getQueryOptions(); + Integer sortAggregateLimitThreshold; + try { + sortAggregateLimitThreshold = + queryOptions != null ? QueryOptionsUtils.getSortAggregateLimitThreshold(queryOptions) : null; + } catch (IllegalArgumentException e) { + return false; + } + return pinotQuery.getLimit() < (sortAggregateLimitThreshold != null ? sortAggregateLimitThreshold + : Server.DEFAULT_SORT_AGGREGATE_LIMIT_THRESHOLD); Review Comment: Done in cbf7c27c9c. For complete group-key ordering without HAVING, the generic server combine and the broker reduce now keep LIMIT groups instead of max(5 * LIMIT, min trim size), and trim at 2 * LIMIT instead of at the trim threshold. Large-LIMIT benchmark: `SELECT d1, SUM(m1) FROM t GROUP BY d1 [ORDER BY d1] LIMIT n`, one server, 16 segments of 200,000 rows (each hits `numGroupsLimit` 100,000), master ed05b38759, 2 JVMs. Heap at the peak, wall: | | LIMIT 10,000 | LIMIT 100,000 | |---|---|---| | No ORDER BY | 11.7-11.8 MB, 13.9-14.5 ms | 31.2 MB, 21.7-22.5 ms | | Generic combine, before | 33.6-35.3 MB, 120 ms | 198.3-199.1 MB, 1,263-1,276 ms | | Generic combine, this PR | 34.3-35.3 MB, 130-135 ms | 48.3 MB, 1,452-1,469 ms | | Sorted combine (threshold above the LIMIT) | 33.1-35.3 MB, 31-34 ms | 243.2-255.1 MB, 77 ms | At LIMIT 100,000 this PR takes 0.24x the heap and 1.15x the wall of before; at LIMIT 10,000, the same heap and 1.08-1.13x the wall. The sorted combine is 18.7-19.0x faster than this PR at LIMIT 100,000, with 5.0-5.3x the heap. All ordered runs were exact against the answer under the same `numGroupsLimit`. The rewrite stays on the sorted-combine case for now, with a TODO for the other cases (408e61ca61). The description is updated and states the sorted-combine cost at LIMIT 100,000: 7.8-8.2x the heap and about 3.5x the wall of the query as written. -- 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]
