siddharthteotia commented on a change in pull request #4535: Implement DISTINCT 
clause
URL: https://github.com/apache/incubator-pinot/pull/4535#discussion_r327248358
 
 

 ##########
 File path: 
pinot-core/src/main/java/org/apache/pinot/core/query/aggregation/DistinctTable.java
 ##########
 @@ -0,0 +1,228 @@
+/**
+ * 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.query.aggregation;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+import org.apache.pinot.common.data.FieldSpec;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.common.utils.DataTable;
+import org.apache.pinot.core.common.datatable.DataTableBuilder;
+import org.apache.pinot.core.common.datatable.DataTableFactory;
+import org.apache.pinot.core.data.table.Key;
+
+
+/**
+ * This serves the following purposes:
+ *
+ * (1) Intermediate result object for Distinct aggregation function
+ * (2) The same object is serialized by the server inside the data table
+ * for sending the results to broker. Broker deserializes it.
+ */
+public class DistinctTable {
+  private static final double LOAD_FACTOR = 0.75;
+  private static final int MAX_INITIAL_CAPACITY = 64 * 1024;
+  private FieldSpec.DataType[] _columnTypes;
+  private String[] _columnNames;
+  private Set<Key> _table;
+
+  /**
+   * Add a row to hash table
+   * @param key multi-column key to add
+   */
+  public void addKey(final Key key) {
+    _table.add(key);
+  }
+
+  public DistinctTable(int limit) {
+    // TODO: see if 64k is the right max initial capacity to use
+    // if it turns out that users always use LIMIT N > 0.75 * 64k and
+    // there are indeed that many records, then there will be resizes.
+    // The current method of setting the initial capacity as
+    // min(64k, limit/loadFactor) will not require resizes for LIMIT N
+    // where N <= 48000
+    int initialCapacity = Math.min(MAX_INITIAL_CAPACITY, 
Math.abs(nextPowerOfTwo((int) (limit / LOAD_FACTOR))));
+    _table = new HashSet<>(initialCapacity);
+  }
+
+  /**
+   * DESERIALIZE: Broker side
+   * @param byteBuffer data to deserialize
+   * @throws IOException
+   */
+  public DistinctTable(ByteBuffer byteBuffer)
+      throws IOException {
+    DataTable dataTable = DataTableFactory.getDataTable(byteBuffer);
+    DataSchema dataSchema = dataTable.getDataSchema();
+    int numRows = dataTable.getNumberOfRows();
+    int numColumns = dataSchema.size();
+
+    _table = new HashSet<>();
+
+    // extract rows from the datatable
+    for (int rowIndex = 0; rowIndex < numRows; rowIndex++) {
+      Object[] columnValues = new Object[numColumns];
+      for (int colIndex = 0; colIndex < numColumns; colIndex++) {
+        DataSchema.ColumnDataType columnDataType = 
dataSchema.getColumnDataType(colIndex);
+        switch (columnDataType) {
+          case INT:
+            columnValues[colIndex] = dataTable.getInt(rowIndex, colIndex);
+            break;
+          case LONG:
+            columnValues[colIndex] = dataTable.getLong(rowIndex, colIndex);
+            break;
+          case FLOAT:
+            columnValues[colIndex] = dataTable.getFloat(rowIndex, colIndex);
+            break;
+          case DOUBLE:
+            columnValues[colIndex] = dataTable.getDouble(rowIndex, colIndex);
+            break;
+          case STRING:
+            columnValues[colIndex] = dataTable.getString(rowIndex, colIndex);
+            break;
+          default:
 
 Review comment:
   I had initially planned to do it later. But I have added support now looking 
at what selection operator does

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to