sgup432 commented on code in PR #16050:
URL: https://github.com/apache/lucene/pull/16050#discussion_r3243884085


##########
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) {
+          return doc = docToCheck;
+        }
+        currentBlockEnd = blockIterator.blockEnd();
+      }
+    }
+    return doc = NO_MORE_DOCS;
+  }
+
+  @Override
+  public long cost() {
+    return values.cost();
+  }
+
+  @Override
+  public int docIDRunEnd() throws IOException {
+    return blockIterator.docIDRunEnd();
+  }
+
+  @Override
+  public void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws 
IOException {
+    while (doc < upTo) {
+      // Advance block iterator if needed
+      if (blockIterator.docID() < doc) {
+        blockIterator.advance(doc);
+      }
+      if (blockIterator.docID() >= upTo || blockIterator.docID() == 
NO_MORE_DOCS) {
+        doc = blockIterator.docID() == NO_MORE_DOCS ? NO_MORE_DOCS : 
blockIterator.docID();
+        return;
+      }
+
+      int blockStart = Math.max(doc, blockIterator.docID());
+      SkipBlockRangeIterator.Match match = blockIterator.getMatch();
+
+      // For YES/YES_IF_PRESENT: docIDRunEnd() respects multi-level block 
promotion.
+      // For MAYBE: docIDRunEnd() returns doc+1 (conservative), but we need 
the actual
+      // block boundary to bulk-evaluate the whole block with 
rangeIntoBitSet().
+      int blockEnd =
+          match == SkipBlockRangeIterator.Match.MAYBE
+              ? Math.min(upTo, blockIterator.blockEnd())
+              : Math.min(upTo, blockIterator.docIDRunEnd());
+
+      switch (match) {
+        case YES:
+          // All docs in this range match — set all bits
+          bitSet.set(blockStart - offset, blockEnd - offset);
+          break;
+
+        case YES_IF_PRESENT:

Review Comment:
   We can expect YES_IF_PRESENT as well here as there is no density check where 
it guarantees that sparse fields can't be handled here. So I need to handle it 
in advance() accordingly and fix that.



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