Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-6554: Removed illegal ToBlockJoinFieldComparator in favor of doc values selectors.

Adrien Grandef1741649413
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 9704e43890f..c06fe8bf987 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -155,6 +155,12 @@ API Changes
* LUCENE-6643: GroupingSearch from lucene/grouping was changed to take a Query
object to define groups instead of a Filter. (Adrien Grand)
+* LUCENE-6554: ToParentBlockJoinFieldComparator was removed because of a bug
+ with missing values that could not be fixed. ToParentBlockJoinSortField now
+ works with string or numeric doc values selectors. Sorting on anything else
+ than a string or numeric field would require to implement a custom selector.
+ (Adrien Grand)
+
Bug fixes
* LUCENE-6500: ParallelCompositeReader did not always call
diff --git a/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java b/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java
index 142b2cd56d5..8d23b0230c5 100644
--- a/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java
+++ b/lucene/core/src/java/org/apache/lucene/search/FieldComparator.java
@@ -152,7 +152,7 @@ public abstract class FieldComparator<T> {
protected void doSetNextReader(LeafReaderContext context) throws IOException {
currentReaderValues = getNumericDocValues(context, field);
if (missingValue != null) {
- docsWithField = DocValues.getDocsWithField(context.reader(), field);
+ docsWithField = getDocsWithValue(context, field);
// optimization to remove unneeded checks on the bit interface:
if (docsWithField instanceof Bits.MatchAllBits) {
docsWithField = null;
@@ -166,6 +166,11 @@ public abstract class FieldComparator<T> {
protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException {
return DocValues.getNumeric(context.reader(), field);
}
+
+ /** Retrieves a {@link Bits} instance representing documents that have a value in this segment. */
+ protected Bits getDocsWithValue(LeafReaderContext context, String field) throws IOException {
+ return DocValues.getDocsWithField(context.reader(), field);
+ }
}
/** Parses field's values as double (using {@link
diff --git a/lucene/join/src/java/org/apache/lucene/search/join/BlockJoinSelector.java b/lucene/join/src/java/org/apache/lucene/search/join/BlockJoinSelector.java
new file mode 100644
index 00000000000..1a0e14b9ae1
--- /dev/null
+++ b/lucene/join/src/java/org/apache/lucene/search/join/BlockJoinSelector.java
@@ -0,0 +1,204 @@
+package org.apache.lucene.search.join;
+
+/*
+ * 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.
+ */
+
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.index.NumericDocValues;
+import org.apache.lucene.index.SortedDocValues;
+import org.apache.lucene.index.SortedNumericDocValues;
+import org.apache.lucene.index.SortedSetDocValues;
+import org.apache.lucene.search.SortField;
+import org.apache.lucene.search.SortedNumericSelector;
+import org.apache.lucene.search.SortedSetSelector;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.BytesRef;
+
+/** Select a value from a block of documents.
+ * @lucene.internal */
+public class BlockJoinSelector {
+
+ /** Type of selection to perform. If none of the documents in the block have
+ * a value then no value will be selected. */
+ public enum Type {
+ MIN, MAX;
+ }
+
+ /** Return a {@link Bits} instance that returns true if, and only if, any of
+ * the children of the given parent document has a value. */
+ public static Bits wrap(final Bits docsWithValue, BitSet parents, BitSet children) {
+ return new Bits() {
+
+ @Override
+ public boolean get(int docID) {
+ assert parents.get(docID) : "this selector may only be used on parent documents";
+
+ if (docID == 0) {
+ // no children
+ return false;
+ }
+
+ final int firstChild = parents.prevSetBit(docID - 1) + 1;
+ for (int child = children.nextSetBit(firstChild); child < docID; child = children.nextSetBit(child + 1)) {
+ if (docsWithValue.get(child)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public int length() {
+ return docsWithValue.length();
+ }
+
+ };
+ }
+
+ /** Wraps the provided {@link SortedSetDocValues} in order to only select
+ * one value per parent among its {@code children} using the configured
+ * {@code selection} type. */
+ public static SortedDocValues wrap(SortedSetDocValues sortedSet, Type selection, BitSet parents, BitSet children) {
+ SortedDocValues values;
+ switch (selection) {
+ case MIN:
+ values = SortedSetSelector.wrap(sortedSet, SortedSetSelector.Type.MIN);
+ break;
+ case MAX:
+ values = SortedSetSelector.wrap(sortedSet, SortedSetSelector.Type.MAX);
+ break;
+ default:
+ throw new AssertionError();
+ }
+ return wrap(values, selection, parents, children);
+ }
+
+ /** Wraps the provided {@link SortedDocValues} in order to only select
+ * one value per parent among its {@code children} using the configured
+ * {@code selection} type. */
+ public static SortedDocValues wrap(final SortedDocValues values, Type selection, BitSet parents, BitSet children) {
+ return new SortedDocValues() {
+
+ @Override
+ public int getOrd(int docID) {
+ assert parents.get(docID) : "this selector may only be used on parent documents";
+
+ if (docID == 0) {
+ // no children
+ return -1;
+ }
+
+ final int firstChild = parents.prevSetBit(docID - 1) + 1;
+
+ int ord = -1;
+ for (int child = children.nextSetBit(firstChild); child < docID; child = children.nextSetBit(child + 1)) {
+ final int childOrd = values.getOrd(child);
+ switch (selection) {
+ case MIN:
+ if (ord == -1) {
+ ord = childOrd;
+ } else if (childOrd != -1) {
+ ord = Math.min(ord, childOrd);
+ }
+ break;
+ case MAX:
+ ord = Math.max(ord, childOrd);
+ break;
+ default:
+ throw new AssertionError();
+ }
+ }
+ return ord;
+ }
+
+ @Override
+ public BytesRef lookupOrd(int ord) {
+ return values.lookupOrd(ord);
+ }
+
+ @Override
+ public int getValueCount() {
+ return values.getValueCount();
+ }
+
+ };
+ }
+
+ /** Wraps the provided {@link SortedNumericDocValues} in order to only select
+ * one value per parent among its {@code children} using the configured
+ * {@code selection} type. */
+ public static NumericDocValues wrap(SortedNumericDocValues sortedNumerics, Type selection, BitSet parents, BitSet children) {
+ NumericDocValues values;
+ switch (selection) {
+ case MIN:
+ values = SortedNumericSelector.wrap(sortedNumerics, SortedNumericSelector.Type.MIN, SortField.Type.LONG);
+ break;
+ case MAX:
+ values = SortedNumericSelector.wrap(sortedNumerics, SortedNumericSelector.Type.MAX, SortField.Type.LONG);
+ break;
+ default:
+ throw new AssertionError();
+ }
+ return wrap(values, DocValues.docsWithValue(sortedNumerics, parents.length()), selection, parents, children);
+ }
+
+ public static NumericDocValues wrap(final NumericDocValues values, Bits docsWithValue, Type selection, BitSet parents, BitSet children) {
+ return new NumericDocValues() {
+
+ @Override
+ public long get(int docID) {
+ assert parents.get(docID) : "this selector may only be used on parent documents";
+
+ if (docID == 0) {
+ // no children
+ return 0;
+ }
+
+ final int firstChild = parents.prevSetBit(docID - 1) + 1;
+
+ long value = 0;
+ boolean hasValue = false;
+
+ for (int child = children.nextSetBit(firstChild); child < docID; child = children.nextSetBit(child + 1)) {
+ final long childValue = values.get(child);
+ final boolean childHasValue = value != 0 || docsWithValue.get(child);
+
+ if (hasValue == false) {
+ value = childValue;
+ hasValue = childHasValue;
+ } else if (childHasValue) {
+ switch (selection) {
+ case MIN:
+ value = Math.min(value, childValue);
+ break;
+ case MAX:
+ value = Math.max(value, childValue);
+ break;
+ default:
+ throw new AssertionError();
+ }
+ }
+ }
+
+ return value;
+ }
+
+ };
+ }
+
+}
diff --git a/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinFieldComparator.java b/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinFieldComparator.java
deleted file mode 100644
index 04ef679e139..00000000000
--- a/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinFieldComparator.java
+++ /dev/null
@@ -1,322 +0,0 @@
-package org.apache.lucene.search.join;
-
-/*
- * 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.
- */
-
-import java.io.IOException;
-
-import org.apache.lucene.index.LeafReaderContext;
-import org.apache.lucene.search.DocIdSetIterator;
-import org.apache.lucene.search.FieldComparator;
-import org.apache.lucene.search.LeafFieldComparator;
-import org.apache.lucene.search.SimpleFieldComparator;
-import org.apache.lucene.util.BitDocIdSet;
-import org.apache.lucene.util.BitSet;
-
-/**
- * A field comparator that allows parent documents to be sorted by fields
- * from the nested / child documents.
- *
- * @lucene.experimental
- */
-public abstract class ToParentBlockJoinFieldComparator extends SimpleFieldComparator<Object> implements LeafFieldComparator { // repeat LeafFieldComparator for javadocs
-
- private final BitDocIdSetFilter parentFilter;
- private final BitDocIdSetFilter childFilter;
- final int spareSlot;
-
- FieldComparator<Object> wrappedComparator;
- LeafFieldComparator wrappedLeafComparator;
- BitSet parentDocuments;
- BitSet childDocuments;
-
- ToParentBlockJoinFieldComparator(FieldComparator<Object> wrappedComparator, BitDocIdSetFilter parentFilter, BitDocIdSetFilter childFilter, int spareSlot) {
- this.wrappedComparator = wrappedComparator;
- this.parentFilter = parentFilter;
- this.childFilter = childFilter;
- this.spareSlot = spareSlot;
- }
-
- @Override
- public int compare(int slot1, int slot2) {
- return wrappedComparator.compare(slot1, slot2);
- }
-
- @Override
- public void setBottom(int slot) {
- wrappedLeafComparator.setBottom(slot);
- }
-
- @Override
- public void setTopValue(Object value) {
- wrappedComparator.setTopValue(value);
- }
-
- @Override
- protected void doSetNextReader(LeafReaderContext context) throws IOException {
- BitDocIdSet children = childFilter.getDocIdSet(context);
- if (children == null) {
- childDocuments = null;
- } else {
- childDocuments = children.bits();
- }
- BitDocIdSet parents = parentFilter.getDocIdSet(context);
- if (parents == null) {
- parentDocuments = null;
- } else {
- parentDocuments = parents.bits();
- }
- wrappedLeafComparator = wrappedComparator.getLeafComparator(context);
- }
-
- @Override
- public Object value(int slot) {
- return wrappedComparator.value(slot);
- }
-
- /**
- * Concrete implementation of {@link ToParentBlockJoinSortField} to sorts the parent docs with the lowest values
- * in the child / nested docs first.
- */
- public static final class Lowest extends ToParentBlockJoinFieldComparator implements LeafFieldComparator {
-
- /**
- * Create ToParentBlockJoinFieldComparator.Lowest
- *
- * @param wrappedComparator The {@link LeafFieldComparator} on the child / nested level.
- * @param parentFilter Filter that identifies the parent documents.
- * @param childFilter Filter that defines which child / nested documents participates in sorting.
- * @param spareSlot The extra slot inside the wrapped comparator that is used to compare which nested document
- * inside the parent document scope is most competitive.
- */
- public Lowest(FieldComparator<Object> wrappedComparator, BitDocIdSetFilter parentFilter, BitDocIdSetFilter childFilter, int spareSlot) {
- super(wrappedComparator, parentFilter, childFilter, spareSlot);
- }
-
- @Override
- public int compareBottom(int parentDoc) throws IOException {
- if (parentDoc == 0 || parentDocuments == null || childDocuments == null) {
- return 0;
- }
-
- // We need to copy the lowest value from all child docs into slot.
- int prevParentDoc = parentDocuments.prevSetBit(parentDoc - 1);
- int childDoc = childDocuments.nextSetBit(prevParentDoc + 1);
- if (childDoc >= parentDoc || childDoc == DocIdSetIterator.NO_MORE_DOCS) {
- return 0;
- }
-
- // We only need to emit a single cmp value for any matching child doc
- int cmp = wrappedLeafComparator.compareBottom(childDoc);
- if (cmp > 0) {
- return cmp;
- }
-
- while (true) {
- childDoc = childDocuments.nextSetBit(childDoc + 1);
- if (childDoc >= parentDoc || childDoc == DocIdSetIterator.NO_MORE_DOCS) {
- return cmp;
- }
- int cmp1 = wrappedLeafComparator.compareBottom(childDoc);
- if (cmp1 > 0) {
- return cmp1;
- } else {
- if (cmp1 == 0) {
- cmp = 0;
- }
- }
- }
- }
-
- @Override
- public void copy(int slot, int parentDoc) throws IOException {
- if (parentDoc == 0 || parentDocuments == null || childDocuments == null) {
- return;
- }
-
- // We need to copy the lowest value from all child docs into slot.
- int prevParentDoc = parentDocuments.prevSetBit(parentDoc - 1);
- int childDoc = childDocuments.nextSetBit(prevParentDoc + 1);
- if (childDoc >= parentDoc || childDoc == DocIdSetIterator.NO_MORE_DOCS) {
- return;
- }
- wrappedLeafComparator.copy(spareSlot, childDoc);
- wrappedLeafComparator.copy(slot, childDoc);
-
- while (true) {
- childDoc = childDocuments.nextSetBit(childDoc + 1);
- if (childDoc >= parentDoc || childDoc == DocIdSetIterator.NO_MORE_DOCS) {
- return;
- }
- wrappedLeafComparator.copy(spareSlot, childDoc);
- if (wrappedComparator.compare(spareSlot, slot) < 0) {
- wrappedLeafComparator.copy(slot, childDoc);
- }
- }
- }
-
- @Override
- public int compareTop(int parentDoc) throws IOException {
- if (parentDoc == 0 || parentDocuments == null || childDocuments == null) {
- return 0;
- }
-
- // We need to copy the lowest value from all nested docs into slot.
- int prevParentDoc = parentDocuments.prevSetBit(parentDoc - 1);
- int childDoc = childDocuments.nextSetBit(prevParentDoc + 1);
- if (childDoc >= parentDoc || childDoc == DocIdSetIterator.NO_MORE_DOCS) {
- return 0;
- }
-
- // We only need to emit a single cmp value for any matching child doc
- int cmp = wrappedLeafComparator.compareBottom(childDoc);
- if (cmp > 0) {
- return cmp;
- }
-
- while (true) {
- childDoc = childDocuments.nextSetBit(childDoc + 1);
- if (childDoc >= parentDoc || childDoc == DocIdSetIterator.NO_MORE_DOCS) {
- return cmp;
- }
- int cmp1 = wrappedLeafComparator.compareTop(childDoc);
- if (cmp1 > 0) {
- return cmp1;
- } else {
- if (cmp1 == 0) {
- cmp = 0;
- }
- }
- }
- }
-
- }
-
- /**
- * Concrete implementation of {@link ToParentBlockJoinSortField} to sorts the parent docs with the highest values
- * in the child / nested docs first.
- */
- public static final class Highest extends ToParentBlockJoinFieldComparator implements LeafFieldComparator {
-
- /**
- * Create ToParentBlockJoinFieldComparator.Highest
- *
- * @param wrappedComparator The {@link LeafFieldComparator} on the child / nested level.
- * @param parentFilter Filter that identifies the parent documents.
- * @param childFilter Filter that defines which child / nested documents participates in sorting.
- * @param spareSlot The extra slot inside the wrapped comparator that is used to compare which nested document
- * inside the parent document scope is most competitive.
- */
- public Highest(FieldComparator<Object> wrappedComparator, BitDocIdSetFilter parentFilter, BitDocIdSetFilter childFilter, int spareSlot) {
- super(wrappedComparator, parentFilter, childFilter, spareSlot);
- }
-
- @Override
- public int compareBottom(int parentDoc) throws IOException {
- if (parentDoc == 0 || parentDocuments == null || childDocuments == null) {
- return 0;
- }
-
- int prevParentDoc = parentDocuments.prevSetBit(parentDoc - 1);
- int childDoc = childDocuments.nextSetBit(prevParentDoc + 1);
- if (childDoc >= parentDoc || childDoc == DocIdSetIterator.NO_MORE_DOCS) {
- return 0;
- }
-
- int cmp = wrappedLeafComparator.compareBottom(childDoc);
- if (cmp < 0) {
- return cmp;
- }
-
- while (true) {
- childDoc = childDocuments.nextSetBit(childDoc + 1);
- if (childDoc >= parentDoc || childDoc == DocIdSetIterator.NO_MORE_DOCS) {
- return cmp;
- }
- int cmp1 = wrappedLeafComparator.compareBottom(childDoc);
- if (cmp1 < 0) {
- return cmp1;
- } else {
- if (cmp1 == 0) {
- cmp = 0;
- }
- }
- }
- }
-
- @Override
- public void copy(int slot, int parentDoc) throws IOException {
- if (parentDoc == 0 || parentDocuments == null || childDocuments == null) {
- return;
- }
-
- int prevParentDoc = parentDocuments.prevSetBit(parentDoc - 1);
- int childDoc = childDocuments.nextSetBit(prevParentDoc + 1);
- if (childDoc >= parentDoc || childDoc == DocIdSetIterator.NO_MORE_DOCS) {
- return;
- }
- wrappedLeafComparator.copy(spareSlot, childDoc);
- wrappedLeafComparator.copy(slot, childDoc);
-
- while (true) {
- childDoc = childDocuments.nextSetBit(childDoc + 1);
- if (childDoc >= parentDoc || childDoc == DocIdSetIterator.NO_MORE_DOCS) {
- return;
- }
- wrappedLeafComparator.copy(spareSlot, childDoc);
- if (wrappedComparator.compare(spareSlot, slot) > 0) {
- wrappedLeafComparator.copy(slot, childDoc);
- }
- }
- }
-
- @Override
- public int compareTop(int parentDoc) throws IOException {
- if (parentDoc == 0 || parentDocuments == null || childDocuments == null) {
- return 0;
- }
-
- int prevParentDoc = parentDocuments.prevSetBit(parentDoc - 1);
- int childDoc = childDocuments.nextSetBit(prevParentDoc + 1);
- if (childDoc >= parentDoc || childDoc == DocIdSetIterator.NO_MORE_DOCS) {
- return 0;
- }
-
- int cmp = wrappedLeafComparator.compareBottom(childDoc);
- if (cmp < 0) {
- return cmp;
- }
-
- while (true) {
- childDoc = childDocuments.nextSetBit(childDoc + 1);
- if (childDoc >= parentDoc || childDoc == DocIdSetIterator.NO_MORE_DOCS) {
- return cmp;
- }
- int cmp1 = wrappedLeafComparator.compareTop(childDoc);
- if (cmp1 < 0) {
- return cmp1;
- } else {
- if (cmp1 == 0) {
- cmp = 0;
- }
- }
- }
- }
-
- }
-
-}
diff --git a/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinSortField.java b/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinSortField.java
index 4d6a6ed38db..801bb8c7d62 100644
--- a/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinSortField.java
+++ b/lucene/join/src/java/org/apache/lucene/search/join/ToParentBlockJoinSortField.java
@@ -17,9 +17,17 @@ package org.apache.lucene.search.join;
* limitations under the License.
*/
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.NumericDocValues;
+import org.apache.lucene.index.SortedDocValues;
+import org.apache.lucene.index.SortedNumericDocValues;
+import org.apache.lucene.index.SortedSetDocValues;
import org.apache.lucene.search.FieldComparator;
-import org.apache.lucene.search.LeafFieldComparator;
import org.apache.lucene.search.SortField;
+import org.apache.lucene.util.BitDocIdSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.NumericUtils;
import java.io.IOException;
@@ -46,6 +54,17 @@ public class ToParentBlockJoinSortField extends SortField {
*/
public ToParentBlockJoinSortField(String field, Type type, boolean reverse, BitDocIdSetFilter parentFilter, BitDocIdSetFilter childFilter) {
super(field, type, reverse);
+ switch (getType()) {
+ case STRING:
+ case DOUBLE:
+ case FLOAT:
+ case LONG:
+ case INT:
+ // ok
+ break;
+ default:
+ throw new UnsupportedOperationException("Sort type " + type + " is not supported");
+ }
this.order = reverse;
this.parentFilter = parentFilter;
this.childFilter = childFilter;
@@ -69,14 +88,156 @@ public class ToParentBlockJoinSortField extends SortField {
}
@Override
- @SuppressWarnings("unchecked")
public FieldComparator<?> getComparator(int numHits, int sortPos) throws IOException {
- FieldComparator<Object> wrappedFieldComparator = (FieldComparator) super.getComparator(numHits + 1, sortPos);
- if (order) {
- return new ToParentBlockJoinFieldComparator.Highest(wrappedFieldComparator, parentFilter, childFilter, numHits);
- } else {
- return new ToParentBlockJoinFieldComparator.Lowest(wrappedFieldComparator, parentFilter, childFilter, numHits);
+ switch (getType()) {
+ case STRING:
+ return getStringComparator(numHits);
+ case DOUBLE:
+ return getDoubleComparator(numHits);
+ case FLOAT:
+ return getFloatComparator(numHits);
+ case LONG:
+ return getLongComparator(numHits);
+ case INT:
+ return getIntComparator(numHits);
+ default:
+ throw new UnsupportedOperationException("Sort type " + getType() + " is not supported");
}
}
+ private FieldComparator<?> getStringComparator(int numHits) {
+ return new FieldComparator.TermOrdValComparator(numHits, getField(), missingValue == STRING_LAST) {
+
+ @Override
+ protected SortedDocValues getSortedDocValues(LeafReaderContext context, String field) throws IOException {
+ SortedSetDocValues sortedSet = DocValues.getSortedSet(context.reader(), field);
+ final BlockJoinSelector.Type type = order
+ ? BlockJoinSelector.Type.MAX
+ : BlockJoinSelector.Type.MIN;
+ final BitDocIdSet parents = parentFilter.getDocIdSet(context);
+ final BitDocIdSet children = childFilter.getDocIdSet(context);
+ if (children == null) {
+ return DocValues.emptySorted();
+ }
+ return BlockJoinSelector.wrap(sortedSet, type, parents.bits(), children.bits());
+ }
+
+ };
+ }
+
+ private FieldComparator<?> getIntComparator(int numHits) {
+ return new FieldComparator.IntComparator(numHits, getField(), (Integer) missingValue) {
+ @Override
+ protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException {
+ SortedNumericDocValues sortedNumeric = DocValues.getSortedNumeric(context.reader(), field);
+ final BlockJoinSelector.Type type = order
+ ? BlockJoinSelector.Type.MAX
+ : BlockJoinSelector.Type.MIN;
+ final BitDocIdSet parents = parentFilter.getDocIdSet(context);
+ final BitDocIdSet children = childFilter.getDocIdSet(context);
+ if (children == null) {
+ return DocValues.emptyNumeric();
+ }
+ return BlockJoinSelector.wrap(sortedNumeric, type, parents.bits(), children.bits());
+ }
+ @Override
+ protected Bits getDocsWithValue(LeafReaderContext context, String field) throws IOException {
+ final Bits docsWithValue = DocValues.getDocsWithField(context.reader(), field);
+ final BitDocIdSet parents = parentFilter.getDocIdSet(context);
+ final BitDocIdSet children = childFilter.getDocIdSet(context);
+ if (children == null) {
+ return new Bits.MatchNoBits(context.reader().maxDoc());
+ }
+ return BlockJoinSelector.wrap(docsWithValue, parents.bits(), children.bits());
+ }
+ };
+ }
+
+ private FieldComparator<?> getLongComparator(int numHits) {
+ return new FieldComparator.LongComparator(numHits, getField(), (Long) missingValue) {
+ @Override
+ protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException {
+ SortedNumericDocValues sortedNumeric = DocValues.getSortedNumeric(context.reader(), field);
+ final BlockJoinSelector.Type type = order
+ ? BlockJoinSelector.Type.MAX
+ : BlockJoinSelector.Type.MIN;
+ final BitDocIdSet parents = parentFilter.getDocIdSet(context);
+ final BitDocIdSet children = childFilter.getDocIdSet(context);
+ if (children == null) {
+ return DocValues.emptyNumeric();
+ }
+ return BlockJoinSelector.wrap(sortedNumeric, type, parents.bits(), children.bits());
+ }
+ @Override
+ protected Bits getDocsWithValue(LeafReaderContext context, String field) throws IOException {
+ final Bits docsWithValue = DocValues.getDocsWithField(context.reader(), field);
+ final BitDocIdSet parents = parentFilter.getDocIdSet(context);
+ final BitDocIdSet children = childFilter.getDocIdSet(context);
+ if (children == null) {
+ return new Bits.MatchNoBits(context.reader().maxDoc());
+ }
+ return BlockJoinSelector.wrap(docsWithValue, parents.bits(), children.bits());
+ }
+ };
+ }
+
+ private FieldComparator<?> getFloatComparator(int numHits) {
+ return new FieldComparator.FloatComparator(numHits, getField(), (Float) missingValue) {
+ @Override
+ protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException {
+ SortedNumericDocValues sortedNumeric = DocValues.getSortedNumeric(context.reader(), field);
+ final BlockJoinSelector.Type type = order
+ ? BlockJoinSelector.Type.MAX
+ : BlockJoinSelector.Type.MIN;
+ final BitDocIdSet parents = parentFilter.getDocIdSet(context);
+ final BitDocIdSet children = childFilter.getDocIdSet(context);
+ if (children == null) {
+ return DocValues.emptyNumeric();
+ }
+ final NumericDocValues view = BlockJoinSelector.wrap(sortedNumeric, type, parents.bits(), children.bits());
+ // undo the numericutils sortability
+ return new NumericDocValues() {
+ @Override
+ public long get(int docID) {
+ return NumericUtils.sortableFloatBits((int) view.get(docID));
+ }
+ };
+ }
+ };
+ }
+
+ private FieldComparator<?> getDoubleComparator(int numHits) {
+ return new FieldComparator.DoubleComparator(numHits, getField(), (Double) missingValue) {
+ @Override
+ protected NumericDocValues getNumericDocValues(LeafReaderContext context, String field) throws IOException {
+ SortedNumericDocValues sortedNumeric = DocValues.getSortedNumeric(context.reader(), field);
+ final BlockJoinSelector.Type type = order
+ ? BlockJoinSelector.Type.MAX
+ : BlockJoinSelector.Type.MIN;
+ final BitDocIdSet parents = parentFilter.getDocIdSet(context);
+ final BitDocIdSet children = childFilter.getDocIdSet(context);
+ if (children == null) {
+ return DocValues.emptyNumeric();
+ }
+ final NumericDocValues view = BlockJoinSelector.wrap(sortedNumeric, type, parents.bits(), children.bits());
+ // undo the numericutils sortability
+ return new NumericDocValues() {
+ @Override
+ public long get(int docID) {
+ return NumericUtils.sortableDoubleBits(view.get(docID));
+ }
+ };
+ }
+ @Override
+ protected Bits getDocsWithValue(LeafReaderContext context, String field) throws IOException {
+ final Bits docsWithValue = DocValues.getDocsWithField(context.reader(), field);
+ final BitDocIdSet parents = parentFilter.getDocIdSet(context);
+ final BitDocIdSet children = childFilter.getDocIdSet(context);
+ if (children == null) {
+ return new Bits.MatchNoBits(context.reader().maxDoc());
+ }
+ return BlockJoinSelector.wrap(docsWithValue, parents.bits(), children.bits());
+ }
+ };
+ }
}
diff --git a/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinSelector.java b/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinSelector.java
new file mode 100644
index 00000000000..ff240cc7b36
--- /dev/null
+++ b/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinSelector.java
@@ -0,0 +1,178 @@
+package org.apache.lucene.search.join;
+
+/*
+ * 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.
+ */
+
+
+import java.util.Arrays;
+
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.index.NumericDocValues;
+import org.apache.lucene.index.SortedDocValues;
+import org.apache.lucene.util.BitSet;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.FixedBitSet;
+import org.apache.lucene.util.LuceneTestCase;
+
+public class TestBlockJoinSelector extends LuceneTestCase {
+
+ public void testDocsWithValue() {
+ final BitSet parents = new FixedBitSet(20);
+ parents.set(0);
+ parents.set(5);
+ parents.set(6);
+ parents.set(10);
+ parents.set(15);
+ parents.set(19);
+
+ final BitSet children = new FixedBitSet(20);
+ children.set(2);
+ children.set(3);
+ children.set(4);
+ children.set(12);
+ children.set(17);
+
+ final BitSet childDocsWithValue = new FixedBitSet(20);
+ childDocsWithValue.set(2);
+ childDocsWithValue.set(3);
+ childDocsWithValue.set(4);
+ childDocsWithValue.set(8);
+ childDocsWithValue.set(16);
+
+ final Bits docsWithValue = BlockJoinSelector.wrap(childDocsWithValue, parents, children);
+ assertFalse(docsWithValue.get(0));
+ assertTrue(docsWithValue.get(5));
+ assertFalse(docsWithValue.get(6));
+ assertFalse(docsWithValue.get(10));
+ assertFalse(docsWithValue.get(15));
+ assertFalse(docsWithValue.get(19));
+ }
+
+ public void testSortedSelector() {
+ final BitSet parents = new FixedBitSet(20);
+ parents.set(0);
+ parents.set(5);
+ parents.set(6);
+ parents.set(10);
+ parents.set(15);
+ parents.set(19);
+
+ final BitSet children = new FixedBitSet(20);
+ children.set(2);
+ children.set(3);
+ children.set(4);
+ children.set(12);
+ children.set(17);
+
+ final int[] ords = new int[20];
+ Arrays.fill(ords, -1);
+ ords[2] = 5;
+ ords[3] = 7;
+ ords[4] = 3;
+ ords[12] = 10;
+ ords[18] = 10;
+ final SortedDocValues values = new SortedDocValues() {
+
+ @Override
+ public int getOrd(int docID) {
+ return ords[docID];
+ }
+
+ @Override
+ public BytesRef lookupOrd(int ord) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public int getValueCount() {
+ return 11;
+ }
+
+ };
+
+ final SortedDocValues mins = BlockJoinSelector.wrap(DocValues.singleton(values), BlockJoinSelector.Type.MIN, parents, children);
+ assertEquals(-1, mins.getOrd(0));
+ assertEquals(3, mins.getOrd(5));
+ assertEquals(-1, mins.getOrd(6));
+ assertEquals(-1, mins.getOrd(10));
+ assertEquals(10, mins.getOrd(15));
+ assertEquals(-1, mins.getOrd(19));
+
+ final SortedDocValues maxs = BlockJoinSelector.wrap(DocValues.singleton(values), BlockJoinSelector.Type.MAX, parents, children);
+ assertEquals(-1, maxs.getOrd(0));
+ assertEquals(7, maxs.getOrd(5));
+ assertEquals(-1, maxs.getOrd(6));
+ assertEquals(-1, maxs.getOrd(10));
+ assertEquals(10, maxs.getOrd(15));
+ assertEquals(-1, maxs.getOrd(19));
+ }
+
+ public void testNumericSelector() {
+ final BitSet parents = new FixedBitSet(20);
+ parents.set(0);
+ parents.set(5);
+ parents.set(6);
+ parents.set(10);
+ parents.set(15);
+ parents.set(19);
+
+ final BitSet children = new FixedBitSet(20);
+ children.set(2);
+ children.set(3);
+ children.set(4);
+ children.set(12);
+ children.set(17);
+
+ final long[] longs = new long[20];
+ final BitSet docsWithValue = new FixedBitSet(20);
+ docsWithValue.set(2);
+ longs[2] = 5;
+ docsWithValue.set(3);
+ longs[3] = 7;
+ docsWithValue.set(4);
+ longs[4] = 3;
+ docsWithValue.set(12);
+ longs[12] = 10;
+ docsWithValue.set(18);
+ longs[18] = 10;
+ final NumericDocValues values = new NumericDocValues() {
+
+ @Override
+ public long get(int docID) {
+ return longs[docID];
+ }
+
+ };
+
+ final NumericDocValues mins = BlockJoinSelector.wrap(DocValues.singleton(values, docsWithValue), BlockJoinSelector.Type.MIN, parents, children);
+ assertEquals(0, mins.get(0));
+ assertEquals(3, mins.get(5));
+ assertEquals(0, mins.get(6));
+ assertEquals(0, mins.get(10));
+ assertEquals(10, mins.get(15));
+ assertEquals(0, mins.get(19));
+
+ final NumericDocValues maxs = BlockJoinSelector.wrap(DocValues.singleton(values, docsWithValue), BlockJoinSelector.Type.MAX, parents, children);
+ assertEquals(0, maxs.get(0));
+ assertEquals(7, maxs.get(5));
+ assertEquals(0, maxs.get(6));
+ assertEquals(0, maxs.get(10));
+ assertEquals(10, maxs.get(15));
+ assertEquals(0, maxs.get(19));
+ }
+}