xiangfu0 commented on code in PR #20300:
URL: https://github.com/apache/pinot/pull/20300#discussion_r4648985206
##########
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:
Fixed in 8e0735c2a3. I rebuilt the same row into dense and sparse segments
and read `9223372036854775809` from both. The parity tests also cover
fractional decimal inputs, FLOAT rounding, and signed zero.
##########
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:
Fixed in 8e0735c2a3. The colliding `a.b.c` case now uses the per-key filter
and matches `first`. Native scalar STRING keys such as `device.os` still use
their JSON index when the manifests rule out a collision. The filter tests
cover both decisions.
##########
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:
Fixed in 8e0735c2a3. The empty array now sets the null bit, reports one
logical value, and reads as the declared default. The regression also covers a
null element in a numeric, STRING, or BYTES array.
##########
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:
Fixed in 8e0735c2a3. Buffered numeric reads now write converted elements
into the caller’s array. The length and presence checks validate elements
without collecting converted values in an intermediate array.
--
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]