Jackie-Jiang commented on code in PR #20366:
URL: https://github.com/apache/pinot/pull/20366#discussion_r4677923838


##########
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

Review Comment:
   Can we generalize this to preserve any existing ORDER BY and append missing 
GROUP BY expressions as tie-breakers, including queries with HAVING? Preserve 
existing directions and null ordering, then append missing expressions in GROUP 
BY order. For example, `GROUP BY a, b ORDER BY SUM(x) DESC, a DESC` becomes 
`ORDER BY SUM(x) DESC, a DESC, b ASC`.
   
   HAVING should affect safe-trimming eligibility, while 
`sortAggregateLimitThreshold` should select the combine algorithm; neither 
needs to prevent completing an existing ordering. Queries with HAVING or 
aggregate-based ordering must still be treated as unsafe for early LIMIT 
trimming. We can keep grouping sets excluded because their visible group keys 
do not uniquely identify every output group.



##########
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:
   Before enabling the rewrite more broadly, we should improve generic server 
combine for complete group-key ordering without HAVING. It can safely retain 
only LIMIT groups, but currently oversamples to `max(5 * LIMIT, 
minServerGroupTrimSize)` and can accumulate up to the configured trim 
threshold. We should optimize both intermediate retention and final output, 
then benchmark large-LIMIT queries across generic and sorted combine.
   
   This threshold alone is also not a sufficient performance bound: 
`sortAggregateLimitThreshold=100001` with `LIMIT 100000` is rewritten, as the 
new test verifies, while the supplied benchmark reports roughly 8x heap and 
3.6x runtime for that sorted-combine case. Optimizing generic combine is a 
prerequisite for broadening the rewrite, but does not by itself resolve that 
sorted-combine overhead.



##########
pinot-core/src/main/java/org/apache/pinot/core/util/GroupByUtils.java:
##########
@@ -584,7 +584,8 @@ public static IndexedTable 
createIndexedTableForCombineOperator(GroupByResultsBl
         resultSize = trimSize;
       } else {
         // TODO: Keeping only 'LIMIT' groups can cause inaccurate result 
because the groups are randomly selected
-        //       without ordering. Consider ordering on group-by columns if no 
ordering is specified.
+        //       without ordering. The single-stage broker orders such a query 
on its group-by columns when its LIMIT
+        //       is below the sort aggregate limit threshold 
(OrderByGroupKeysRewriter). Consider a larger LIMIT too.

Review Comment:
   Optional follow-up: we could use a leading group-key ORDER BY prefix to 
reduce overfetch further. Without HAVING, if that prefix includes all group 
keys, retaining only LIMIT is safe even when aggregate expressions follow, 
since those expressions cannot distinguish different groups after the full key 
tuple.
   
   If the prefix covers only some group keys, prefix-based pruning must retain 
all ties at the cutoff. For example, `GROUP BY country, city ORDER BY country, 
SUM(revenue) DESC LIMIT 10` cannot simply retain 10 local groups: cities within 
the boundary country still compete on partial aggregates. That boundary bucket 
may exceed `5 * LIMIT`, so the first ordering key being a group key is not by 
itself enough for an exact LIMIT cap.



-- 
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