Jackie-Jiang commented on a change in pull request #7141:
URL: https://github.com/apache/incubator-pinot/pull/7141#discussion_r666498797



##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/operator/query/DictionaryBasedDistinctOperator.java
##########
@@ -0,0 +1,163 @@
+/**
+ * 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.query;
+
+import it.unimi.dsi.fastutil.ints.IntHeapPriorityQueue;
+import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
+import it.unimi.dsi.fastutil.ints.IntPriorityQueue;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import org.apache.pinot.common.request.context.ExpressionContext;
+import org.apache.pinot.common.request.context.OrderByExpressionContext;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.data.table.Record;
+import org.apache.pinot.core.operator.ExecutionStatistics;
+import org.apache.pinot.core.operator.blocks.IntermediateResultsBlock;
+import org.apache.pinot.core.operator.transform.TransformOperator;
+import org.apache.pinot.core.query.aggregation.function.AggregationFunction;
+import 
org.apache.pinot.core.query.aggregation.function.DistinctAggregationFunction;
+import org.apache.pinot.core.query.distinct.DistinctTable;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+import org.apache.pinot.segment.spi.IndexSegment;
+import org.apache.pinot.segment.spi.index.reader.Dictionary;
+import org.apache.pinot.spi.data.FieldSpec;
+
+/**
+ * Operator which executes DISTINCT operation based on dictionary
+ */
+public class DictionaryBasedDistinctOperator extends DistinctOperator {
+    private static final String OPERATOR_NAME = 
"DictionaryBasedDistinctOperator";
+    int MAX_INITIAL_CAPACITY = 10000;
+
+    private final DistinctAggregationFunction _distinctAggregationFunction;
+    private final Map<String, Dictionary> _dictionaryMap;
+    private final int _numTotalDocs;
+    private final TransformOperator _transformOperator;
+    private final IntOpenHashSet _dictIdSet;
+
+    private IntPriorityQueue _priorityQueue;
+
+    private boolean _hasOrderBy;
+
+    public DictionaryBasedDistinctOperator(IndexSegment indexSegment, 
DistinctAggregationFunction distinctAggregationFunction,
+                                           Map<String, Dictionary> 
dictionaryMap, int numTotalDocs,
+                                           TransformOperator 
transformOperator) {
+        super(indexSegment, distinctAggregationFunction, transformOperator);
+
+        _distinctAggregationFunction = distinctAggregationFunction;
+        _dictionaryMap = dictionaryMap;
+        _numTotalDocs = numTotalDocs;
+        _transformOperator = transformOperator;
+        _dictIdSet = new IntOpenHashSet();
+
+        List<OrderByExpressionContext> orderByExpressionContexts = 
_distinctAggregationFunction.getOrderByExpressions();
+
+        if (orderByExpressionContexts != null) {
+            OrderByExpressionContext orderByExpressionContext = 
orderByExpressionContexts.get(0);
+            int limit = _distinctAggregationFunction.getLimit();
+            int comparisonFactor = orderByExpressionContext.isAsc() ? -1 : 1;
+            _priorityQueue =
+                    new IntHeapPriorityQueue(Math.min(limit, 
MAX_INITIAL_CAPACITY), (i1, i2) -> (i1 - i2) * comparisonFactor);
+            _hasOrderBy = true;
+        }
+    }
+
+    @Override
+    protected IntermediateResultsBlock getNextBlock() {
+        int limit = _distinctAggregationFunction.getLimit();
+        String column = 
_distinctAggregationFunction.getInputExpressions().get(0).getIdentifier();
+
+        assert _distinctAggregationFunction.getType() == 
AggregationFunctionType.DISTINCT;
+
+        Dictionary dictionary = _dictionaryMap.get(column);
+        int dictionarySize = dictionary.length();
+
+        for (int dictId = 0; dictId < dictionarySize; dictId++) {
+            if (!_hasOrderBy) {
+                _dictIdSet.add(dictId);
+
+                if (_dictIdSet.size() >= limit) {
+                    break;
+                }
+            } else {
+                // We already ascertained that the dictionary is sorted, hence 
we can use IDs instead of actual values

Review comment:
       I don't follow. dictId is simply the index within dictionary. E.g. for 
ascending order of limit 10, just read dictId 0-9 from the dictionary and 
construct the DistinctTable. No need to scan the whole dictionary




-- 
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-unsubscr...@pinot.apache.org

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