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


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/openstruct/SparseKeyDataSource.java:
##########
@@ -374,21 +413,88 @@ public void close() {
     }
   }
 
+  /// The dense tier's conversion for a key of `dataType`: [PinotDataType] of 
its stored type, as
+  /// `OpenStructColumnSplitter` applies at build. Null for STRING, JSON and 
BYTES, which read any value as is.
+  @Nullable
+  static PinotDataType converterFor(DataType dataType) {
+    switch (dataType.getStoredType()) {
+      case INT:
+      case LONG:
+      case FLOAT:
+      case DOUBLE:
+      case BIG_DECIMAL:
+        return 
ColumnDataType.fromDataTypeSV(dataType.getStoredType()).toPinotDataType();
+      default:
+        return null;
+    }
+  }
+
+  /// `node` converted by `converter`, or null when the conversion rejects it. 
With no converter the node itself is
+  /// returned. A container is offered as JSON text, as the dense tier sees a 
nested value.
+  @Nullable
+  static Object convert(@Nullable PinotDataType converter, JsonNode node) {
+    if (converter == null) {
+      return node;
+    }
+    Object raw = node.isNumber() ? node.numberValue() : node.isBoolean() ? 
(Object) node.booleanValue()

Review Comment:
   A normally built sparse BIG_DECIMAL key can lose precision here. For `new 
BigDecimal("9223372036854775809")`, the splitter writes the exact integer into 
the blob. Jackson reads it as `BigInteger`, which 
`PinotDataType.getSingleValueType` classifies as `OBJECT`. Its `toBigDecimal` 
goes through `doubleValue()`, so the sparse reader returns 
`9223372036854776000`. The dense reader and the previous sparse reader both 
return `9223372036854775809`. Could we preserve the exact integer when 
converting to BIG_DECIMAL and add this value to the parity test?



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/openstruct/SparseKeyDataSource.java:
##########
@@ -374,21 +413,88 @@ public void close() {
     }
   }
 
+  /// The dense tier's conversion for a key of `dataType`: [PinotDataType] of 
its stored type, as
+  /// `OpenStructColumnSplitter` applies at build. Null for STRING, JSON and 
BYTES, which read any value as is.
+  @Nullable
+  static PinotDataType converterFor(DataType dataType) {
+    switch (dataType.getStoredType()) {
+      case INT:
+      case LONG:
+      case FLOAT:
+      case DOUBLE:
+      case BIG_DECIMAL:
+        return 
ColumnDataType.fromDataTypeSV(dataType.getStoredType()).toPinotDataType();
+      default:
+        return null;
+    }
+  }
+
+  /// `node` converted by `converter`, or null when the conversion rejects it. 
With no converter the node itself is
+  /// returned. A container is offered as JSON text, as the dense tier sees a 
nested value.
+  @Nullable
+  static Object convert(@Nullable PinotDataType converter, JsonNode node) {
+    if (converter == null) {
+      return node;
+    }
+    Object raw = node.isNumber() ? node.numberValue() : node.isBoolean() ? 
(Object) node.booleanValue()
+        : node.isTextual() ? node.textValue() : node.toString();
+    try {
+      return converter.convert(raw, PinotDataType.getSingleValueType(raw));
+    } catch (RuntimeException e) {
+      return null;
+    }
+  }
+
+  /// The elements of a multi-value key's value: an array's elements, or the 
value itself, so a scalar stored on the
+  /// key before it became multi-value reads back as the one-element value the 
dense side stores for it.
+  static JsonNode[] elementsOf(JsonNode node) {
+    if (!node.isArray()) {
+      return new JsonNode[]{node};
+    }
+    JsonNode[] nodes = new JsonNode[node.size()];
+    for (int i = 0; i < nodes.length; i++) {
+      nodes[i] = node.get(i);
+    }
+    return nodes;
+  }
+
+  /// Whether the dense tier would keep this value for a key read through 
`converter`.
+  static boolean keeps(@Nullable PinotDataType converter, boolean singleValue, 
JsonNode node) {
+    if (converter == null) {
+      return true;
+    }
+    if (singleValue) {
+      return convert(converter, node) != null;
+    }
+    for (JsonNode element : elementsOf(node)) {

Review Comment:
   For a declared MV LONG key, `{"k":[]}` reaches the end of this loop and 
counts as present. `convertedValues()` also returns an empty array, so `IS NOT 
NULL` matches and `getNumValuesMV()` reports zero. 
`OpenStructColumnSplitter.addEntry()` drops an empty array before recording 
presence, so the dense key is null and its forward reader returns a singleton 
default. Could we handle an empty array as absent in both sparse paths and 
include `[]` in the multi-value parity case?



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/openstruct/OpenStructSparseBlobReader.java:
##########
@@ -59,7 +62,38 @@ public JsonNode getValue(int docId, String key, @Nullable 
ForwardIndexReaderCont
     // TODO: an N-key projection parses each doc N times. 
FastJsonPathExtractor fixes this in one pass,
     // but only across a batch of paths — this single-key signature has to 
change first.
     JsonNode blob = parseBlob(docId, context);
-    return blob == null ? null : blob.get(key);
+    return blob == null ? null : resolve(blob, key);
+  }
+
+  /// Resolves `key` against one document the way [OpenStructKeyFlattener] 
would have keyed
+  /// it at ingest, so `col['o.z']` reads `{"o": {"z": 2}}` from a blob that 
holds the nested object:
+  /// - a key the object carries literally wins at every level (`.` is an 
ordinary key character);
+  /// - otherwise the key is walked through nested objects, taking the first 
match in document order when two
+  ///   synthesized paths collide, as the flattener's first emission does;
+  /// - arrays are never indexed into, and a missing, null or scalar 
intermediate is absent;
+  /// - empty key segments are allowed, as the flattener emits them (`{"o": 
{"": 1}}` keys `o.` to `1`);
+  /// - unlike a flattened column, the walk has no depth limit. Native 
segments store a nested object that was not
+  ///   flattened as JSON text, which this does not parse, and a segment with 
a sparse-key manifest only serves the
+  ///   keys it lists, so the walk serves blobs written without flattening: a 
segment with no manifest, such as one
+  ///   produced outside the standard build. A miss costs a scan of each 
level's field names per document.
+  @Nullable
+  static JsonNode resolve(JsonNode node, String key) {
+    JsonNode literal = node.get(key);
+    if (literal != null || key.indexOf(OpenStructKeyFlattener.PATH_SEPARATOR) 
< 0 || !node.isObject()) {
+      return literal;
+    }
+    for (Map.Entry<String, JsonNode> field : node.properties()) {
+      String name = field.getKey();
+      JsonNode child = field.getValue();
+      if (child.isObject() && key.length() > name.length() && 
key.startsWith(name)
+          && key.charAt(name.length()) == 
OpenStructKeyFlattener.PATH_SEPARATOR) {
+        JsonNode found = resolve(child, key.substring(name.length() + 1));
+        if (found != null) {
+          return found;

Review Comment:
   The sparse JSON index can pick a different value for this dotted key. With 
`{"a.b":{"c":"first"},"a":{"b.c":"second"}}`, the new resolver projects `a.b.c` 
as `first`. The standard JSON index flattens both branches to `.a.b.c` and its 
second `put` overwrites the first. For a scalar STRING key, 
`MapFilterOperator.trySparseJsonIndex` uses those postings for equality, so 
`col['a.b.c'] = 'first'` misses a row that matches with the index disabled. 
Could we use the per-key filter for dotted lookups on unflattened blobs unless 
the index guarantees the same collision rule?



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/openstruct/SparseKeyDataSource.java:
##########
@@ -195,38 +222,50 @@ public byte[] getBytes(int docId, 
ForwardIndexReaderContext context) {
     @Nullable
     private JsonNode[] valueNodes(int docId, ForwardIndexReaderContext 
context) {
       JsonNode node = valueNode(docId, context);
-      if (node == null) {
+      return node == null ? null : elementsOf(node);
+    }
+
+    /// Elements of a multi-value key converted to its stored type, or null 
when the document lacks the key or the
+    /// dense tier's conversion rejects any element (it drops the whole value, 
since length is part of it).
+    @Nullable
+    private Object[] convertedValues(int docId, ForwardIndexReaderContext 
context) {
+      JsonNode[] nodes = valueNodes(docId, context);
+      if (nodes == null) {
         return null;
       }
-      if (!node.isArray()) {
-        return new JsonNode[]{node};
-      }
-      JsonNode[] nodes = new JsonNode[node.size()];
+      Object[] values = new Object[nodes.length];

Review Comment:
   Each numeric MV read now allocates this `Object[]` between the `JsonNode[]` 
and the primitive result. `MVScanDocIdIterator` supplies a reusable primitive 
buffer, yet the buffered getter builds all three arrays per row and copies the 
last one into that buffer. `getNumValuesMV()` also builds and discards the 
converted array just to return its length. Could the buffered path convert 
directly into the supplied array, returning one default on rejection, and the 
count path validate elements without collecting their values?



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/openstruct/OpenStructSparseBlobReader.java:
##########
@@ -59,7 +62,38 @@ public JsonNode getValue(int docId, String key, @Nullable 
ForwardIndexReaderCont
     // TODO: an N-key projection parses each doc N times. 
FastJsonPathExtractor fixes this in one pass,
     // but only across a batch of paths — this single-key signature has to 
change first.
     JsonNode blob = parseBlob(docId, context);
-    return blob == null ? null : blob.get(key);
+    return blob == null ? null : resolve(blob, key);

Review Comment:
   Returning a nested array here can exceed the buffer allocated by an MV 
filter. For a segment without sparse MV metadata, declare `o.ids` as MV LONG 
and read `{"o":{"ids":[1,2]}}`. `ImmutableOpenStructDataSource.maxNumValues()` 
reports 1, so `MVScanDocIdIterator.LongMatcher` allocates `long[1]`. A filter 
such as `col['o.ids'] = 2` then calls the buffered `getLongMV`, which copies 
two elements and throws. Could we derive the actual maximum or use a read that 
can grow its buffer when the maximum is unknown? A test through the parent data 
source would exercise this metadata path.



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