You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by "yashmayya (via GitHub)" <gi...@apache.org> on 2026/03/30 22:36:15 UTC
[PR] Implement streaming group by leaf stage operator for multi-stage engine [pinot]
yashmayya opened a new pull request, #18035:
URL: https://github.com/apache/pinot/pull/18035
- Adds `StreamingGroupByCombineOperator`, a new combine operator that flushes partial group-by results when accumulated groups reach a configurable threshold, instead of buffering all groups in memory before returning. This bounds server memory usage for high-cardinality group-by queries on MSE leaf stages while still performing partial aggregation to reduce data volume.
- This offers a middle ground between skipping leaf stage group by (`is_skip_leaf_stage_group_by`) and full leaf stage group by.
- Introduces the `streamingGroupByFlushThreshold` query option (e.g., `SET streamingGroupByFlushThreshold = 5000`) to control the flush threshold. When unset or 0, the existing `GroupByCombineOperator` is used.
- Note that we disable trimming and result size bounding on the indexed table because the streaming operator is managing memory bounding externally via the flush threshold. When the table reaches the flush threshold, the operator drains and replaces it with a fresh table. Allowing the `IndexedTable` to independently trim or cap records would silently drop groups before flushing, producing incorrect partial aggregates that the downstream `FINAL` stage cannot recover from.
- Correctness is maintained because hash exchange routes the same group key to the same `FINAL` worker and `AggregationFunction.merge()` is associative, so partial results from multiple flushes merge correctly.
--
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: commits-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
Re: [PR] Implement streaming group by leaf stage operator for multi-stage engine [pinot]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #18035:
URL: https://github.com/apache/pinot/pull/18035#issuecomment-4158777058
## [Codecov](https://app.codecov.io/gh/apache/pinot/pull/18035?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:x: Patch coverage is `72.38095%` with `29 lines` in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 55.52%. Comparing base ([`144daa0`](https://app.codecov.io/gh/apache/pinot/commit/144daa04436bc2689657fc24059ca9107f0b7d42?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`8258b70`](https://app.codecov.io/gh/apache/pinot/commit/8258b704c300377e65b99db9f3e09ad7be25a64f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
:warning: Report is 4 commits behind head on master.
| [Files with missing lines](https://app.codecov.io/gh/apache/pinot/pull/18035?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
|---|---|---|
| [...tor/streaming/StreamingGroupByCombineOperator.java](https://app.codecov.io/gh/apache/pinot/pull/18035?src=pr&el=tree&filepath=pinot-core%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fpinot%2Fcore%2Foperator%2Fstreaming%2FStreamingGroupByCombineOperator.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cGlub3QtY29yZS9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcGlub3QvY29yZS9vcGVyYXRvci9zdHJlYW1pbmcvU3RyZWFtaW5nR3JvdXBCeUNvbWJpbmVPcGVyYXRvci5qYXZh) | 73.23% | [10 Missing and 9 partials :warning: ](https://app.codecov.io/gh/apache/pinot/pull/18035?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [...va/org/apache/pinot/core/plan/CombinePlanNode.java](https://app.codecov.io/gh/apache/pinot/pull/18035?src=pr&el=tree&filepath=pinot-core%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fpinot%2Fcore%2Fplan%2FCombinePlanNode.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cGlub3QtY29yZS9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcGlub3QvY29yZS9wbGFuL0NvbWJpbmVQbGFuTm9kZS5qYXZh) | 76.00% | [2 Missing and 4 partials :warning: ](https://app.codecov.io/gh/apache/pinot/pull/18035?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [...pinot/core/plan/maker/InstancePlanMakerImplV2.java](https://app.codecov.io/gh/apache/pinot/pull/18035?src=pr&el=tree&filepath=pinot-core%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fpinot%2Fcore%2Fplan%2Fmaker%2FInstancePlanMakerImplV2.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cGlub3QtY29yZS9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcGlub3QvY29yZS9wbGFuL21ha2VyL0luc3RhbmNlUGxhbk1ha2VySW1wbFYyLmphdmE=) | 50.00% | [1 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/pinot/pull/18035?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [...pinot/core/query/request/context/QueryContext.java](https://app.codecov.io/gh/apache/pinot/pull/18035?src=pr&el=tree&filepath=pinot-core%2Fsrc%2Fmain%2Fjava%2Forg%2Fapache%2Fpinot%2Fcore%2Fquery%2Frequest%2Fcontext%2FQueryContext.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-cGlub3QtY29yZS9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcGlub3QvY29yZS9xdWVyeS9yZXF1ZXN0L2NvbnRleHQvUXVlcnlDb250ZXh0LmphdmE=) | 33.33% | [2 Missing :warning: ](https://app.codecov.io/gh/apache/pinot/pull/18035?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
> :exclamation: There is a different number of reports uploaded between BASE (144daa0) and HEAD (8258b70). Click for more details.
>
> <details><summary>HEAD has 36 uploads less than BASE</summary>
>
>| Flag | BASE (144daa0) | HEAD (8258b70) |
>|------|------|------|
>|java-21|5|1|
>|unittests1|2|1|
>|unittests|4|1|
>|temurin|10|1|
>|java-11|5|0|
>|unittests2|2|0|
>|integration|6|0|
>|integration2|2|0|
>|integration1|2|0|
>|custom-integration1|2|0|
></details>
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## master #18035 +/- ##
============================================
- Coverage 63.29% 55.52% -7.78%
+ Complexity 1543 745 -798
============================================
Files 3200 2503 -697
Lines 194169 143211 -50958
Branches 29915 22985 -6930
============================================
- Hits 122900 79512 -43388
+ Misses 61614 56939 -4675
+ Partials 9655 6760 -2895
```
| [Flag](https://app.codecov.io/gh/apache/pinot/pull/18035/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
|---|---|---|
| [custom-integration1](https://app.codecov.io/gh/apache/pinot/pull/18035/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [integration](https://app.codecov.io/gh/apache/pinot/pull/18035/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [integration1](https://app.codecov.io/gh/apache/pinot/pull/18035/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [integration2](https://app.codecov.io/gh/apache/pinot/pull/18035/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [java-11](https://app.codecov.io/gh/apache/pinot/pull/18035/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
| [java-21](https://app.codecov.io/gh/apache/pinot/pull/18035/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `55.52% <72.38%> (-7.73%)` | :arrow_down: |
| [temurin](https://app.codecov.io/gh/apache/pinot/pull/18035/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `55.52% <72.38%> (-7.78%)` | :arrow_down: |
| [unittests](https://app.codecov.io/gh/apache/pinot/pull/18035/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `55.52% <72.38%> (-7.78%)` | :arrow_down: |
| [unittests1](https://app.codecov.io/gh/apache/pinot/pull/18035/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `55.52% <72.38%> (-0.02%)` | :arrow_down: |
| [unittests2](https://app.codecov.io/gh/apache/pinot/pull/18035/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
</details>
[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/pinot/pull/18035?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
<details><summary> :rocket: New features to boost your workflow: </summary>
- :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
--
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: commits-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
Re: [PR] Implement streaming group by leaf stage operator for multi-stage engine [pinot]
Posted by "yashmayya (via GitHub)" <gi...@apache.org>.
yashmayya commented on code in PR #18035:
URL: https://github.com/apache/pinot/pull/18035#discussion_r3017030944
##########
pinot-core/src/main/java/org/apache/pinot/core/operator/streaming/StreamingGroupByCombineOperator.java:
##########
@@ -0,0 +1,191 @@
+/**
+ * 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.core.operator.streaming;
+
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.common.utils.HashUtil;
+import org.apache.pinot.core.common.Operator;
+import org.apache.pinot.core.data.table.IntermediateRecord;
+import org.apache.pinot.core.data.table.Key;
+import org.apache.pinot.core.data.table.Record;
+import org.apache.pinot.core.data.table.SimpleIndexedTable;
+import org.apache.pinot.core.operator.blocks.results.BaseResultsBlock;
+import org.apache.pinot.core.operator.blocks.results.ExceptionResultsBlock;
+import org.apache.pinot.core.operator.blocks.results.GroupByResultsBlock;
+import org.apache.pinot.core.operator.blocks.results.MetadataResultsBlock;
+import org.apache.pinot.core.query.aggregation.function.AggregationFunction;
+import org.apache.pinot.core.query.aggregation.groupby.AggregationGroupByResult;
+import org.apache.pinot.core.query.aggregation.groupby.GroupKeyGenerator;
+import org.apache.pinot.core.query.request.context.QueryContext;
+import org.apache.pinot.core.query.scheduler.resources.ResourceManager;
+import org.apache.pinot.spi.exception.QueryErrorCode;
+import org.apache.pinot.spi.query.QueryThreadContext;
+
+
+/// Streaming combine operator for group-by queries. Instead of accumulating all groups into a single IndexedTable
+/// before returning (like {@link org.apache.pinot.core.operator.combine.GroupByCombineOperator}), this operator
+/// flushes partial results when the number of accumulated groups reaches a configurable threshold.
+///
+/// <p>This bounds server memory usage for high-cardinality group-by queries on MSE leaf stages, while still
+/// performing partial aggregation to reduce data volume compared to skipping leaf-stage group-by entirely.
+///
+/// <p>The downstream FINAL stage merges partial results from multiple flushes correctly because:
+/// <ul>
+/// <li>Hash exchange routes the same group key to the same FINAL worker</li>
+/// <li>AggregationFunction.merge() is associative</li>
+/// </ul>
+@SuppressWarnings({"rawtypes", "unchecked"})
+public class StreamingGroupByCombineOperator extends BaseStreamingCombineOperator<GroupByResultsBlock> {
+ private static final String EXPLAIN_NAME = "STREAMING_COMBINE_GROUP_BY";
+
+ private final int _flushThreshold;
+ private final int _numAggregationFunctions;
+ private final int _numGroupByExpressions;
+ private final int _numColumns;
+
+ // Main-thread-only state for accumulating group-by results
+ private DataSchema _dataSchema;
+ private SimpleIndexedTable _indexedTable;
+
+ public StreamingGroupByCombineOperator(List<Operator> operators, QueryContext queryContext,
+ ExecutorService executorService, int flushThreshold) {
+ super(null, operators, overrideMaxExecutionThreads(queryContext, operators.size()), executorService);
+ _flushThreshold = flushThreshold;
+
+ AggregationFunction[] aggregationFunctions = _queryContext.getAggregationFunctions();
+ assert aggregationFunctions != null;
+ _numAggregationFunctions = aggregationFunctions.length;
+ assert _queryContext.getGroupByExpressions() != null;
+ _numGroupByExpressions = _queryContext.getGroupByExpressions().size();
+ _numColumns = _numGroupByExpressions + _numAggregationFunctions;
+ }
+
+ /// For group-by queries, when maxExecutionThreads is not explicitly configured, override it to create as many tasks
+ /// as the default number of query worker threads (or the number of operators / segments if that's lower).
+ private static QueryContext overrideMaxExecutionThreads(QueryContext queryContext, int numOperators) {
+ int maxExecutionThreads = queryContext.getMaxExecutionThreads();
+ if (maxExecutionThreads <= 0) {
+ queryContext.setMaxExecutionThreads(Math.min(numOperators, ResourceManager.DEFAULT_QUERY_WORKER_THREADS));
+ }
+ return queryContext;
+ }
+
+ @Override
+ public String toExplainString() {
+ return EXPLAIN_NAME;
+ }
+
+ @Override
+ protected boolean isQuerySatisfied(GroupByResultsBlock resultsBlock, Object tracker) {
+ return false;
+ }
+
+ /// Polls per-segment result blocks from worker threads, merges them into a SimpleIndexedTable, and flushes
+ /// when the table reaches the flush threshold. Returns one block per call:
+ /// - A GroupByResultsBlock when flushing accumulated groups
+ /// - A MetadataResultsBlock when all operators are done and remaining data has been flushed
+ /// - An ExceptionResultsBlock on error or timeout
+ @Override
+ protected BaseResultsBlock getNextBlock() {
+ long endTimeMs = _queryContext.getEndTimeMs();
+ try {
+ while (_numOperatorsFinished < _numOperators) {
+ QueryThreadContext.checkTermination(this::getExplainName);
+ BaseResultsBlock resultsBlock =
+ _blockingQueue.poll(endTimeMs - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
+ if (resultsBlock == null) {
+ throw QueryErrorCode.EXECUTION_TIMEOUT.asException("Timed out while streaming group-by results");
+ }
+ if (resultsBlock instanceof ExceptionResultsBlock) {
+ return checkTerminateExceptionAndAttachExecutionStats(resultsBlock);
+ }
+ if (resultsBlock == LAST_RESULTS_BLOCK) {
+ _numOperatorsFinished++;
+ continue;
+ }
+ mergeBlock((GroupByResultsBlock) resultsBlock);
+ if (_indexedTable != null && _indexedTable.size() >= _flushThreshold) {
+ return flushTable();
+ }
Review Comment:
The number of groups a single segment block can contribute is already bounded by `numGroupsLimit` (default 100K). That limit is enforced in the segment-level `GroupKeyGenerator` before results ever reach the combine operator. So the worst-case overshoot after merging one block is `numGroupsLimit`, which is an existing, accepted per-segment memory bound. I don't think adding mid merge flush here is worth the additional complexity.
--
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: commits-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
Re: [PR] Implement streaming group by leaf stage operator for multi-stage engine [pinot]
Posted by "yashmayya (via GitHub)" <gi...@apache.org>.
yashmayya merged PR #18035:
URL: https://github.com/apache/pinot/pull/18035
--
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: commits-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
Re: [PR] Implement streaming group by leaf stage operator for multi-stage engine [pinot]
Posted by "yashmayya (via GitHub)" <gi...@apache.org>.
yashmayya commented on PR #18035:
URL: https://github.com/apache/pinot/pull/18035#issuecomment-4172746890
> StreamingGroupByCombineOperator does not carry groupsTrimmed / numGroupsLimitReached / numGroupsWarningLimitReached into emitted blocks. I guess the streaming threashold is expected to be larger than numGroupsLimit, but AFAIS there is nothing enforncing that.
Good point, there is indeed nothing enforcing that the streaming threshold is _smaller_ (which is what I think you meant instead of larger) than the num groups limit, so we should wire that metadata in. In fact, we should be doing that regardless of the streaming threshold because the num groups limit can be breached even within a single segment so we should be carrying forward that metadata. And while we do explicitly disable group trimming in the streaming combine operator (because it doesn't make sense to have both a streaming flush threshold and trimming IMO), we still need to forward that metadata from the individual segment level operator blocks. I've made these changes.
> We now use the threshold to pre-allocate stuff. I think that is fine, but the side effect is that a large threshold means we are going to use more memory than without streaming, right?
Yeah you're right - this is optimized for the expectation that the user sets the right thresholds for a particular query. Maybe we can revisit this in a future patch to find a more optimal initial allocation in case the streaming flush threshold is set to a super large value?
--
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: commits-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
Re: [PR] Implement streaming group by leaf stage operator for multi-stage engine [pinot]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #18035:
URL: https://github.com/apache/pinot/pull/18035#discussion_r3012850773
##########
pinot-core/src/main/java/org/apache/pinot/core/operator/streaming/StreamingGroupByCombineOperator.java:
##########
@@ -0,0 +1,191 @@
+/**
+ * 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.core.operator.streaming;
+
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.common.utils.HashUtil;
+import org.apache.pinot.core.common.Operator;
+import org.apache.pinot.core.data.table.IntermediateRecord;
+import org.apache.pinot.core.data.table.Key;
+import org.apache.pinot.core.data.table.Record;
+import org.apache.pinot.core.data.table.SimpleIndexedTable;
+import org.apache.pinot.core.operator.blocks.results.BaseResultsBlock;
+import org.apache.pinot.core.operator.blocks.results.ExceptionResultsBlock;
+import org.apache.pinot.core.operator.blocks.results.GroupByResultsBlock;
+import org.apache.pinot.core.operator.blocks.results.MetadataResultsBlock;
+import org.apache.pinot.core.query.aggregation.function.AggregationFunction;
+import org.apache.pinot.core.query.aggregation.groupby.AggregationGroupByResult;
+import org.apache.pinot.core.query.aggregation.groupby.GroupKeyGenerator;
+import org.apache.pinot.core.query.request.context.QueryContext;
+import org.apache.pinot.core.query.scheduler.resources.ResourceManager;
+import org.apache.pinot.spi.exception.QueryErrorCode;
+import org.apache.pinot.spi.query.QueryThreadContext;
+
+
+/// Streaming combine operator for group-by queries. Instead of accumulating all groups into a single IndexedTable
+/// before returning (like {@link org.apache.pinot.core.operator.combine.GroupByCombineOperator}), this operator
+/// flushes partial results when the number of accumulated groups reaches a configurable threshold.
+///
+/// <p>This bounds server memory usage for high-cardinality group-by queries on MSE leaf stages, while still
+/// performing partial aggregation to reduce data volume compared to skipping leaf-stage group-by entirely.
+///
+/// <p>The downstream FINAL stage merges partial results from multiple flushes correctly because:
+/// <ul>
+/// <li>Hash exchange routes the same group key to the same FINAL worker</li>
+/// <li>AggregationFunction.merge() is associative</li>
+/// </ul>
+@SuppressWarnings({"rawtypes", "unchecked"})
+public class StreamingGroupByCombineOperator extends BaseStreamingCombineOperator<GroupByResultsBlock> {
+ private static final String EXPLAIN_NAME = "STREAMING_COMBINE_GROUP_BY";
+
+ private final int _flushThreshold;
+ private final int _numAggregationFunctions;
+ private final int _numGroupByExpressions;
+ private final int _numColumns;
+
+ // Main-thread-only state for accumulating group-by results
+ private DataSchema _dataSchema;
+ private SimpleIndexedTable _indexedTable;
+
+ public StreamingGroupByCombineOperator(List<Operator> operators, QueryContext queryContext,
+ ExecutorService executorService, int flushThreshold) {
+ super(null, operators, overrideMaxExecutionThreads(queryContext, operators.size()), executorService);
+ _flushThreshold = flushThreshold;
+
+ AggregationFunction[] aggregationFunctions = _queryContext.getAggregationFunctions();
+ assert aggregationFunctions != null;
+ _numAggregationFunctions = aggregationFunctions.length;
+ assert _queryContext.getGroupByExpressions() != null;
+ _numGroupByExpressions = _queryContext.getGroupByExpressions().size();
+ _numColumns = _numGroupByExpressions + _numAggregationFunctions;
+ }
+
+ /// For group-by queries, when maxExecutionThreads is not explicitly configured, override it to create as many tasks
+ /// as the default number of query worker threads (or the number of operators / segments if that's lower).
+ private static QueryContext overrideMaxExecutionThreads(QueryContext queryContext, int numOperators) {
+ int maxExecutionThreads = queryContext.getMaxExecutionThreads();
+ if (maxExecutionThreads <= 0) {
+ queryContext.setMaxExecutionThreads(Math.min(numOperators, ResourceManager.DEFAULT_QUERY_WORKER_THREADS));
+ }
+ return queryContext;
+ }
+
+ @Override
+ public String toExplainString() {
+ return EXPLAIN_NAME;
+ }
+
+ @Override
+ protected boolean isQuerySatisfied(GroupByResultsBlock resultsBlock, Object tracker) {
+ return false;
+ }
+
+ /// Polls per-segment result blocks from worker threads, merges them into a SimpleIndexedTable, and flushes
+ /// when the table reaches the flush threshold. Returns one block per call:
+ /// - A GroupByResultsBlock when flushing accumulated groups
+ /// - A MetadataResultsBlock when all operators are done and remaining data has been flushed
+ /// - An ExceptionResultsBlock on error or timeout
+ @Override
+ protected BaseResultsBlock getNextBlock() {
+ long endTimeMs = _queryContext.getEndTimeMs();
+ try {
+ while (_numOperatorsFinished < _numOperators) {
+ QueryThreadContext.checkTermination(this::getExplainName);
+ BaseResultsBlock resultsBlock =
+ _blockingQueue.poll(endTimeMs - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
+ if (resultsBlock == null) {
+ throw QueryErrorCode.EXECUTION_TIMEOUT.asException("Timed out while streaming group-by results");
+ }
+ if (resultsBlock instanceof ExceptionResultsBlock) {
+ return checkTerminateExceptionAndAttachExecutionStats(resultsBlock);
+ }
+ if (resultsBlock == LAST_RESULTS_BLOCK) {
+ _numOperatorsFinished++;
+ continue;
+ }
+ mergeBlock((GroupByResultsBlock) resultsBlock);
+ if (_indexedTable != null && _indexedTable.size() >= _flushThreshold) {
+ return flushTable();
+ }
Review Comment:
The flush threshold is only checked after an entire per-segment GroupByResultsBlock has been fully merged (i.e., after `mergeBlock(...)` returns). If a single segment block contains far more distinct groups than `_flushThreshold`, `_indexedTable` can still grow well beyond the intended bound before the flush happens, defeating the memory-bounding goal for high-cardinality queries. Consider flushing incrementally during merge (e.g., when the table reaches the threshold while iterating keys) and buffering any additional flushed blocks to be returned on subsequent `nextBlock()` calls.
##########
pinot-common/src/main/java/org/apache/pinot/common/utils/config/QueryOptionsUtils.java:
##########
@@ -350,6 +350,12 @@ public static Integer getChunkSizeExtractFinalResult(Map<String, String> queryOp
return checkedParseInt(QueryOptionKey.CHUNK_SIZE_EXTRACT_FINAL_RESULT, chunkSizeExtractFinalResultString, 1);
}
+ @Nullable
+ public static Integer getStreamingGroupByFlushThreshold(Map<String, String> queryOptions) {
+ String value = queryOptions.get(QueryOptionKey.STREAMING_GROUP_BY_FLUSH_THRESHOLD);
+ return checkedParseIntPositive(QueryOptionKey.STREAMING_GROUP_BY_FLUSH_THRESHOLD, value);
Review Comment:
`streamingGroupByFlushThreshold` is documented (and described in the PR) as supporting `0` to disable streaming, but this parser uses `checkedParseIntPositive(...)` which rejects `0` and will throw an IllegalArgumentException for `SET streamingGroupByFlushThreshold = 0`. Use a non-negative parse (and return 0) so the option can be explicitly turned off without failing query planning.
```suggestion
return checkedParseInt(QueryOptionKey.STREAMING_GROUP_BY_FLUSH_THRESHOLD, value, 0);
```
--
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: commits-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
Re: [PR] Implement streaming group by leaf stage operator for multi-stage engine [pinot]
Posted by "xiangfu0 (via GitHub)" <gi...@apache.org>.
xiangfu0 commented on PR #18035:
URL: https://github.com/apache/pinot/pull/18035#issuecomment-4173595439
Documentation PR opened: https://github.com/pinot-contrib/pinot-docs/pull/608
This documents the new `streamingGroupByFlushThreshold` query option introduced in this PR.
--
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: commits-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org
Re: [PR] Implement streaming group by leaf stage operator for multi-stage engine [pinot]
Posted by "gortiz (via GitHub)" <gi...@apache.org>.
gortiz commented on PR #18035:
URL: https://github.com/apache/pinot/pull/18035#issuecomment-4171089740
Things to discuss:
- StreamingGroupByCombineOperator does not carry groupsTrimmed / numGroupsLimitReached / numGroupsWarningLimitReached into emitted blocks. I guess the streaming threashold is expected to be larger than numGroupsLimit, but AFAIS there is nothing enforncing that.
- We now use the threshold to pre-allocate stuff. I think that is fine, but the side effect is that a large threshold means we are going to use more memory than without streaming, right?
--
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: commits-unsubscribe@pinot.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org