Skip to content

Apache Lucene open-source search software

ScoreSupplier#cost throws an IOException (#16519)

Ignacio Verabc601411a316
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 98d021b6994..48fceaff75a 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -109,6 +109,8 @@ API Changes
* GITHUB#16363: Add API to use a custom `DictionarySuggester` instead of hardcoded `GeneratingSuggester`
+* GITHUB#16518: Changes the signature of ScorerSupplier#cost to throw an IOException. (Ignacio Vera)
+
New Features
---------------------
* GITHUB#16418: Incremental doc-values updates. A set-only doc-values update (NUMERIC or BINARY) is written as a sparse
diff --git a/lucene/core/src/java/org/apache/lucene/document/SortedSkipperScorerSupplier.java b/lucene/core/src/java/org/apache/lucene/document/SortedSkipperScorerSupplier.java
index 5bd00061fb3..5bc19f5da4d 100644
--- a/lucene/core/src/java/org/apache/lucene/document/SortedSkipperScorerSupplier.java
+++ b/lucene/core/src/java/org/apache/lucene/document/SortedSkipperScorerSupplier.java
@@ -17,7 +17,6 @@
package org.apache.lucene.document;
import java.io.IOException;
-import java.io.UncheckedIOException;
import java.util.function.LongPredicate;
import org.apache.lucene.index.DocValuesSkipper;
import org.apache.lucene.search.BulkScorer;
@@ -101,15 +100,9 @@ abstract class SortedSkipperScorerSupplier extends ScorerSupplier {
}
@Override
- public long cost() {
+ public long cost() throws IOException {
if (skipperMinDocId == -1) {
- try {
- // Similar to PointValues, IOExceptions needs to be caught and rethrown as
- // UncheckedIOException
- computeSkipperDocIds();
- } catch (IOException e) {
- throw new UncheckedIOException(e);
- }
+ computeSkipperDocIds();
}
if (skipperMaxDocIdExact) {
return skipperMaxDocId - skipperMinDocId;
diff --git a/lucene/core/src/java/org/apache/lucene/document/XYPointInGeometryQuery.java b/lucene/core/src/java/org/apache/lucene/document/XYPointInGeometryQuery.java
index d8d9b8916af..0c2da1d10e2 100644
--- a/lucene/core/src/java/org/apache/lucene/document/XYPointInGeometryQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/document/XYPointInGeometryQuery.java
@@ -161,7 +161,7 @@ final class XYPointInGeometryQuery extends Query {
}
@Override
- public long cost() {
+ public long cost() throws IOException {
if (cost == -1) {
// Computing the cost may be expensive, so only do it if necessary
cost = values.estimateDocCount(visitor);
diff --git a/lucene/core/src/java/org/apache/lucene/search/BayesianScoreQuery.java b/lucene/core/src/java/org/apache/lucene/search/BayesianScoreQuery.java
index 56593a9918e..e11855f4b49 100644
--- a/lucene/core/src/java/org/apache/lucene/search/BayesianScoreQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/BayesianScoreQuery.java
@@ -231,7 +231,7 @@ public final class BayesianScoreQuery extends Query {
}
@Override
- public long cost() {
+ public long cost() throws IOException {
return innerSupplier.cost();
}
diff --git a/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.java b/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.java
index ccc9b91bd45..dbac4e61050 100644
--- a/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.java
+++ b/lucene/core/src/java/org/apache/lucene/search/BooleanScorerSupplier.java
@@ -25,7 +25,6 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.OptionalLong;
-import java.util.stream.Stream;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.Weight.DefaultBulkScorer;
import org.apache.lucene.util.Bits;
@@ -69,19 +68,31 @@ final class BooleanScorerSupplier extends ScorerSupplier {
this.maxDoc = maxDoc;
}
- private long computeShouldCost() {
+ private long computeShouldCost() throws IOException {
final Collection<ScorerSupplier> optionalScorers = subs.get(Occur.SHOULD);
+ long[] costs = new long[optionalScorers.size()];
+ int i = 0;
+ for (ScorerSupplier ss : optionalScorers) {
+ costs[i++] = ss.cost();
+ }
return ScorerUtil.costWithMinShouldMatch(
- optionalScorers.stream().mapToLong(ScorerSupplier::cost),
- optionalScorers.size(),
- minShouldMatch);
+ Arrays.stream(costs), optionalScorers.size(), minShouldMatch);
}
- private long computeCost() {
- OptionalLong minRequiredCost =
- Stream.concat(subs.get(Occur.MUST).stream(), subs.get(Occur.FILTER).stream())
- .mapToLong(ScorerSupplier::cost)
- .min();
+ private long computeCost() throws IOException {
+ OptionalLong minRequiredCost = OptionalLong.empty();
+ for (ScorerSupplier ss : subs.get(Occur.MUST)) {
+ long c = ss.cost();
+ if (minRequiredCost.isEmpty() || c < minRequiredCost.getAsLong()) {
+ minRequiredCost = OptionalLong.of(c);
+ }
+ }
+ for (ScorerSupplier ss : subs.get(Occur.FILTER)) {
+ long c = ss.cost();
+ if (minRequiredCost.isEmpty() || c < minRequiredCost.getAsLong()) {
+ minRequiredCost = OptionalLong.of(c);
+ }
+ }
if (minRequiredCost.isPresent() && minShouldMatch == 0) {
return minRequiredCost.getAsLong();
} else {
@@ -105,7 +116,7 @@ final class BooleanScorerSupplier extends ScorerSupplier {
}
@Override
- public long cost() {
+ public long cost() throws IOException {
if (cost == -1) {
cost = computeCost();
}
@@ -383,13 +394,14 @@ final class BooleanScorerSupplier extends ScorerSupplier {
return scorer;
}
- long mustLeadCost =
- subs.get(Occur.MUST).stream().mapToLong(ScorerSupplier::cost).min().orElse(Long.MAX_VALUE);
- long filterLeadCost =
- subs.get(Occur.FILTER).stream()
- .mapToLong(ScorerSupplier::cost)
- .min()
- .orElse(Long.MAX_VALUE);
+ long mustLeadCost = Long.MAX_VALUE;
+ for (ScorerSupplier ss : subs.get(Occur.MUST)) {
+ mustLeadCost = Math.min(mustLeadCost, ss.cost());
+ }
+ long filterLeadCost = Long.MAX_VALUE;
+ for (ScorerSupplier ss : subs.get(Occur.FILTER)) {
+ filterLeadCost = Math.min(filterLeadCost, ss.cost());
+ }
long leadCost = Math.min(mustLeadCost, filterLeadCost);
List<Scorer> requiredNoScoring = new ArrayList<>();
diff --git a/lucene/core/src/java/org/apache/lucene/search/ConstantScoreQuery.java b/lucene/core/src/java/org/apache/lucene/search/ConstantScoreQuery.java
index 7767e40b5ae..eab94000f08 100644
--- a/lucene/core/src/java/org/apache/lucene/search/ConstantScoreQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/ConstantScoreQuery.java
@@ -178,7 +178,7 @@ public final class ConstantScoreQuery extends Query {
}
@Override
- public long cost() {
+ public long cost() throws IOException {
return innerScorerSupplier.cost();
}
};
diff --git a/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java b/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java
index c26903fb33c..05bb4e79b55 100644
--- a/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/DisjunctionMaxQuery.java
@@ -171,7 +171,7 @@ public final class DisjunctionMaxQuery extends Query implements Iterable<Query>
}
@Override
- public long cost() {
+ public long cost() throws IOException {
if (cost == -1) {
long cost = 0;
for (ScorerSupplier ss : scorerSuppliers) {
diff --git a/lucene/core/src/java/org/apache/lucene/search/IndexOrDocValuesQuery.java b/lucene/core/src/java/org/apache/lucene/search/IndexOrDocValuesQuery.java
index 57b9af2289a..e4e975bb567 100644
--- a/lucene/core/src/java/org/apache/lucene/search/IndexOrDocValuesQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/IndexOrDocValuesQuery.java
@@ -191,7 +191,7 @@ public final class IndexOrDocValuesQuery extends Query {
}
@Override
- public long cost() {
+ public long cost() throws IOException {
return indexScorerSupplier.cost();
}
};
diff --git a/lucene/core/src/java/org/apache/lucene/search/LogOddsFusionQuery.java b/lucene/core/src/java/org/apache/lucene/search/LogOddsFusionQuery.java
index f311ecf9055..fba74567237 100644
--- a/lucene/core/src/java/org/apache/lucene/search/LogOddsFusionQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/LogOddsFusionQuery.java
@@ -259,7 +259,7 @@ public final class LogOddsFusionQuery extends Query implements Iterable<Query> {
}
@Override
- public long cost() {
+ public long cost() throws IOException {
if (cost == -1) {
long cost = 0;
for (ScorerSupplier ss : scorerSuppliers) {
diff --git a/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java b/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java
index 90bca4e8def..6efe5a9f5d0 100644
--- a/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java
@@ -17,7 +17,6 @@
package org.apache.lucene.search;
import java.io.IOException;
-import java.io.UncheckedIOException;
import java.util.AbstractCollection;
import java.util.Arrays;
import java.util.Collection;
@@ -216,20 +215,16 @@ public abstract class PointInSetQuery extends Query implements Accountable {
}
@Override
- public long cost() {
- try {
- if (cost == -1) {
- // Computing the cost may be expensive, so only do it if necessary
- DocIdSetBuilder result = new DocIdSetBuilder(reader.maxDoc(), values);
- cost =
- values.estimateDocCount(
- new MergePointVisitor(sortedPackedPoints.iterator(), result));
- assert cost >= 0;
- }
- return cost;
- } catch (IOException e) {
- throw new UncheckedIOException(e);
+ public long cost() throws IOException {
+ if (cost == -1) {
+ // Computing the cost may be expensive, so only do it if necessary
+ DocIdSetBuilder result = new DocIdSetBuilder(reader.maxDoc(), values);
+ cost =
+ values.estimateDocCount(
+ new MergePointVisitor(sortedPackedPoints.iterator(), result));
+ assert cost >= 0;
}
+ return cost;
}
};
} else {
@@ -255,23 +250,19 @@ public abstract class PointInSetQuery extends Query implements Accountable {
}
@Override
- public long cost() {
- try {
- if (cost == -1) {
- DocIdSetBuilder result = new DocIdSetBuilder(reader.maxDoc(), values);
- SinglePointVisitor visitor = new SinglePointVisitor(result);
- TermIterator iterator = sortedPackedPoints.iterator();
- cost = 0;
- for (BytesRef point = iterator.next(); point != null; point = iterator.next()) {
- visitor.setPoint(point);
- cost += values.estimateDocCount(visitor);
- }
- assert cost >= 0;
+ public long cost() throws IOException {
+ if (cost == -1) {
+ DocIdSetBuilder result = new DocIdSetBuilder(reader.maxDoc(), values);
+ SinglePointVisitor visitor = new SinglePointVisitor(result);
+ TermIterator iterator = sortedPackedPoints.iterator();
+ cost = 0;
+ for (BytesRef point = iterator.next(); point != null; point = iterator.next()) {
+ visitor.setPoint(point);
+ cost += values.estimateDocCount(visitor);
}
- return cost;
- } catch (IOException e) {
- throw new UncheckedIOException(e);
+ assert cost >= 0;
}
+ return cost;
}
};
}
diff --git a/lucene/core/src/java/org/apache/lucene/search/ScorerSupplier.java b/lucene/core/src/java/org/apache/lucene/search/ScorerSupplier.java
index a1532775a3e..6328577873a 100644
--- a/lucene/core/src/java/org/apache/lucene/search/ScorerSupplier.java
+++ b/lucene/core/src/java/org/apache/lucene/search/ScorerSupplier.java
@@ -51,7 +51,7 @@ public abstract class ScorerSupplier {
*
* @see DocIdSetIterator#cost
*/
- public abstract long cost();
+ public abstract long cost() throws IOException;
/**
* Inform this {@link ScorerSupplier} that its returned scorers produce scores that get passed to
diff --git a/lucene/core/src/java/org/apache/lucene/search/SynonymQuery.java b/lucene/core/src/java/org/apache/lucene/search/SynonymQuery.java
index 761dc30b6fa..a350116e6b0 100644
--- a/lucene/core/src/java/org/apache/lucene/search/SynonymQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/SynonymQuery.java
@@ -17,7 +17,6 @@
package org.apache.lucene.search;
import java.io.IOException;
-import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
@@ -388,12 +387,8 @@ public final class SynonymQuery extends Query {
}
@Override
- public long cost() {
- try {
- init();
- } catch (IOException e) {
- throw new UncheckedIOException(e);
- }
+ public long cost() throws IOException {
+ init();
return cost;
}
};
diff --git a/lucene/core/src/java/org/apache/lucene/search/TermQuery.java b/lucene/core/src/java/org/apache/lucene/search/TermQuery.java
index 20b2d85ac16..290beb1acc1 100644
--- a/lucene/core/src/java/org/apache/lucene/search/TermQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/TermQuery.java
@@ -17,7 +17,6 @@
package org.apache.lucene.search;
import java.io.IOException;
-import java.io.UncheckedIOException;
import java.util.Objects;
import org.apache.lucene.index.IndexReaderContext;
import org.apache.lucene.index.LeafReader;
@@ -177,13 +176,9 @@ public class TermQuery extends Query {
}
@Override
- public long cost() {
- try {
- TermsEnum te = getTermsEnum();
- return te == null ? 0 : te.docFreq();
- } catch (IOException e) {
- throw new UncheckedIOException(e);
- }
+ public long cost() throws IOException {
+ TermsEnum te = getTermsEnum();
+ return te == null ? 0 : te.docFreq();
}
@Override
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestBooleanScorerSupplier.java b/lucene/core/src/test/org/apache/lucene/search/TestBooleanScorerSupplier.java
index fc6404c153a..09910bf4085 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestBooleanScorerSupplier.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestBooleanScorerSupplier.java
@@ -138,7 +138,7 @@ public class TestBooleanScorerSupplier extends LuceneTestCase {
}
}
- public void testConjunctionCost() {
+ public void testConjunctionCost() throws IOException {
Map<Occur, Collection<ScorerSupplier>> subs = new EnumMap<>(Occur.class);
for (Occur occur : Occur.values()) {
subs.put(occur, new ArrayList<>());
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestMaxScoreBulkScorer.java b/lucene/core/src/test/org/apache/lucene/search/TestMaxScoreBulkScorer.java
index 14946135369..834da692c97 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestMaxScoreBulkScorer.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestMaxScoreBulkScorer.java
@@ -1293,7 +1293,7 @@ public class TestMaxScoreBulkScorer extends LuceneTestCase {
}
@Override
- public long cost() {
+ public long cost() throws IOException {
return innerSS.cost();
}
};
@@ -1450,7 +1450,7 @@ public class TestMaxScoreBulkScorer extends LuceneTestCase {
}
@Override
- public long cost() {
+ public long cost() throws IOException {
return innerSS.cost();
}
};
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestWANDScorer.java b/lucene/core/src/test/org/apache/lucene/search/TestWANDScorer.java
index 5dd0fe6b2f2..d017a2d735e 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestWANDScorer.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestWANDScorer.java
@@ -1038,7 +1038,7 @@ public class TestWANDScorer extends LuceneTestCase {
}
@Override
- public long cost() {
+ public long cost() throws IOException {
return supplier.cost();
}
};
diff --git a/lucene/join/src/java/org/apache/lucene/search/join/ParentsChildrenBlockJoinQuery.java b/lucene/join/src/java/org/apache/lucene/search/join/ParentsChildrenBlockJoinQuery.java
index 0d14191bace..4274dc5e8bb 100644
--- a/lucene/join/src/java/org/apache/lucene/search/join/ParentsChildrenBlockJoinQuery.java
+++ b/lucene/join/src/java/org/apache/lucene/search/join/ParentsChildrenBlockJoinQuery.java
@@ -219,7 +219,7 @@ public class ParentsChildrenBlockJoinQuery extends Query {
}
@Override
- public long cost() {
+ public long cost() throws IOException {
if (cost == -1) {
// Calculate cost based on parent and child costs
// The cost should reflect the number of documents that will be visited
diff --git a/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java b/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java
index 3fc0f2fbf66..4744c5401ca 100644
--- a/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java
+++ b/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinQuery.java
@@ -175,7 +175,7 @@ public class ToParentBlockJoinQuery extends Query {
}
@Override
- public long cost() {
+ public long cost() throws IOException {
return childScorerSupplier.cost();
}
diff --git a/lucene/monitor/src/java/org/apache/lucene/monitor/ForceNoBulkScoringQuery.java b/lucene/monitor/src/java/org/apache/lucene/monitor/ForceNoBulkScoringQuery.java
index f293b487d97..e21025d29ad 100644
--- a/lucene/monitor/src/java/org/apache/lucene/monitor/ForceNoBulkScoringQuery.java
+++ b/lucene/monitor/src/java/org/apache/lucene/monitor/ForceNoBulkScoringQuery.java
@@ -101,7 +101,7 @@ class ForceNoBulkScoringQuery extends Query {
}
@Override
- public long cost() {
+ public long cost() throws IOException {
return innerScorerSupplier.cost();
}
diff --git a/lucene/monitor/src/test/org/apache/lucene/monitor/TestForceNoBulkScoringQuery.java b/lucene/monitor/src/test/org/apache/lucene/monitor/TestForceNoBulkScoringQuery.java
index e7d39448e62..4032650118f 100644
--- a/lucene/monitor/src/test/org/apache/lucene/monitor/TestForceNoBulkScoringQuery.java
+++ b/lucene/monitor/src/test/org/apache/lucene/monitor/TestForceNoBulkScoringQuery.java
@@ -154,7 +154,7 @@ public class TestForceNoBulkScoringQuery extends LuceneTestCase {
}
@Override
- public long cost() {
+ public long cost() throws IOException {
return innerSupplier.cost();
}
diff --git a/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerWeight.java b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerWeight.java
index 6b5ed7defd8..d29495182c3 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerWeight.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerWeight.java
@@ -87,7 +87,7 @@ class QueryProfilerWeight extends FilterWeight {
}
@Override
- public long cost() {
+ public long cost() throws IOException {
timer.start();
try {
return subQueryScorerSupplier.cost();
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingWeight.java b/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingWeight.java
index f46158fb2b1..acba44ad7a9 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingWeight.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingWeight.java
@@ -99,7 +99,7 @@ class AssertingWeight extends FilterWeight {
}
@Override
- public long cost() {
+ public long cost() throws IOException {
final long cost = inScorerSupplier.cost();
assert cost >= 0;
return cost;
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/search/DisablingBulkScorerQuery.java b/lucene/test-framework/src/java/org/apache/lucene/tests/search/DisablingBulkScorerQuery.java
index b042e0bd9c0..313deca6d26 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/search/DisablingBulkScorerQuery.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/search/DisablingBulkScorerQuery.java
@@ -63,7 +63,7 @@ public class DisablingBulkScorerQuery extends Query {
}
@Override
- public long cost() {
+ public long cost() throws IOException {
return scorerSupplier.cost();
}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/search/ScorerIndexSearcher.java b/lucene/test-framework/src/java/org/apache/lucene/tests/search/ScorerIndexSearcher.java
index d2e70e777b3..59f165d5b7b 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/search/ScorerIndexSearcher.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/search/ScorerIndexSearcher.java
@@ -76,7 +76,7 @@ public class ScorerIndexSearcher extends IndexSearcher {
}
@Override
- public long cost() {
+ public long cost() throws IOException {
return in.cost();
}
};