Skip to content

Apache Lucene open-source search software

Delegate docIDRunEnd in AssertingScorer's two-phase iterator (#16512)

Tim Allison4db07a50d050
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 91527017833..df93df3dc8a 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -281,6 +281,9 @@ Build
Other
---------------------
+* GITHUB#16512: AssertingScorer's two-phase iterator now delegates docIDRunEnd(),
+ so it exercises that contract in randomized tests. (Tim Allison)
+
* GITHUB#16074: Ban presized collection#toArray. (Zhou Hui)
* GITHUB#16063: Remove redundant final modifiers. (Zhou Hui)
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingScorer.java b/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingScorer.java
index 263a44f24ac..6de5adb2f66 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingScorer.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingScorer.java
@@ -278,6 +278,17 @@ public class AssertingScorer extends Scorer {
return matchCost;
}
+ @Override
+ public int docIDRunEnd() throws IOException {
+ assert state == IteratorState.APPROXIMATING || state == IteratorState.ITERATING : state;
+ int runEnd = in.docIDRunEnd();
+ // >= not >: a two-phase iterator's current doc is not necessarily a match, so
+ // runEnd == docID() ("no known run") is legal, unlike the DocIdSetIterator variant.
+ assert runEnd >= inApproximation.docID()
+ : "docIDRunEnd() " + runEnd + " < approximation docID " + inApproximation.docID();
+ return runEnd;
+ }
+
@Override
public String toString() {
return "AssertingScorer@asTwoPhaseIterator(" + in + ")";
diff --git a/lucene/test-framework/src/test/org/apache/lucene/tests/search/TestAssertingScorer.java b/lucene/test-framework/src/test/org/apache/lucene/tests/search/TestAssertingScorer.java
new file mode 100644
index 00000000000..65611219b09
--- /dev/null
+++ b/lucene/test-framework/src/test/org/apache/lucene/tests/search/TestAssertingScorer.java
@@ -0,0 +1,92 @@
+/*
+ * 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.tests.search;
+
+import java.io.IOException;
+import org.apache.lucene.search.ConstantScoreScorer;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.ScoreMode;
+import org.apache.lucene.search.Scorer;
+import org.apache.lucene.search.TwoPhaseIterator;
+import org.apache.lucene.tests.util.LuceneTestCase;
+
+public class TestAssertingScorer extends LuceneTestCase {
+
+ /**
+ * The wrapper must report the wrapped iterator's real {@link TwoPhaseIterator#docIDRunEnd()}, not
+ * the conservative default — otherwise framework-wrapped searches never exercise the
+ * bulk-collection fast paths that run ends enable.
+ */
+ public void testTwoPhaseDocIDRunEndDelegates() throws IOException {
+ TwoPhaseIterator asserting =
+ wrap(
+ new AllMatchesTwoPhase(20) {
+ @Override
+ public int docIDRunEnd() {
+ return 10;
+ }
+ });
+ assertEquals(0, asserting.approximation().nextDoc());
+ assertEquals(10, asserting.docIDRunEnd());
+ }
+
+ /** The conservative default ("no known run") must pass through unchanged too. */
+ public void testTwoPhaseDocIDRunEndConservativeDefault() throws IOException {
+ TwoPhaseIterator asserting = wrap(new AllMatchesTwoPhase(20));
+ assertEquals(0, asserting.approximation().nextDoc());
+ assertEquals(asserting.approximation().docID(), asserting.docIDRunEnd());
+ }
+
+ /** A run end below the approximation's current doc ID violates the contract. */
+ public void testTwoPhaseDocIDRunEndBelowCurrentDocTrips() throws IOException {
+ TwoPhaseIterator asserting =
+ wrap(
+ new AllMatchesTwoPhase(20) {
+ @Override
+ public int docIDRunEnd() {
+ return approximation().docID() - 1;
+ }
+ });
+ assertEquals(0, asserting.approximation().nextDoc());
+ if (TEST_ASSERTS_ENABLED) {
+ expectThrows(AssertionError.class, asserting::docIDRunEnd);
+ } else {
+ assertEquals(-1, asserting.docIDRunEnd());
+ }
+ }
+
+ private static class AllMatchesTwoPhase extends TwoPhaseIterator {
+ AllMatchesTwoPhase(int maxDoc) {
+ super(DocIdSetIterator.all(maxDoc));
+ }
+
+ @Override
+ public boolean matches() {
+ return true;
+ }
+
+ @Override
+ public float matchCost() {
+ return 1f;
+ }
+ }
+
+ private static TwoPhaseIterator wrap(TwoPhaseIterator tpi) {
+ Scorer scorer = new ConstantScoreScorer(1f, ScoreMode.COMPLETE_NO_SCORES, tpi);
+ return AssertingScorer.wrap(scorer, false, false).twoPhaseIterator();
+ }
+}