sgup432 commented on code in PR #16050: URL: https://github.com/apache/lucene/pull/16050#discussion_r3244241097
########## lucene/core/src/java/org/apache/lucene/search/BatchDocValuesRangeIterator.java: ########## @@ -0,0 +1,171 @@ +/* + * 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.lucene.search; + +import java.io.IOException; +import org.apache.lucene.index.DocValuesSkipper; +import org.apache.lucene.index.NumericDocValues; +import org.apache.lucene.util.FixedBitSet; + +/** + * A {@link DocIdSetIterator} for numeric doc values range queries that batch-evaluates values for + * MAYBE blocks. Instead of checking one doc at a time through a {@link + * org.apache.lucene.search.TwoPhaseIterator}, this iterator reads values in a tight loop and sets + * bits directly in a {@link FixedBitSet}, enabling the {@link DenseConjunctionBulkScorer} to use + * the faster bitset intersection path. + * + * <p>This is used for dense single-valued numeric fields with a skip index. + */ +public final class BatchDocValuesRangeIterator extends DocIdSetIterator { + + private final SkipBlockRangeIterator blockIterator; + private final NumericDocValues values; + private final long minValue; + private final long maxValue; + private int doc = -1; + + public BatchDocValuesRangeIterator( + NumericDocValues values, DocValuesSkipper skipper, long minValue, long maxValue) { + this.blockIterator = new SkipBlockRangeIterator(skipper, minValue, maxValue); + this.values = values; + this.minValue = minValue; + this.maxValue = maxValue; + } + + @Override + public int docID() { + return doc; + } + + @Override + public int nextDoc() throws IOException { + return advance(doc + 1); + } + + @Override + public int advance(int target) throws IOException { + // Use the block iterator to skip NO blocks + int blockDoc = blockIterator.docID(); + if (blockDoc < target) { + blockDoc = blockIterator.advance(target); + } + if (blockDoc == NO_MORE_DOCS) { + return doc = NO_MORE_DOCS; + } + + // For YES blocks, all docs match — find the first doc with a value + if (blockIterator.getMatch() == SkipBlockRangeIterator.Match.YES) { + return doc = blockDoc; + } + + // For MAYBE blocks, scan forward to find a matching doc + int docToCheck = Math.max(target, blockDoc); + // Use the actual block boundary (not docIDRunEnd which returns doc+1 for MAYBE blocks) + int currentBlockEnd = blockIterator.blockEnd(); + while (docToCheck != NO_MORE_DOCS) { + if (values.advanceExact(docToCheck)) { + long v = values.longValue(); + if (v >= minValue && v <= maxValue) { + return doc = docToCheck; + } + } + docToCheck++; + // Check if we've left the current block + if (docToCheck >= currentBlockEnd) { + // Move to next matching block + blockDoc = blockIterator.advance(docToCheck); + if (blockDoc == NO_MORE_DOCS) { + return doc = NO_MORE_DOCS; + } + docToCheck = blockDoc; + if (blockIterator.getMatch() == SkipBlockRangeIterator.Match.YES) { Review Comment: I believe you meant handling `YES_IF_PRESENT` case here? I have added that logic now. Where in the next loop, we check if the next block is a `YES_IF_PRESENT`, in which case we return if it has a value. -- 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]
