Copilot commented on code in PR #18407:
URL: https://github.com/apache/pinot/pull/18407#discussion_r3179077605


##########
pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/index/mutable/provider/MutableIndexContext.java:
##########
@@ -97,11 +114,15 @@ public File getConsumerDir() {
     return _consumerDir;
   }
 
-  public static Builder builder() {
-    return new Builder();
+  /// Constructs a builder for the given forward-index encoding. The encoding 
is required at construction time to
+  /// avoid implicit defaults that would silently misclassify 
shared-dictionary columns; callers should typically
+  /// pass `forwardIndexConfig.getEncodingType()` (which itself defaults to 
`FieldConfig.getEncodingType()`).
+  public static Builder builder(FieldConfig.EncodingType forwardIndexEncoding) 
{
+    return new Builder(forwardIndexEncoding);

Review Comment:
   Changing `builder()` to require the encoding in its factory method is a 
source-incompatible SPI break for downstream plugins that currently call 
`MutableIndexContext.builder()`. The PR description says the builder would gain 
a `withForwardIndexEncoding(...)` setter while preserving existing call sites, 
so this should keep the no-arg entry point and add the new field configurator 
instead of removing the old API shape.



##########
pinot-core/src/main/java/org/apache/pinot/core/common/DataFetcher.java:
##########
@@ -345,13 +354,84 @@ private ForwardIndexReaderContext getReaderContext() {
 
     void readDictIds(int[] docIds, int length, int[] dictIdBuffer) {
       Tracing.activeRecording().setInputDataType(_storedType, _singleValue);
-      _reader.readDictIds(docIds, length, dictIdBuffer, getReaderContext());
+      if (_useDictionary) {
+        _reader.readDictIds(docIds, length, dictIdBuffer, getReaderContext());
+      } else {
+        // RAW forward index with a standalone dictionary: read raw values 
from the forward index and translate them
+        // to dictionary IDs via Dictionary.indexOf.
+        readDictIdsFromRawValues(docIds, length, dictIdBuffer);
+      }
+    }
+
+    /// Translates the raw values stored in a RAW forward index to dictionary 
IDs via [Dictionary#indexOf], for
+    /// single-valued columns. Caller must guarantee the column has a non-null 
dictionary.
+    ///
+    /// The standalone dictionary on a "RAW forward + dict" column is expected 
to cover every value in the forward
+    /// index (it is built from the column's full value set during ingestion / 
reload). If a value is somehow not
+    /// found, [Dictionary#indexOf] returns [Dictionary#NULL_VALUE_INDEX] 
(`-1`); writing that to the dictId buffer
+    /// would silently corrupt downstream dict-id-keyed group-by / distinct 
buckets, so this method fails fast with
+    /// [IllegalStateException] in that case.
+    private void readDictIdsFromRawValues(int[] docIds, int length, int[] 
dictIdBuffer) {
+      ForwardIndexReaderContext readerContext = getReaderContext();
+      switch (_storedType) {
+        case INT:
+          for (int i = 0; i < length; i++) {
+            dictIdBuffer[i] = 
checkDictId(_dictionary.indexOf(_reader.getInt(docIds[i], readerContext)));
+          }
+          break;
+        case LONG:
+          for (int i = 0; i < length; i++) {
+            dictIdBuffer[i] = 
checkDictId(_dictionary.indexOf(_reader.getLong(docIds[i], readerContext)));
+          }
+          break;
+        case FLOAT:
+          for (int i = 0; i < length; i++) {
+            dictIdBuffer[i] = 
checkDictId(_dictionary.indexOf(_reader.getFloat(docIds[i], readerContext)));
+          }
+          break;
+        case DOUBLE:
+          for (int i = 0; i < length; i++) {
+            dictIdBuffer[i] = 
checkDictId(_dictionary.indexOf(_reader.getDouble(docIds[i], readerContext)));
+          }
+          break;
+        case BIG_DECIMAL:
+          for (int i = 0; i < length; i++) {
+            dictIdBuffer[i] = 
checkDictId(_dictionary.indexOf(_reader.getBigDecimal(docIds[i], 
readerContext)));
+          }
+          break;
+        case STRING:
+          for (int i = 0; i < length; i++) {
+            dictIdBuffer[i] = 
checkDictId(_dictionary.indexOf(_reader.getString(docIds[i], readerContext)));
+          }
+          break;
+        case BYTES:
+          for (int i = 0; i < length; i++) {
+            dictIdBuffer[i] =
+                checkDictId(_dictionary.indexOf(new 
ByteArray(_reader.getBytes(docIds[i], readerContext))));
+          }
+          break;
+        default:
+          throw new IllegalStateException(
+              "Unsupported stored type for raw-to-dictionary translation: " + 
_storedType);
+      }
+    }
+
+    /// Asserts the dictionary lookup found the value (i.e. the standalone 
dictionary is complete). Uses `assert`
+    /// rather than `Preconditions.checkState` because this is a per-row 
hot-path guard; the standalone dictionary
+    /// is built from the column's full value set during ingestion / reload 
and is expected to cover every raw
+    /// forward-index value, so the check exists to surface invariant 
violations during testing without paying the
+    /// cost on production query paths. Static so JIT can inline cleanly at 
megamorphic call sites.
+    private static int checkDictId(int dictId) {
+      assert dictId != Dictionary.NULL_VALUE_INDEX
+          : "Dictionary lookup miss while translating RAW forward-index values 
to dictionary IDs; "
+              + "the standalone dictionary must cover every value in the 
forward index";

Review Comment:
   `checkDictId` now relies on `assert`, which is disabled in normal production 
JVMs. If a standalone dictionary ever misses a raw forward value, this code 
will silently write `-1` into the dict-id buffer instead of failing fast, 
reintroducing the downstream group-by/distinct corruption that this change is 
trying to prevent.
   



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/readers/PinotSegmentColumnReader.java:
##########
@@ -82,22 +88,38 @@ public DataType getValueType() {
     return _valueType;
   }
 
+  /// Returns `true` when the column has a dictionary attached. Note this does 
NOT imply the forward index is
+  /// dictionary-encoded — a column may carry a standalone dictionary purely 
to support a secondary index while the
+  /// forward index stores raw values. Use [#isDictionaryEncoded()] to check 
the forward-index encoding.
   public boolean hasDictionary() {
     return _dictionary != null;
   }
 
+  /// Returns `true` when the forward index stores dictionary IDs (so 
[#getDictId(int)] is defined). For columns
+  /// with a RAW forward index — even those that carry a standalone dictionary 
— this returns `false` and
+  /// [#getDictId(int)] must not be called.
+  public boolean isDictionaryEncoded() {
+    return _dictEncoded;
+  }
+
   @Nullable
   public Dictionary getDictionary() {
     return _dictionary;
   }
 
+  /// Returns the dictionary ID for the given doc, read directly from the 
forward index. Only valid when the forward
+  /// index is dictionary-encoded (see [#isDictionaryEncoded()]); calling this 
on a column with a RAW forward index
+  /// returns undefined data. Uses `assert` rather than 
`Preconditions.checkState` because this is a per-row hot
+  /// path; callers are expected to gate on [#isDictionaryEncoded()] before 
invoking.
   public int getDictId(int docId) {
+    assert _dictEncoded
+        : "getDictId is only valid for dictionary-encoded forward indexes; 
this column has a RAW forward index";

Review Comment:
   The new guard is also only an `assert`, so production builds will still 
execute `ForwardIndexReader.getDictId(...)` on RAW-forward columns if a caller 
forgets to check `isDictionaryEncoded()`. There are existing callers that only 
gate on `hasDictionary()` before calling `getDictId()` (for example star-tree 
dimension readers), so RAW-forward + standalone-dictionary columns can still 
yield undefined dict IDs outside assertion-enabled tests.
   



##########
pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/index/mutable/provider/MutableIndexContext.java:
##########
@@ -37,12 +39,14 @@ public class MutableIndexContext {
   private final PinotDataBufferMemoryManager _memoryManager;
   private final File _consumerDir;
 
-  public MutableIndexContext(FieldSpec fieldSpec, int fixedLengthBytes, 
boolean hasDictionary, String segmentName,
-      PinotDataBufferMemoryManager memoryManager, int capacity, boolean 
offHeap, int estimatedColSize,
-      int estimatedCardinality, int avgNumMultiValues, File consumerDir) {
+  public MutableIndexContext(FieldSpec fieldSpec, int fixedLengthBytes, 
boolean hasDictionary,
+      FieldConfig.EncodingType forwardIndexEncoding, String segmentName, 
PinotDataBufferMemoryManager memoryManager,
+      int capacity, boolean offHeap, int estimatedColSize, int 
estimatedCardinality, int avgNumMultiValues,
+      File consumerDir) {

Review Comment:
   This replaces the previous 11-argument public constructor instead of keeping 
it as a deprecated delegate. Because `MutableIndexContext` lives in the SPI 
module, any external `IndexType`/mutable-index plugin compiled against the old 
signature will now fail with `NoSuchMethodError` as soon as it constructs the 
context.



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