You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2021/07/08 18:44:18 UTC

[GitHub] [incubator-pinot] Jackie-Jiang commented on a change in pull request #7141: Support Dictionary Based Plan For DISTINCT

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



##########
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:
       Please follow the code style: https://docs.pinot.apache.org/developers/developers-and-contributors/code-setup

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

Review comment:
       Use `DistinctExecutor.MAX_INITIAL_CAPACITY`

##########
File path: pinot-core/src/test/java/org/apache/pinot/queries/DistinctQueriesTest.java
##########
@@ -245,19 +245,60 @@ 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 ORDER BY stringColumn");

Review comment:
       This test is already covered in `testSingleColumnDistinctOrderByInnerSegment()`. We don't need to add extra correctness test for the change, but you may want to add test to ensure the dictionary-based operator is used

##########
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) {

Review comment:
       Without ordering, pick the first `limit` dictIds and put them into the `DistinctTable`

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/plan/maker/InstancePlanMakerImplV2.java
##########
@@ -264,4 +267,31 @@ static boolean isFitForDictionaryBasedPlan(QueryContext queryContext, IndexSegme
     }
     return true;
   }
+
+  /**
+   * Returns dictionary based distinct plan node iff supported, else distinct plan node
+   */
+  private PlanNode getDistinctExecutionNode(IndexSegment indexSegment, QueryContext queryContext) {
+    AggregationFunction[] aggregationFunctions = queryContext.getAggregationFunctions();
+
+    // If we have gotten here, it must have been verified that there is only on aggregation function in the context
+    // and it is a DistinctAggregationFunction
+
+    DistinctAggregationFunction distinctAggregationFunction = (DistinctAggregationFunction) aggregationFunctions[0];
+    List<ExpressionContext> expressions = distinctAggregationFunction.getInputExpressions();
+
+    if (expressions.size() == 1 && queryContext.getFilter() == null) {
+      ExpressionContext expression = expressions.get(0);
+
+      if (expression.getType() == ExpressionContext.Type.IDENTIFIER) {
+        String column = expression.getIdentifier();
+        Dictionary dictionary = indexSegment.getDataSource(column).getDictionary();
+        if (dictionary != null && dictionary.isSorted()) {

Review comment:
       We should still be able to use dictionary based operator when dictionary is not sorted: create a `DistinctTable` and add all dictionary values into the table

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/plan/DictionaryBasedDistinctPlanNode.java
##########
@@ -0,0 +1,69 @@
+/**
+ * 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 java.util.HashMap;
+import java.util.Map;
+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 Map<String, Dictionary> _dictionaryMap;
+    private final TransformPlanNode _transformPlanNode;
+
+    /**
+     * Constructor for the class.
+     *
+     * @param indexSegment Segment to process
+     * @param queryContext Query context
+     */
+    public DictionaryBasedDistinctPlanNode(IndexSegment indexSegment, QueryContext queryContext) {
+        _indexSegment = indexSegment;
+        AggregationFunction[] aggregationFunctions = queryContext.getAggregationFunctions();
+
+        assert aggregationFunctions != null && aggregationFunctions.length == 1
+                && aggregationFunctions[0] instanceof DistinctAggregationFunction;
+
+        _distinctAggregationFunction = (DistinctAggregationFunction) aggregationFunctions[0];
+
+        _dictionaryMap = new HashMap<>();

Review comment:
       We don't really need a map. There should be only one dictionary. You can directly pass in the dictionary when constructing the plan node

##########
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:
       Since we already know the dictionary is sorted, simply pick the first (asc) or last (desc) `limit` dictIds and put them into the `DistinctTable`, no need to scan the dictionary.

##########
File path: pinot-core/src/main/java/org/apache/pinot/core/plan/maker/InstancePlanMakerImplV2.java
##########
@@ -264,4 +267,31 @@ static boolean isFitForDictionaryBasedPlan(QueryContext queryContext, IndexSegme
     }
     return true;
   }
+
+  /**
+   * Returns dictionary based distinct plan node iff supported, else distinct plan node
+   */
+  private PlanNode getDistinctExecutionNode(IndexSegment indexSegment, QueryContext queryContext) {

Review comment:
       (nit) `getDistinctPlanNode`




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

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



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