raghavyadav01 commented on code in PR #19154: URL: https://github.com/apache/pinot/pull/19154#discussion_r3722219825
########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/openstruct/OpenStructSparseBlobReader.java: ########## @@ -0,0 +1,116 @@ +/** + * 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.segment.index.openstruct; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.MissingNode; +import java.util.LinkedHashMap; +import java.util.Map; +import javax.annotation.Nullable; +import org.apache.pinot.segment.spi.index.reader.ForwardIndexReader; +import org.apache.pinot.segment.spi.index.reader.ForwardIndexReaderContext; +import org.apache.pinot.segment.spi.index.reader.NullValueVectorReader; +import org.apache.pinot.spi.utils.JsonUtils; +import org.roaringbitmap.buffer.ImmutableRoaringBitmap; +import org.roaringbitmap.buffer.MutableRoaringBitmap; + + +/// Shared per-parent sparse blob parser with a ThreadLocal LRU cache so multi-key projections +/// parse each doc's blob once. Thread-safe: cache is ThreadLocal, forward reader is segment-shared. +public class OpenStructSparseBlobReader { + static final int PARSE_CACHE_SIZE = 10_000; + + private final ForwardIndexReader<ForwardIndexReaderContext> _blobReader; + @Nullable + private final NullValueVectorReader _blobNulls; + private final int _numDocs; + + private final ThreadLocal<LinkedHashMap<Integer, JsonNode>> _parseCache = Review Comment: [perf/mem] This `ThreadLocal` LRU is an instance field, so it stays reachable for the segment's whole loaded lifetime and is never cleared at query/block end. A pooled query thread accumulates one ≤ 10k-`JsonNode` cache per sparse column it ever scans, none released until the segment is offloaded/GC'd. On a server with many sparse OPEN_STRUCT segments that's a lot of retained heap. Could we scope the cache to the operator's `ForwardIndexReaderContext` (closed at scan end) so lifetime tracks the scan rather than the thread? ########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/openstruct/OpenStructSparseBlobReader.java: ########## @@ -0,0 +1,116 @@ +/** + * 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.segment.index.openstruct; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.MissingNode; +import java.util.LinkedHashMap; +import java.util.Map; +import javax.annotation.Nullable; +import org.apache.pinot.segment.spi.index.reader.ForwardIndexReader; +import org.apache.pinot.segment.spi.index.reader.ForwardIndexReaderContext; +import org.apache.pinot.segment.spi.index.reader.NullValueVectorReader; +import org.apache.pinot.spi.utils.JsonUtils; +import org.roaringbitmap.buffer.ImmutableRoaringBitmap; +import org.roaringbitmap.buffer.MutableRoaringBitmap; + + +/// Shared per-parent sparse blob parser with a ThreadLocal LRU cache so multi-key projections +/// parse each doc's blob once. Thread-safe: cache is ThreadLocal, forward reader is segment-shared. +public class OpenStructSparseBlobReader { + static final int PARSE_CACHE_SIZE = 10_000; + + private final ForwardIndexReader<ForwardIndexReaderContext> _blobReader; + @Nullable + private final NullValueVectorReader _blobNulls; + private final int _numDocs; + + private final ThreadLocal<LinkedHashMap<Integer, JsonNode>> _parseCache = + ThreadLocal.withInitial(() -> new LinkedHashMap<>(1024, 0.75f, true) { + @Override + protected boolean removeEldestEntry(Map.Entry<Integer, JsonNode> eldest) { + return size() > PARSE_CACHE_SIZE; + } + }); + + @SuppressWarnings("unchecked") + public OpenStructSparseBlobReader(ForwardIndexReader<?> blobReader, @Nullable NullValueVectorReader blobNulls, + int numDocs) { + _blobReader = (ForwardIndexReader<ForwardIndexReaderContext>) blobReader; + _blobNulls = blobNulls; + _numDocs = numDocs; + } + + public int getNumDocs() { + return _numDocs; + } + + @Nullable + public ForwardIndexReaderContext createBlobContext() { + return _blobReader.createContext(); + } + + @Nullable + public JsonNode getValue(int docId, String key, @Nullable ForwardIndexReaderContext context) { + JsonNode blob = getBlob(docId, context); + return blob == null ? null : blob.get(key); + } + + @Nullable + private JsonNode getBlob(int docId, @Nullable ForwardIndexReaderContext context) { + LinkedHashMap<Integer, JsonNode> cache = _parseCache.get(); + JsonNode cached = cache.get(docId); + if (cached != null) { + return cached.isMissingNode() ? null : cached; + } + JsonNode parsed = parseBlob(docId, context); + cache.put(docId, parsed == null ? MissingNode.getInstance() : parsed); + return parsed; + } + + @Nullable + private JsonNode parseBlob(int docId, @Nullable ForwardIndexReaderContext context) { + if (_blobNulls != null && _blobNulls.isNull(docId)) { + return null; + } + String json = _blobReader.getString(docId, context); + if (json.isEmpty()) { + return null; + } + try { + return JsonUtils.stringToJsonNode(json); + } catch (Exception e) { + throw new IllegalStateException("Failed to parse OPEN_STRUCT sparse blob at doc " + docId, e); + } + } + + /// Full scan for docs where `key` is present (explicit JSON null = absent). Callers memoize. + public ImmutableRoaringBitmap computePresence(String key) { Review Comment: [perf] `computePresence` full-scans and parses every doc for IS_NULL / null-handling queries on a sparse key. When the opt-in JSON index exists it already encodes per-key presence and could answer this far more cheaply than an O(numDocs) blob scan. Worth routing presence through the index when available? ########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/openstruct/SparseKeyDataSource.java: ########## @@ -0,0 +1,254 @@ +/** + * 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.segment.index.openstruct; + +import com.fasterxml.jackson.databind.JsonNode; +import java.io.IOException; +import java.math.BigDecimal; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; +import javax.annotation.Nullable; +import org.apache.pinot.segment.local.segment.index.datasource.BaseDataSource; +import org.apache.pinot.segment.spi.Constants; +import org.apache.pinot.segment.spi.datasource.DataSourceMetadata; +import org.apache.pinot.segment.spi.index.StandardIndexes; +import org.apache.pinot.segment.spi.index.column.ColumnIndexContainer; +import org.apache.pinot.segment.spi.index.reader.ForwardIndexReader; +import org.apache.pinot.segment.spi.index.reader.ForwardIndexReaderContext; +import org.apache.pinot.segment.spi.index.reader.NullValueVectorReader; +import org.apache.pinot.segment.spi.partition.PartitionFunction; +import org.apache.pinot.spi.data.FieldSpec; +import org.apache.pinot.spi.data.FieldSpec.DataType; +import org.roaringbitmap.buffer.ImmutableRoaringBitmap; +import org.roaringbitmap.buffer.MutableRoaringBitmap; + + +/// Virtual per-key DataSource for a sparse OPEN_STRUCT key. Parses the blob per doc via +/// the shared [OpenStructSparseBlobReader], coerces to the resolved stored type. No dictionary. +/// Null vector built lazily (one blob scan, memoized). +public class SparseKeyDataSource extends BaseDataSource { + private final FieldSpec _fieldSpec; + + public SparseKeyDataSource(FieldSpec resolvedChildSpec, OpenStructSparseBlobReader blobReader) { + super(new SparseKeyMetadata(resolvedChildSpec, blobReader.getNumDocs()), + new ColumnIndexContainer.FromMap(Map.of( + StandardIndexes.forward(), + new SparseKeyForwardIndexReader(resolvedChildSpec.getName(), + resolvedChildSpec.getDataType().getStoredType(), blobReader), + StandardIndexes.nullValueVector(), + new LazyPresenceNullVector(resolvedChildSpec.getName(), blobReader)))); + _fieldSpec = resolvedChildSpec; + } + + public FieldSpec getFieldSpec() { + return _fieldSpec; + } + + static class SparseKeyForwardIndexReader implements ForwardIndexReader<ForwardIndexReaderContext> { + private final String _key; + private final DataType _storedType; + private final OpenStructSparseBlobReader _blob; + + SparseKeyForwardIndexReader(String key, DataType storedType, OpenStructSparseBlobReader blob) { + _key = key; + _storedType = storedType; + _blob = blob; + } + + @Override + public boolean isDictionaryEncoded() { + return false; + } + + @Override + public boolean isSingleValue() { + return true; + } + + @Override + public DataType getStoredType() { + return _storedType; + } + + @Override + public ForwardIndexReaderContext createContext() { + return _blob.createBlobContext(); + } + + @Nullable + private JsonNode valueNode(int docId, ForwardIndexReaderContext context) { + JsonNode node = _blob.getValue(docId, _key, context); + return node == null || node.isNull() ? null : node; + } + + private <T> T orDefault(int docId, ForwardIndexReaderContext context, Function<JsonNode, T> map, T defaultValue) { + JsonNode node = valueNode(docId, context); + return node == null ? defaultValue : map.apply(node); + } + + @Override + public int getInt(int docId, ForwardIndexReaderContext context) { Review Comment: [correctness/parity] `asInt`/`asLong` coerce a non-numeric value to `0`, not the null default (`Integer.MIN_VALUE`). Does the dense build path coerce mismatched values the same way? If not, dense and sparse tiers return different answers for identical data. Worth an explicit parity-test case for the mismatch. ########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/openstruct/SparseKeyDataSource.java: ########## @@ -0,0 +1,254 @@ +/** + * 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.segment.index.openstruct; + +import com.fasterxml.jackson.databind.JsonNode; +import java.io.IOException; +import java.math.BigDecimal; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; +import javax.annotation.Nullable; +import org.apache.pinot.segment.local.segment.index.datasource.BaseDataSource; +import org.apache.pinot.segment.spi.Constants; +import org.apache.pinot.segment.spi.datasource.DataSourceMetadata; +import org.apache.pinot.segment.spi.index.StandardIndexes; +import org.apache.pinot.segment.spi.index.column.ColumnIndexContainer; +import org.apache.pinot.segment.spi.index.reader.ForwardIndexReader; +import org.apache.pinot.segment.spi.index.reader.ForwardIndexReaderContext; +import org.apache.pinot.segment.spi.index.reader.NullValueVectorReader; +import org.apache.pinot.segment.spi.partition.PartitionFunction; +import org.apache.pinot.spi.data.FieldSpec; +import org.apache.pinot.spi.data.FieldSpec.DataType; +import org.roaringbitmap.buffer.ImmutableRoaringBitmap; +import org.roaringbitmap.buffer.MutableRoaringBitmap; + + +/// Virtual per-key DataSource for a sparse OPEN_STRUCT key. Parses the blob per doc via +/// the shared [OpenStructSparseBlobReader], coerces to the resolved stored type. No dictionary. +/// Null vector built lazily (one blob scan, memoized). +public class SparseKeyDataSource extends BaseDataSource { + private final FieldSpec _fieldSpec; + + public SparseKeyDataSource(FieldSpec resolvedChildSpec, OpenStructSparseBlobReader blobReader) { + super(new SparseKeyMetadata(resolvedChildSpec, blobReader.getNumDocs()), + new ColumnIndexContainer.FromMap(Map.of( + StandardIndexes.forward(), + new SparseKeyForwardIndexReader(resolvedChildSpec.getName(), + resolvedChildSpec.getDataType().getStoredType(), blobReader), + StandardIndexes.nullValueVector(), + new LazyPresenceNullVector(resolvedChildSpec.getName(), blobReader)))); + _fieldSpec = resolvedChildSpec; + } + + public FieldSpec getFieldSpec() { + return _fieldSpec; + } + + static class SparseKeyForwardIndexReader implements ForwardIndexReader<ForwardIndexReaderContext> { + private final String _key; + private final DataType _storedType; + private final OpenStructSparseBlobReader _blob; + + SparseKeyForwardIndexReader(String key, DataType storedType, OpenStructSparseBlobReader blob) { + _key = key; + _storedType = storedType; + _blob = blob; + } + + @Override + public boolean isDictionaryEncoded() { + return false; + } + + @Override + public boolean isSingleValue() { + return true; + } + + @Override + public DataType getStoredType() { + return _storedType; + } + + @Override + public ForwardIndexReaderContext createContext() { + return _blob.createBlobContext(); + } + + @Nullable + private JsonNode valueNode(int docId, ForwardIndexReaderContext context) { + JsonNode node = _blob.getValue(docId, _key, context); + return node == null || node.isNull() ? null : node; + } + + private <T> T orDefault(int docId, ForwardIndexReaderContext context, Function<JsonNode, T> map, T defaultValue) { + JsonNode node = valueNode(docId, context); + return node == null ? defaultValue : map.apply(node); + } + + @Override + public int getInt(int docId, ForwardIndexReaderContext context) { + return orDefault(docId, context, JsonNode::asInt, FieldSpec.DEFAULT_DIMENSION_NULL_VALUE_OF_INT); + } + + @Override + public long getLong(int docId, ForwardIndexReaderContext context) { + return orDefault(docId, context, JsonNode::asLong, FieldSpec.DEFAULT_DIMENSION_NULL_VALUE_OF_LONG); + } + + @Override + public float getFloat(int docId, ForwardIndexReaderContext context) { + return orDefault(docId, context, node -> (float) node.asDouble(), + FieldSpec.DEFAULT_DIMENSION_NULL_VALUE_OF_FLOAT); + } + + @Override + public double getDouble(int docId, ForwardIndexReaderContext context) { + return orDefault(docId, context, JsonNode::asDouble, FieldSpec.DEFAULT_DIMENSION_NULL_VALUE_OF_DOUBLE); + } + + @Override + public BigDecimal getBigDecimal(int docId, ForwardIndexReaderContext context) { + return orDefault(docId, context, node -> new BigDecimal(node.asText()), + FieldSpec.DEFAULT_DIMENSION_NULL_VALUE_OF_BIG_DECIMAL); + } + + @Override + public String getString(int docId, ForwardIndexReaderContext context) { + return orDefault(docId, context, JsonNode::asText, FieldSpec.DEFAULT_DIMENSION_NULL_VALUE_OF_STRING); + } + + @Override + public byte[] getBytes(int docId, ForwardIndexReaderContext context) { Review Comment: [correctness] `getBytes` throws `IllegalStateException` on a non-binary node, which fails the whole query. Every other getter here falls back to the type default instead. Since the blob can hold arbitrary JSON this is data-triggerable — should we fall back to `DEFAULT_DIMENSION_NULL_VALUE_OF_BYTES` for consistency? ########## pinot-core/src/main/java/org/apache/pinot/core/operator/filter/MapFilterOperator.java: ########## @@ -218,6 +226,54 @@ private BaseFilterOperator buildPerKeyFilterOperator(DataSource keyDs, QueryCont } } + @Nullable + private BaseFilterOperator trySparseJsonIndex(OpenStructDataSource osDs, DataSource sparseKeyDs, + QueryContext queryContext, int numDocs) { + JsonIndexReader jsonIndex = osDs.getSparseJsonIndex(); + if (jsonIndex == null) { + return null; + } + if (sparseKeyDs.getDataSourceMetadata().getDataType().getStoredType() != FieldSpec.DataType.STRING) { Review Comment: [correctness] The fast path treats the JSON index as equivalent to the scan for STRING keys, but if a key's value is a JSON object/array the scan matches the serialized text via `asText()` while the index has no top-level `key=<obj>` posting — the two would disagree. Is a non-scalar value reachable for a sparse key here? If so, the refusal rules should also exclude it. -- 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]
