iprithv commented on code in PR #16172: URL: https://github.com/apache/lucene/pull/16172#discussion_r3345014718
########## lucene/core/src/java/org/apache/lucene/search/BatchDocValuesOrdinalRangeIterator.java: ########## @@ -0,0 +1,207 @@ +/* + * 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.SortedDocValues; +import org.apache.lucene.index.SortedSetDocValues; +import org.apache.lucene.util.FixedBitSet; + +/** + * A {@link DocIdSetIterator} for sorted doc-values ordinal range queries that can bulk-evaluate + * skip blocks via {@link #intoBitSet(int, FixedBitSet, int)}. + * + * <p>This is used when a sorted or sorted-set doc-values field has a skip index. YES blocks are set + * directly, YES_IF_PRESENT blocks only check value presence, and MAYBE blocks check ordinals + * inline. + */ +public final class BatchDocValuesOrdinalRangeIterator extends DocIdSetIterator { + + private final SkipBlockRangeIterator blockIterator; + private final SortedDocValues sortedValues; + private final SortedSetDocValues sortedSetValues; + private final long minOrd; + private final long maxOrd; + private final long cost; + private int doc = -1; + + public BatchDocValuesOrdinalRangeIterator( + SortedDocValues values, DocValuesSkipper skipper, long minOrd, long maxOrd) { + this.blockIterator = new SkipBlockRangeIterator(skipper, minOrd, maxOrd); + this.sortedValues = values; + this.sortedSetValues = null; + this.minOrd = minOrd; + this.maxOrd = maxOrd; + this.cost = values.cost(); + } + + public BatchDocValuesOrdinalRangeIterator( + SortedSetDocValues values, DocValuesSkipper skipper, long minOrd, long maxOrd) { + this.blockIterator = new SkipBlockRangeIterator(skipper, minOrd, maxOrd); + this.sortedValues = null; + this.sortedSetValues = values; + this.minOrd = minOrd; + this.maxOrd = maxOrd; + this.cost = values.cost(); + } + + @Override + public int docID() { + return doc; + } + + @Override + public int nextDoc() throws IOException { + return advance(doc + 1); + } + + @Override + public int advance(int target) throws IOException { + int blockDoc = blockIterator.docID(); + if (blockDoc < target) { + blockDoc = blockIterator.advance(target); + } + if (blockDoc == NO_MORE_DOCS) { + return doc = NO_MORE_DOCS; + } + + if (blockIterator.getMatch() == SkipBlockRangeIterator.Match.YES) { + return doc = blockDoc; + } + + int docToCheck = Math.max(target, blockDoc); + int currentBlockEnd = blockIterator.blockEnd(); + while (docToCheck != NO_MORE_DOCS) { + if (matchesCurrentBlock(docToCheck)) { + return doc = docToCheck; + } + docToCheck++; + if (docToCheck >= currentBlockEnd) { + 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; + } + + private boolean matchesCurrentBlock(int target) throws IOException { + return switch (blockIterator.getMatch()) { + case YES -> true; + case YES_IF_PRESENT -> advanceExact(target); + case MAYBE -> ordinalInRange(target); + }; + } + + private boolean advanceExact(int target) throws IOException { + if (sortedValues != null) { + return sortedValues.advanceExact(target); + } + return sortedSetValues.advanceExact(target); + } + + private boolean ordinalInRange(int target) throws IOException { + if (sortedValues != null) { + return sortedValues.advanceExact(target) + && sortedValues.ordValue() >= minOrd + && sortedValues.ordValue() <= maxOrd; + } + if (sortedSetValues.advanceExact(target) == false) { + return false; + } + for (int i = 0; i < sortedSetValues.docValueCount(); i++) { + long ord = sortedSetValues.nextOrd(); + if (ord > maxOrd) { + return false; + } + if (ord >= minOrd) { + return true; + } + } + return false; + } + + @Override + public long cost() { + return cost; + } + + @Override + public int docIDRunEnd() throws IOException { + if (blockIterator.getMatch() == SkipBlockRangeIterator.Match.YES) { + return blockIterator.docIDRunEnd(); + } + return doc + 1; + } + + @Override + public void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException { + while (doc < upTo) { + if (blockIterator.docID() < doc) { + blockIterator.advance(doc); + } + if (blockIterator.docID() >= upTo || blockIterator.docID() == NO_MORE_DOCS) { Review Comment: I think intoBitSet can position on an unconfirmed doc. it could return a non-matching document, need to advance(upTo) here instead of blindly using the block start. ########## lucene/core/src/java/org/apache/lucene/search/BatchDocValuesOrdinalRangeIterator.java: ########## @@ -0,0 +1,207 @@ +/* + * 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.SortedDocValues; +import org.apache.lucene.index.SortedSetDocValues; +import org.apache.lucene.util.FixedBitSet; + +/** + * A {@link DocIdSetIterator} for sorted doc-values ordinal range queries that can bulk-evaluate + * skip blocks via {@link #intoBitSet(int, FixedBitSet, int)}. + * + * <p>This is used when a sorted or sorted-set doc-values field has a skip index. YES blocks are set + * directly, YES_IF_PRESENT blocks only check value presence, and MAYBE blocks check ordinals + * inline. + */ +public final class BatchDocValuesOrdinalRangeIterator extends DocIdSetIterator { + + private final SkipBlockRangeIterator blockIterator; + private final SortedDocValues sortedValues; + private final SortedSetDocValues sortedSetValues; + private final long minOrd; + private final long maxOrd; + private final long cost; + private int doc = -1; + + public BatchDocValuesOrdinalRangeIterator( + SortedDocValues values, DocValuesSkipper skipper, long minOrd, long maxOrd) { + this.blockIterator = new SkipBlockRangeIterator(skipper, minOrd, maxOrd); + this.sortedValues = values; + this.sortedSetValues = null; + this.minOrd = minOrd; + this.maxOrd = maxOrd; + this.cost = values.cost(); + } + + public BatchDocValuesOrdinalRangeIterator( + SortedSetDocValues values, DocValuesSkipper skipper, long minOrd, long maxOrd) { + this.blockIterator = new SkipBlockRangeIterator(skipper, minOrd, maxOrd); + this.sortedValues = null; + this.sortedSetValues = values; + this.minOrd = minOrd; + this.maxOrd = maxOrd; + this.cost = values.cost(); + } + + @Override + public int docID() { + return doc; + } + + @Override + public int nextDoc() throws IOException { + return advance(doc + 1); + } + + @Override + public int advance(int target) throws IOException { + int blockDoc = blockIterator.docID(); + if (blockDoc < target) { + blockDoc = blockIterator.advance(target); + } + if (blockDoc == NO_MORE_DOCS) { + return doc = NO_MORE_DOCS; + } + + if (blockIterator.getMatch() == SkipBlockRangeIterator.Match.YES) { + return doc = blockDoc; + } + + int docToCheck = Math.max(target, blockDoc); + int currentBlockEnd = blockIterator.blockEnd(); + while (docToCheck != NO_MORE_DOCS) { + if (matchesCurrentBlock(docToCheck)) { + return doc = docToCheck; + } + docToCheck++; + if (docToCheck >= currentBlockEnd) { + 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; + } + + private boolean matchesCurrentBlock(int target) throws IOException { + return switch (blockIterator.getMatch()) { + case YES -> true; + case YES_IF_PRESENT -> advanceExact(target); + case MAYBE -> ordinalInRange(target); + }; + } + + private boolean advanceExact(int target) throws IOException { + if (sortedValues != null) { + return sortedValues.advanceExact(target); + } + return sortedSetValues.advanceExact(target); + } + + private boolean ordinalInRange(int target) throws IOException { + if (sortedValues != null) { + return sortedValues.advanceExact(target) + && sortedValues.ordValue() >= minOrd + && sortedValues.ordValue() <= maxOrd; + } + if (sortedSetValues.advanceExact(target) == false) { + return false; + } + for (int i = 0; i < sortedSetValues.docValueCount(); i++) { + long ord = sortedSetValues.nextOrd(); + if (ord > maxOrd) { + return false; + } + if (ord >= minOrd) { + return true; + } + } + return false; + } + + @Override + public long cost() { + return cost; + } + + @Override + public int docIDRunEnd() throws IOException { + if (blockIterator.getMatch() == SkipBlockRangeIterator.Match.YES) { + return blockIterator.docIDRunEnd(); + } + return doc + 1; + } + + @Override + public void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException { Review Comment: current doc can be dropped here, current confirmed doc must be included in bitset without re-evaluation. I dont think re-checking is good idea, wouldn't values already be consumed? -- 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]
