Jackie-Jiang commented on a change in pull request #7141: URL: https://github.com/apache/incubator-pinot/pull/7141#discussion_r666601239
########## 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"; Review comment: The format is still incorrect ########## File path: pinot-core/src/main/java/org/apache/pinot/core/plan/DictionaryBasedDistinctPlanNode.java ########## @@ -0,0 +1,64 @@ +/** + * 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.plan; + +import org.apache.pinot.core.operator.query.DictionaryBasedDistinctOperator; +import org.apache.pinot.core.query.aggregation.function.AggregationFunction; +import org.apache.pinot.core.query.aggregation.function.DistinctAggregationFunction; +import org.apache.pinot.core.query.request.context.QueryContext; +import org.apache.pinot.segment.spi.IndexSegment; +import org.apache.pinot.segment.spi.index.reader.Dictionary; + +/** + * Execute a DISTINCT operation using dictionary based plan + */ +public class DictionaryBasedDistinctPlanNode implements PlanNode { + private final IndexSegment _indexSegment; + private final DistinctAggregationFunction _distinctAggregationFunction; + private final Dictionary _dictionary; + private final TransformPlanNode _transformPlanNode; + + /** + * Constructor for the class. + * + * @param indexSegment Segment to process + * @param queryContext Query context + */ + public DictionaryBasedDistinctPlanNode(IndexSegment indexSegment, QueryContext queryContext, Dictionary dictionary) { + _indexSegment = indexSegment; + AggregationFunction[] aggregationFunctions = queryContext.getAggregationFunctions(); + + assert aggregationFunctions != null && aggregationFunctions.length == 1 + && aggregationFunctions[0] instanceof DistinctAggregationFunction; + + _distinctAggregationFunction = (DistinctAggregationFunction) aggregationFunctions[0]; + + _dictionary = dictionary; + + _transformPlanNode = Review comment: We don't need to construct the transform plan node. Dictionary itself is enough to solve the query ########## File path: pinot-core/src/test/java/org/apache/pinot/queries/DistinctQueriesTest.java ########## @@ -245,12 +245,14 @@ public void testSingleColumnDistinctOnlyInnerSegment() // String columns //@formatter:off List<String> queries = Arrays - .asList("SELECT DISTINCT(stringColumn) FROM testTable", "SELECT DISTINCT(rawStringColumn) FROM testTable"); + .asList("SELECT DISTINCT(stringColumn) FROM testTable"); Review comment: Revert the change in this file ########## 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: We don't need to read dictIds from the transform block. - If there is no order-by, read the first `min(limit, dictionarySize)` values from the dictionary - If there is order-by - If dictionary is sorted, read the first if asc, last if desc `min(limit, dictionarySize)` values from the dictionary - If dictionary is not sorted, dump all values in the dictionary into the `DistinctTable` -- 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