npawar commented on a change in pull request #5934:
URL: https://github.com/apache/incubator-pinot/pull/5934#discussion_r486683505



##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/segment/processing/collector/RollupCollector.java
##########
@@ -0,0 +1,159 @@
+/**
+ * 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.segment.processing.collector;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.MetricFieldSpec;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.readers.GenericRow;
+
+
+/**
+ * A Collector that rolls up the incoming records on unique dimensions + time 
columns, based on provided aggregation types for metrics.
+ * By default will use the SUM aggregation on metrics.
+ */
+public class RollupCollector implements Collector {
+
+  private final Map<Record, GenericRow> _collection = new HashMap<>();
+  private Iterator<GenericRow> _iterator;
+  private GenericRowSorter _sorter;
+
+  private final int _keySize;
+  private final int _valueSize;
+  private final String[] _keyColumns;
+  private final String[] _valueColumns;
+  private final ValueAggregator[] _valueAggregators;
+  private final MetricFieldSpec[] _metricFieldSpecs;
+
+  public RollupCollector(CollectorConfig collectorConfig, Schema schema) {
+    _keySize = schema.getPhysicalColumnNames().size() - 
schema.getMetricNames().size();
+    _valueSize = schema.getMetricNames().size();
+    _keyColumns = new String[_keySize];
+    _valueColumns = new String[_valueSize];
+    _valueAggregators = new ValueAggregator[_valueSize];
+    _metricFieldSpecs = new MetricFieldSpec[_valueSize];
+
+    Map<String, ValueAggregatorFactory.ValueAggregatorType> aggregatorTypeMap 
= collectorConfig.getAggregatorTypeMap();
+    if (aggregatorTypeMap == null) {
+      aggregatorTypeMap = Collections.emptyMap();
+    }
+    int valIdx = 0;
+    int keyIdx = 0;
+    for (FieldSpec fieldSpec : schema.getAllFieldSpecs()) {
+      if (!fieldSpec.isVirtualColumn()) {
+        String name = fieldSpec.getName();
+        if (fieldSpec.getFieldType().equals(FieldSpec.FieldType.METRIC)) {
+          _metricFieldSpecs[valIdx] = (MetricFieldSpec) fieldSpec;
+          _valueColumns[valIdx] = name;
+          _valueAggregators[valIdx] = 
ValueAggregatorFactory.getValueAggregator(
+              aggregatorTypeMap.getOrDefault(name, 
ValueAggregatorFactory.ValueAggregatorType.SUM).toString());
+          valIdx++;
+        } else {
+          _keyColumns[keyIdx++] = name;
+        }
+      }
+    }
+
+    List<String> sortOrder = collectorConfig.getSortOrder();
+    if (sortOrder.size() > 0) {
+      _sorter = new GenericRowSorter(sortOrder, schema);
+    }
+  }
+
+  /**
+   * If a row already exists in the collection (based on dimension + time 
columns), rollup the metric values, else add the row
+   */
+  @Override
+  public void collect(GenericRow genericRow) {

Review comment:
       Yes, we keep the entire data of a partition on heap.
   Yes, we can certainly add off-heap or file-based in future. For now, we have 
a knob `maxRecordsPerSegment` that can help control number of records collected 
in memory.
   
   In order to sort the entire data on all dimensions+time, we'll still have to 
get all the data into memory right? And then we'll have to rewrite the files 
onto disk. The input is raw avro file. We do not have any dictionaries or 
docIds. 




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

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to