Skip to content

Commit 858d9b4

Browse files
maosuhanjpountz
authored andcommitted
fix bug of incorrect cost after upgradeToBitSet in DocIdSetBuilder class (#11939)
1 parent 17dda1c commit 858d9b4

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

lucene/CHANGES.txt

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Bug Fixes
1111
This addresses a bug that was introduced in 9.2.0 where having many vectors is not handled well
1212
in the vector connections reader.
1313

14+
* GITHUB#11939: Fix incorrect cost calculation in DocIdSetBuilder after upgradeToBitSet when doc list is growing.
15+
This addresses a bug where the cost of TermRangeQuery/TermInSetQuery and some other queries will be highly underestimated.
16+
1417
Improvements
1518
---------------------
1619
* GITHUB#11912, GITHUB#11918: Port generic exception handling from MemorySegmentIndexInput

lucene/core/src/java/org/apache/lucene/util/DocIdSetBuilder.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,12 @@ public DocIdSetBuilder(int maxDoc, PointValues values, String field) throws IOEx
162162
* RoaringDocIdSet.Builder}.
163163
*/
164164
public void add(DocIdSetIterator iter) throws IOException {
165+
int cost = (int) Math.min(Integer.MAX_VALUE, iter.cost());
166+
BulkAdder adder = grow(cost);
165167
if (bitSet != null) {
166168
bitSet.or(iter);
167169
return;
168170
}
169-
int cost = (int) Math.min(Integer.MAX_VALUE, iter.cost());
170-
BulkAdder adder = grow(cost);
171171
for (int i = 0; i < cost; ++i) {
172172
int doc = iter.nextDoc();
173173
if (doc == DocIdSetIterator.NO_MORE_DOCS) {

lucene/core/src/test/org/apache/lucene/util/TestDocIdSetBuilder.java

+12
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,18 @@ public void testLeverageStats() throws IOException {
243243
assertTrue(builder.multivalued);
244244
}
245245

246+
public void testCostIsCorrectAfterBitsetUpgrade() throws IOException {
247+
final int maxDoc = 1000000;
248+
DocIdSetBuilder builder = new DocIdSetBuilder(maxDoc);
249+
// 1000000 >> 6 is greater than DocIdSetBuilder.threshold which is 1000000 >> 7
250+
for (int i = 0; i < 1000000 >> 6; ++i) {
251+
builder.add(DocIdSetIterator.range(i, i + 1));
252+
}
253+
DocIdSet result = builder.build();
254+
assertTrue(result instanceof BitDocIdSet);
255+
assertEquals(1000000 >> 6, result.iterator().cost());
256+
}
257+
246258
private static class DummyTerms extends Terms {
247259

248260
private final int docCount;

0 commit comments

Comments
 (0)