Skip to content

Apache Lucene open-source search software

LUCENE-5873: BJQ: Throw an error if the top-level filter is not in the child space instead of just asserting.

Adrien Grand64d933d34d81
diff --git a/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java b/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java
index 65f4bcfcd39..3d6d8f01b80 100644
--- a/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java
+++ b/lucene/join/src/java/org/apache/lucene/search/join/ToChildBlockJoinQuery.java
@@ -50,6 +50,7 @@ public class ToChildBlockJoinQuery extends Query {
* ToChildBlockJoinScorer#validateParentDoc} on mis-use,
* when the parent query incorrectly returns child docs. */
static final String INVALID_QUERY_MESSAGE = "Parent query yields document which is not matched by parents filter, docID=";
+ static final String ILLEGAL_ADVANCE_ON_PARENT = "Expect to be advanced on child docs only. got docID=";
private final BitDocIdSetFilter parentsFilter;
private final Query parentQuery;
@@ -279,7 +280,6 @@ public class ToChildBlockJoinQuery extends Query {
@Override
public int advance(int childTarget) throws IOException {
- assert childTarget >= parentBits.length() || !parentBits.get(childTarget);
//System.out.println("Q.advance childTarget=" + childTarget);
if (childTarget == NO_MORE_DOCS) {
@@ -287,6 +287,10 @@ public class ToChildBlockJoinQuery extends Query {
return childDoc = parentDoc = NO_MORE_DOCS;
}
+ if (parentBits.get(childTarget)) {
+ throw new IllegalStateException(ILLEGAL_ADVANCE_ON_PARENT + childTarget);
+ }
+
assert childDoc == -1 || childTarget != parentDoc: "childTarget=" + childTarget;
if (childDoc == -1 || childTarget > parentDoc) {
// Advance to new parent:
diff --git a/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinValidation.java b/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinValidation.java
index 740cbaedaf3..596f3abe6ff 100644
--- a/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinValidation.java
+++ b/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinValidation.java
@@ -30,6 +30,7 @@ import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.Filter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryWrapperFilter;
@@ -112,6 +113,17 @@ public class TestBlockJoinValidation extends LuceneTestCase {
indexSearcher.search(blockJoinQuery, 1);
}
+ @Test
+ public void testValidationForToChildBjqWithChildFilterQuery() throws Exception {
+ Query parentQueryWithRandomChild = createParentQuery();
+
+ ToChildBlockJoinQuery blockJoinQuery = new ToChildBlockJoinQuery(parentQueryWithRandomChild, parentsFilter, false);
+ Filter childFilter = new QueryWrapperFilter(new TermQuery(new Term("common_field", "1")));
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage(ToChildBlockJoinQuery.ILLEGAL_ADVANCE_ON_PARENT);
+ indexSearcher.search(blockJoinQuery, childFilter, 1);
+ }
+
@Test
public void testAdvanceValidationForToChildBjq() throws Exception {
int randomChildNumber = getRandomChildNumber(0);
@@ -163,6 +175,7 @@ public class TestBlockJoinValidation extends LuceneTestCase {
Document result = new Document();
result.add(newStringField("id", createFieldValue(segmentNumber * AMOUNT_OF_PARENT_DOCS + parentNumber), Field.Store.YES));
result.add(newStringField("parent", createFieldValue(parentNumber), Field.Store.NO));
+ result.add(newStringField("common_field", "1", Field.Store.NO));
return result;
}
@@ -170,6 +183,7 @@ public class TestBlockJoinValidation extends LuceneTestCase {
Document result = new Document();
result.add(newStringField("id", createFieldValue(segmentNumber * AMOUNT_OF_PARENT_DOCS + parentNumber, childNumber), Field.Store.YES));
result.add(newStringField("child", createFieldValue(childNumber), Field.Store.NO));
+ result.add(newStringField("common_field", "1", Field.Store.NO));
return result;
}
@@ -201,6 +215,10 @@ public class TestBlockJoinValidation extends LuceneTestCase {
return childQueryWithRandomParent;
}
+ private static Query createParentQuery() {
+ return new TermQuery(new Term("id", createFieldValue(getRandomParentId())));
+ }
+
private static int getRandomParentId() {
return random().nextInt(AMOUNT_OF_PARENT_DOCS * AMOUNT_OF_SEGMENTS);
}