You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by "xiangfu0 (via GitHub)" <gi...@apache.org> on 2026/04/02 20:56:54 UTC

Re: [PR] Fix compacted column statistics to restrict all stats to valid documents [pinot]

xiangfu0 commented on code in PR #18041:
URL: https://github.com/apache/pinot/pull/18041#discussion_r3030339825


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/realtime/converter/stats/CompactedColumnStatistics.java:
##########
@@ -0,0 +1,378 @@
+/**
+ * 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.segment.local.realtime.converter.stats;
+
+import com.google.common.base.Utf8;
+import it.unimi.dsi.fastutil.ints.IntIterator;
+import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
+import java.math.BigDecimal;
+import java.util.Arrays;
+import javax.annotation.Nullable;
+import org.apache.pinot.segment.spi.datasource.DataSource;
+import org.apache.pinot.segment.spi.index.mutable.MutableForwardIndex;
+import org.apache.pinot.segment.spi.index.reader.Dictionary;
+import org.apache.pinot.spi.data.FieldSpec.DataType;
+import org.apache.pinot.spi.utils.BigDecimalUtils;
+import org.apache.pinot.spi.utils.ByteArray;
+import org.roaringbitmap.PeekableIntIterator;
+import org.roaringbitmap.RoaringBitmap;
+
+
+/**
+ * Column statistics for dictionary columns with dictionary-encoded forward indexes.
+ * Uses direct getDictId() calls for single-value columns and getDictIdMV() calls for multi-value columns
+ * to find which dictionary entries are used by valid documents.
+ *
+ * This is used when:
+ * - Column has a dictionary (dataSource.getDictionary() != null)
+ * - Forward index is dictionary-encoded (forwardIndex.isDictionaryEncoded() == true)
+ * - Commit-time compaction is enabled
+ */
+@SuppressWarnings("rawtypes")
+public class CompactedColumnStatistics extends MutableColumnStatistics {
+  @Nullable
+  private final Object _minValue;
+  @Nullable
+  private final Object _maxValue;
+  private final Object _uniqueValues;
+  private final int _cardinality;
+  private final int _minElementLength;
+  private final int _maxElementLength;
+  private final boolean _isSorted;
+  private final int _totalEntries;
+  private final int _maxMultiValues;
+  private final int _maxRowLength;
+
+  public CompactedColumnStatistics(DataSource dataSource, @Nullable int[] sortedDocIds, boolean isSortedColumn,
+      RoaringBitmap validDocIds) {
+    super(dataSource, sortedDocIds, isSortedColumn);
+
+    MutableForwardIndex forwardIndex = (MutableForwardIndex) dataSource.getForwardIndex();
+    Dictionary dictionary = _dictionary;
+    DataType valueType = dictionary.getValueType();
+    boolean isSingleValue = forwardIndex.isSingleValue();
+    boolean isVarBytesMV = !isSingleValue && (valueType == DataType.STRING || valueType == DataType.BYTES);
+
+    // Find which dictionary IDs are actually used by valid documents
+    IntOpenHashSet usedDictIds = new IntOpenHashSet();
+
+    // Single pass over valid documents to collect used dict IDs and entry counts.
+    // For non-sorted columns, sort order is also tracked inline: when sortedDocIds is provided,
+    // iterate in that order; when null, iterate via the bitmap. isSorted is initialized to false
+    // for sorted columns to skip the per-doc dictionary compare entirely.
+    boolean isSorted = !_isSortedColumn;
+    int prevDictId = -1;
+    int maxRowLength = -1;
+    int totalEntries = 0;
+    int maxMultiValues = -1;
+
+    if (_sortedDocIds != null) {
+      for (int docId : _sortedDocIds) {
+        if (!validDocIds.contains(docId)) {
+          continue;
+        }
+        if (isSingleValue) {
+          int dictId = forwardIndex.getDictId(docId);
+          totalEntries++;
+          usedDictIds.add(dictId);
+          if (isSorted) {
+            if (prevDictId != -1 && dictionary.compare(prevDictId, dictId) > 0) {
+              isSorted = false;
+            }
+            prevDictId = dictId;
+          }
+        } else {
+          int[] dictIds = forwardIndex.getDictIdMV(docId);

Review Comment:
   Maybe just wrap `_sortedDocIds` and `validDocIds` into a `ValidSortedDocIdsIterator` then simplify this code?
   



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