atris commented on a change in pull request #7141: URL: https://github.com/apache/incubator-pinot/pull/7141#discussion_r666684255
########## File path: pinot-core/src/main/java/org/apache/pinot/core/operator/query/DictionaryBasedDistinctOperator.java ########## @@ -0,0 +1,176 @@ +/** + * 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 it.unimi.dsi.fastutil.ints.IntSet; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +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.common.BlockValSet; +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.blocks.TransformBlock; +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; + +import static org.apache.pinot.core.query.distinct.DistinctExecutor.MAX_INITIAL_CAPACITY; + + +/** + * Operator which executes DISTINCT operation based on dictionary + */ +public class DictionaryBasedDistinctOperator extends DistinctOperator { + private static final String OPERATOR_NAME = "DictionaryBasedDistinctOperator"; + + private final DistinctAggregationFunction _distinctAggregationFunction; + private final Dictionary _dictionary; + private final int _numTotalDocs; + private final TransformOperator _transformOperator; + private IntSet _dictIdSet; + + private IntPriorityQueue _priorityQueue; + + private boolean _hasOrderBy; + + public DictionaryBasedDistinctOperator(IndexSegment indexSegment, DistinctAggregationFunction distinctAggregationFunction, + Dictionary dictionary, int numTotalDocs, + TransformOperator transformOperator) { + super(indexSegment, distinctAggregationFunction, transformOperator); + + _distinctAggregationFunction = distinctAggregationFunction; + _dictionary = dictionary; + _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(); + ExpressionContext expression = _distinctAggregationFunction.getInputExpressions().get(0); + + assert _distinctAggregationFunction.getType() == AggregationFunctionType.DISTINCT; + + TransformBlock transformBlock = _transformOperator.nextBlock(); Review comment: Yes, I was not aware that dictionary (when sorted) has IDs strictly in the ascending order of numbers (i.e. starting dictID will always be 0, next 1 ...). Thanks for highlighting that. Fixed per your suggestions. For the no order by case, we dont really have an order defined in which we read values from the underlying storage (so the order of values read from segment vs dictionary can be different). Hence, need to change DistinctQueriesTest to reflect the same. -- 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]
