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


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

Review Comment:
   This replaces the existing public 11-argument constructor instead of 
overloading it. Any external mutable-index plugin compiled against the previous 
SPI will now hit `NoSuchMethodError` at runtime when it instantiates 
`MutableIndexContext`, so the old signature needs to remain and delegate to the 
new one for binary compatibility.
   



##########
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";
     return _forwardIndexReader.getDictId(docId, _forwardIndexReaderContext);
   }
 

Review Comment:
   `getDictId()` is now undefined for RAW forward indexes, but existing callers 
like the star-tree builder only check `hasDictionary()` before invoking it 
(`BaseSingleTreeBuilder` constructs `PinotSegmentColumnReader`s for dimensions 
and then calls `getDictId()` for every row). On a RAW-forward + 
standalone-dictionary column that path will now fail or read invalid data, so 
those callers need to be updated in the same change or this method needs to 
translate raw values to dict ids.
   



##########
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 an argument removes the existing public 
entry point from this SPI. Callers compiled against 
`MutableIndexContext.builder()` will also fail with `NoSuchMethodError`, so we 
need to keep the no-arg overload (for example as deprecated) or add the 
encoding through a separate setter to preserve compatibility.



##########
pinot-core/src/main/java/org/apache/pinot/core/plan/FilterPlanNode.java:
##########
@@ -316,17 +316,15 @@ private BaseFilterOperator 
constructPhysicalOperator(FilterContext filter, int n
                           dataSource.getIFSTIndex(), 
dataSource.getDictionary());
                 } else {
                   predicateEvaluator =
-                      
PredicateEvaluatorProvider.getPredicateEvaluator(predicate, 
dataSource.getDictionary(),
-                          dataSource.getDataSourceMetadata().getDataType(), 
_queryContext);
+                      
PredicateEvaluatorProvider.getPredicateEvaluator(predicate, dataSource, 
_queryContext);

Review Comment:
   This still bypasses `skipIndexes` for case-insensitive `REGEXP_LIKE`: 
whenever an IFST is present we unconditionally build an IFST-based evaluator 
before consulting `QueryContext.isIndexUseAllowed`. Queries that explicitly 
skip IFST will therefore keep using it instead of falling back to the raw-value 
path.



##########
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:
   Using `assert` here means the guard disappears in production, so a missing 
dictionary entry will still write `-1` into the dict-id buffer and silently 
corrupt downstream group-by/distinct state. This needs a real runtime 
check/exception instead of an assertion-only safeguard.
   



##########
pinot-core/src/main/java/org/apache/pinot/core/plan/FilterPlanNode.java:
##########
@@ -316,17 +316,15 @@ private BaseFilterOperator 
constructPhysicalOperator(FilterContext filter, int n
                           dataSource.getIFSTIndex(), 
dataSource.getDictionary());
                 } else {
                   predicateEvaluator =
-                      
PredicateEvaluatorProvider.getPredicateEvaluator(predicate, 
dataSource.getDictionary(),
-                          dataSource.getDataSourceMetadata().getDataType(), 
_queryContext);
+                      
PredicateEvaluatorProvider.getPredicateEvaluator(predicate, dataSource, 
_queryContext);
                 }
               } else {
                 if (dataSource.getFSTIndex() != null) {
                   predicateEvaluator = 
FSTBasedRegexpPredicateEvaluatorFactory.newFSTBasedEvaluator(regexpLikePredicate,
                       dataSource.getFSTIndex(), dataSource.getDictionary());
                 } else {
                   predicateEvaluator =
-                      
PredicateEvaluatorProvider.getPredicateEvaluator(predicate, 
dataSource.getDictionary(),
-                          dataSource.getDataSourceMetadata().getDataType(), 
_queryContext);
+                      
PredicateEvaluatorProvider.getPredicateEvaluator(predicate, dataSource, 
_queryContext);

Review Comment:
   This has the same `skipIndexes` problem for case-sensitive `REGEXP_LIKE`: if 
an FST exists we always create the FST-based evaluator, so 
`QueryContext.isIndexUseAllowed(..., FST)` is never honored and the query 
cannot force a scan/raw-evaluator fallback.



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