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


##########
pinot-core/src/main/java/org/apache/pinot/core/data/table/SortedRecordTable.java:
##########
@@ -0,0 +1,340 @@
+/**
+ * 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.data.table;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.operator.blocks.results.GroupByResultsBlock;
+import org.apache.pinot.core.query.aggregation.function.AggregationFunction;
+import org.apache.pinot.core.query.request.context.QueryContext;
+import org.apache.pinot.core.query.scheduler.resources.ResourceManager;
+import org.apache.pinot.core.util.QueryMultiThreadingUtils;
+import org.apache.pinot.core.util.trace.TraceCallable;
+
+
+/**
+ * Util class used for merging of sorted group-by aggregation
+ */
+public class SortedRecordTable extends BaseTable {
+  private final ExecutorService _executorService;
+  protected final int _resultSize;
+  protected final int _numKeyColumns;
+  protected final AggregationFunction[] _aggregationFunctions;
+
+  protected Record[] _topRecords;
+
+  private Record[] _records;
+  private int _numMergedBlocks;
+  private final int _desiredNumMergedBlocks;
+  private int _nextIdx;
+  private final Comparator<Record> _comparator;
+  protected final int _numThreadsExtractFinalResult;
+  protected final int _chunkSizeExtractFinalResult;
+
+  public SortedRecordTable(DataSchema dataSchema, QueryContext queryContext, 
int resultSize,
+      ExecutorService executorService,
+      int numMergedBlocks, int desiredNumMergedBlocks, Comparator<Record> 
comparator) {
+    super(dataSchema);
+    _numMergedBlocks = numMergedBlocks;
+    _desiredNumMergedBlocks = desiredNumMergedBlocks;
+    _numKeyColumns = queryContext.getGroupByExpressions().size();
+    _aggregationFunctions = queryContext.getAggregationFunctions();
+    _executorService = executorService;
+    _comparator = comparator;
+    _resultSize = resultSize;
+    _numThreadsExtractFinalResult = 
Math.min(queryContext.getNumThreadsExtractFinalResult(),
+        Math.max(1, ResourceManager.DEFAULT_QUERY_RUNNER_THREADS));
+    _chunkSizeExtractFinalResult = 
queryContext.getChunkSizeExtractFinalResult();
+    _records = new Record[_resultSize];
+    _nextIdx = 0;
+  }
+
+  ///  only used when creating SortedRecordTable from unique, sorted segment 
groupby results

Review Comment:
   (minor, format) Keep one space after `///`, also we usually capitalize the 
javadoc. Same for other places



##########
pinot-core/src/main/java/org/apache/pinot/core/operator/query/GroupByOperator.java:
##########
@@ -166,6 +165,20 @@ protected GroupByResultsBlock getNextBlock() {
         
resultsBlock.setNumGroupsWarningLimitReached(numGroupsWarningLimitReached);
         return resultsBlock;
       }
+      if (GroupByUtils.shouldSortAggregateUnderSafeTrim(_queryContext)) {
+        // if sort-aggregate, sort the array even if it's smaller than trimSize
+        // to benefit combining. This is not very large overhead since the
+        // limit threshold of sort-aggregate is small
+        TableResizer tableResizer = new TableResizer(_dataSchema, 
_queryContext);
+        List<IntermediateRecord> intermediateRecords =
+            
tableResizer.sortInSegmentResults(groupByExecutor.getGroupKeyGenerator(),

Review Comment:
   Do you need to close group key generator here?



##########
pinot-core/src/main/java/org/apache/pinot/core/operator/query/GroupByOperator.java:
##########
@@ -166,6 +165,20 @@ protected GroupByResultsBlock getNextBlock() {
         
resultsBlock.setNumGroupsWarningLimitReached(numGroupsWarningLimitReached);
         return resultsBlock;
       }
+      if (GroupByUtils.shouldSortAggregateUnderSafeTrim(_queryContext)) {

Review Comment:
   (minor) Can be refactored to:
   ```
   GroupByResultsBlock resultsBlock;
   if (GroupByUtils.shouldSortAggregateUnderSafeTrim(_queryContext)) {
     ...
     resultsBlock = GroupByResultsBlock(_dataSchema, intermediateRecords, 
_queryContext);
   } else {
     resultsBlock = new GroupByResultsBlock(_dataSchema, 
groupByExecutor.getResult(), _queryContext);
   }
   resultsBlock.setNumGroupsLimitReached(numGroupsLimitReached);
   resultsBlock.setNumGroupsWarningLimitReached(numGroupsWarningLimitReached);
   return resultsBlock;
   ```



##########
pinot-core/src/main/java/org/apache/pinot/core/data/table/SortedRecordTable.java:
##########
@@ -0,0 +1,340 @@
+/**
+ * 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.data.table;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.operator.blocks.results.GroupByResultsBlock;
+import org.apache.pinot.core.query.aggregation.function.AggregationFunction;
+import org.apache.pinot.core.query.request.context.QueryContext;
+import org.apache.pinot.core.query.scheduler.resources.ResourceManager;
+import org.apache.pinot.core.util.QueryMultiThreadingUtils;
+import org.apache.pinot.core.util.trace.TraceCallable;
+
+
+/**
+ * Util class used for merging of sorted group-by aggregation
+ */
+public class SortedRecordTable extends BaseTable {
+  private final ExecutorService _executorService;
+  protected final int _resultSize;
+  protected final int _numKeyColumns;
+  protected final AggregationFunction[] _aggregationFunctions;
+
+  protected Record[] _topRecords;
+
+  private Record[] _records;
+  private int _numMergedBlocks;
+  private final int _desiredNumMergedBlocks;
+  private int _nextIdx;
+  private final Comparator<Record> _comparator;
+  protected final int _numThreadsExtractFinalResult;
+  protected final int _chunkSizeExtractFinalResult;
+
+  public SortedRecordTable(DataSchema dataSchema, QueryContext queryContext, 
int resultSize,
+      ExecutorService executorService,
+      int numMergedBlocks, int desiredNumMergedBlocks, Comparator<Record> 
comparator) {
+    super(dataSchema);
+    _numMergedBlocks = numMergedBlocks;
+    _desiredNumMergedBlocks = desiredNumMergedBlocks;
+    _numKeyColumns = queryContext.getGroupByExpressions().size();
+    _aggregationFunctions = queryContext.getAggregationFunctions();
+    _executorService = executorService;
+    _comparator = comparator;
+    _resultSize = resultSize;
+    _numThreadsExtractFinalResult = 
Math.min(queryContext.getNumThreadsExtractFinalResult(),
+        Math.max(1, ResourceManager.DEFAULT_QUERY_RUNNER_THREADS));
+    _chunkSizeExtractFinalResult = 
queryContext.getChunkSizeExtractFinalResult();
+    _records = new Record[_resultSize];
+    _nextIdx = 0;
+  }
+
+  ///  only used when creating SortedRecordTable from unique, sorted segment 
groupby results
+  @Override
+  public boolean upsert(Record record) {
+    if (_nextIdx == _resultSize) {
+      // enough records
+      return false;
+    }
+    _records[_nextIdx++] = record;
+    return true;
+  }
+
+  @Override
+  public boolean upsert(Key key, Record record) {
+    throw new UnsupportedOperationException("method unused for 
SortedRecordTable");
+  }
+
+  public boolean isSatisfied() {
+    return _numMergedBlocks == _desiredNumMergedBlocks;
+  }
+
+  // merge another SortedRecordTable into self

Review Comment:
   (minor, format) Use `///` for javadoc



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