ankitsultana commented on code in PR #14701:
URL: https://github.com/apache/pinot/pull/14701#discussion_r1896073385


##########
pinot-core/src/main/java/org/apache/pinot/core/query/distinct/table/StringDistinctTable.java:
##########
@@ -0,0 +1,323 @@
+/**
+ * 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.distinct.table;
+
+import com.google.common.collect.Sets;
+import it.unimi.dsi.fastutil.objects.ObjectHeapPriorityQueue;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import javax.annotation.Nullable;
+import org.apache.commons.lang3.ArrayUtils;
+import org.apache.pinot.common.datatable.DataTable;
+import org.apache.pinot.common.request.context.OrderByExpressionContext;
+import org.apache.pinot.common.response.broker.ResultTable;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.common.datatable.DataTableBuilder;
+import org.apache.pinot.core.common.datatable.DataTableBuilderFactory;
+import org.apache.pinot.spi.trace.Tracing;
+import org.apache.pinot.spi.utils.CommonConstants;
+import org.roaringbitmap.RoaringBitmap;
+
+
+public class StringDistinctTable extends DistinctTable {
+  private final HashSet<String> _valueSet;
+  private final OrderByExpressionContext _orderByExpression;
+
+  private ObjectHeapPriorityQueue<String> _priorityQueue;
+
+  /**
+   * Constructor for distinct table without data table (on the server side).
+   */
+  public StringDistinctTable(DataSchema dataSchema, int limit, boolean 
nullHandlingEnabled,
+      @Nullable OrderByExpressionContext orderByExpression) {
+    super(dataSchema, limit, nullHandlingEnabled);
+
+    _valueSet = Sets.newHashSetWithExpectedSize(Math.min(limit, 
MAX_INITIAL_CAPACITY));
+    _orderByExpression = orderByExpression;
+  }
+
+  /**
+   * Constructor for distinct table with data table (on the broker side).
+   */
+  public StringDistinctTable(DataSchema dataSchema, int limit, boolean 
nullHandlingEnabled,
+      @Nullable OrderByExpressionContext orderByExpression, DataTable 
dataTable) {
+    super(dataSchema, limit, nullHandlingEnabled);
+
+    int numRows = dataTable.getNumberOfRows();
+    _valueSet = Sets.newHashSetWithExpectedSize(numRows);
+    _orderByExpression = orderByExpression;
+
+    RoaringBitmap nullRowIds = nullHandlingEnabled ? 
dataTable.getNullRowIds(0) : null;
+    if (nullRowIds == null) {
+      for (int i = 0; i < numRows; i++) {
+        _valueSet.add(dataTable.getString(i, 0));
+      }
+    } else {
+      assert nullRowIds.getCardinality() == 1;
+      addNull();
+      int nullRowId = nullRowIds.first();
+      if (nullRowId == 0) {
+        for (int i = 1; i < numRows; i++) {
+          _valueSet.add(dataTable.getString(i, 0));
+        }
+      } else {
+        // For backward compatibility where null value is not stored as the 
first row
+        for (int i = 0; i < nullRowId; i++) {
+          _valueSet.add(dataTable.getString(i, 0));
+        }
+        for (int i = nullRowId + 1; i < numRows; i++) {
+          _valueSet.add(dataTable.getString(i, 0));
+        }
+      }
+    }
+    assert _valueSet.size() <= limit;
+  }
+
+  @Override
+  public boolean hasOrderBy() {
+    return _orderByExpression != null;
+  }
+
+  public boolean addWithoutOrderBy(String value) {
+    assert _valueSet.size() < _limit;
+    _valueSet.add(value);
+    return _valueSet.size() >= _limitWithoutNull;
+  }
+
+  public void addWithOrderBy(String value) {
+    assert _valueSet.size() <= _limit;
+    if (_valueSet.size() < _limit) {
+      _valueSet.add(value);
+      return;
+    }
+    if (_valueSet.contains(value)) {
+      return;
+    }
+    if (_priorityQueue == null) {
+      Comparator<String> comparator =
+          _orderByExpression.isAsc() ? Comparator.reverseOrder() : 
Comparator.naturalOrder();
+      _priorityQueue = new ObjectHeapPriorityQueue<>(_valueSet, comparator);
+    }
+    String firstValue = _priorityQueue.first();
+    if (_priorityQueue.comparator().compare(value, firstValue) > 0) {
+      _valueSet.remove(firstValue);
+      _valueSet.add(value);
+      _priorityQueue.dequeue();
+      _priorityQueue.enqueue(value);
+    }
+  }
+
+  public void addUnbounded(String value) {
+    _valueSet.add(value);
+  }
+
+  @Override
+  public void mergeDistinctTable(DistinctTable distinctTable) {
+    StringDistinctTable bigDecimalDistinctTable = (StringDistinctTable) 
distinctTable;

Review Comment:
   `bigDecimalDistinctTable` => `stringDistinctTable`



-- 
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]

Reply via email to