Skip to content

Updating Dense#intoBitSet to properly set upTo if it exceeds bitset size #14922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ Bug Fixes

* GITHUB#14838: Make it possible to extend Patience/Seeded knn queries (Tommaso Teofili)

* GITHUB#14922: Fix for IndexedDISI.Dense#intoBitSetWithinBlock to take into account the provided bitset size
and avoid throwing IndexOutOfBoundsException (Panagiotis Bailis)

Build
---------------------
* Upgrade forbiddenapis to version 3.9. (Uwe Schindler)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.BitSetIterator;
import org.apache.lucene.util.FixedBitSet;
import org.apache.lucene.util.MathUtil;
import org.apache.lucene.util.RoaringDocIdSet;

/**
Expand Down Expand Up @@ -473,7 +474,7 @@ public int advance(int target) throws IOException {

@Override
public void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException {
assert doc >= offset;
assert doc >= offset : "offset=" + offset + " doc=" + doc;
while (doc < upTo && method.intoBitSetWithinBlock(this, upTo, bitSet, offset) == false) {
readBlockHeader();
boolean found = method.advanceWithinBlock(this, block);
Expand Down Expand Up @@ -719,10 +720,10 @@ boolean intoBitSetWithinBlock(IndexedDISI disi, int upTo, FixedBitSet bitSet, in
if (disi.bitSet == null) {
disi.bitSet = new FixedBitSet(BLOCK_SIZE);
}

int sourceFrom = disi.doc & 0xFFFF;
int sourceTo = Math.min(upTo - disi.block, BLOCK_SIZE);
int destFrom = disi.doc - offset;
int destTo = MathUtil.unsignedMin(upTo, offset + bitSet.length());
int sourceFrom = disi.doc & 0xFFFF;
int sourceTo = Math.min(destTo - disi.block, BLOCK_SIZE);

long fp = disi.slice.getFilePointer();
disi.slice.seek(fp - Long.BYTES); // seek back a long to include current word (disi.word).
Expand All @@ -731,13 +732,24 @@ boolean intoBitSetWithinBlock(IndexedDISI disi, int upTo, FixedBitSet bitSet, in
FixedBitSet.orRange(disi.bitSet, sourceFrom, bitSet, destFrom, sourceTo - sourceFrom);

int blockEnd = disi.block | 0xFFFF;
if (upTo > blockEnd) {
if (destTo > blockEnd) {
disi.slice.seek(disi.blockEnd);
disi.index += disi.bitSet.cardinality(sourceFrom, sourceTo);
return false;
} else {
disi.slice.seek(fp);
return advanceWithinBlock(disi, upTo);
boolean found = advanceWithinBlock(disi, destTo);
if (found && disi.doc < upTo) {
throw new IllegalStateException(
"There are bits set in the source bitset that are not accounted for."
+ " doc="
+ disi.doc
+ " upTo="
+ upTo
+ " disi.block="
+ disi.block);
}
return found;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,36 @@ public void testDenseMultiBlock() throws IOException {
}
}

public void testDenseBitSizeLessThanBlockSize() throws IOException {
final byte denseRankPower = (byte) (random().nextInt(7) + 7);
try (Directory dir = newDirectory()) {
// initialize a maxDoc that is less than IndexedDISI.BLOCK_SIZE
int maxDoc = random().nextInt(4096 * 2, 65536);
FixedBitSet set = new FixedBitSet(maxDoc);
for (int i = 0; i < maxDoc; i += 2) { // Set every other to ensure dense
set.set(i);
}
int jumpTableEntryCount; // this should always be 0 given that maxDoc < BLOCK_SIZE
long length;
try (IndexOutput out = dir.createOutput("foo", IOContext.DEFAULT)) {
jumpTableEntryCount =
IndexedDISI.writeBitSet(
new BitSetIterator(set, set.cardinality()), out, denseRankPower);
length = out.getFilePointer();
assertTrue(
"jumpTableEntryCount should be 0 for dense bitsets with size < BLOCK_SIZE",
0 == jumpTableEntryCount);
}
try (IndexInput in = dir.openInput("foo", IOContext.DEFAULT)) {
IndexedDISI disi =
new IndexedDISI(in, 0L, length, jumpTableEntryCount, denseRankPower, set.cardinality());
FixedBitSet disiSet = new FixedBitSet(maxDoc);
// This would throw IOOB if bitset size is not handled correctly as per #14882
disiSet.or(disi);
}
}
}

public void testIllegalDenseRankPower() throws IOException {

// Legal values
Expand Down
Loading