Skip to content

Apache Lucene open-source search software

LUCENE-6719: Fix NumericUtils getMin/Max methods to return null if there are no terms for the specified field

Timothy Pottera42e164b3d94
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index c7cb9f275a6..7bb5e05c087 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -242,6 +242,11 @@ API Changes
add dedicated method to consider all context values at query time.
(Areek Zillur, Mike McCandless)
+* LUCENE-6719: NumericUtils getMinInt, getMaxInt, getMinLong, getMaxLong now
+ return null if there are no terms for the specified field, previously these
+ methods returned primitive values and raised an undocumented NullPointerException
+ if there were no terms for the field. (hossman, Timothy Potter)
+
Bug fixes
* LUCENE-6500: ParallelCompositeReader did not always call
diff --git a/lucene/core/src/java/org/apache/lucene/util/NumericUtils.java b/lucene/core/src/java/org/apache/lucene/util/NumericUtils.java
index c7584401b2c..6579a662824 100644
--- a/lucene/core/src/java/org/apache/lucene/util/NumericUtils.java
+++ b/lucene/core/src/java/org/apache/lucene/util/NumericUtils.java
@@ -552,34 +552,46 @@ public final class NumericUtils {
};
}
- /** Returns the minimum int value indexed into this
- * numeric field. */
- public static int getMinInt(Terms terms) throws IOException {
+ /**
+ * Returns the minimum int value indexed into this
+ * numeric field or null if no terms exist.
+ */
+ public static Integer getMinInt(Terms terms) throws IOException {
// All shift=0 terms are sorted first, so we don't need
// to filter the incoming terms; we can just get the
- // min:
- return NumericUtils.prefixCodedToInt(terms.getMin());
+ // min:
+ BytesRef min = terms.getMin();
+ return (min != null) ? NumericUtils.prefixCodedToInt(min) : null;
}
- /** Returns the maximum int value indexed into this
- * numeric field. */
- public static int getMaxInt(Terms terms) throws IOException {
- return NumericUtils.prefixCodedToInt(intTerms(terms).getMax());
+ /**
+ * Returns the maximum int value indexed into this
+ * numeric field or null if no terms exist.
+ */
+ public static Integer getMaxInt(Terms terms) throws IOException {
+ BytesRef max = intTerms(terms).getMax();
+ return (max != null) ? NumericUtils.prefixCodedToInt(max) : null;
}
- /** Returns the minimum long value indexed into this
- * numeric field. */
- public static long getMinLong(Terms terms) throws IOException {
+ /**
+ * Returns the minimum long value indexed into this
+ * numeric field or null if no terms exist.
+ */
+ public static Long getMinLong(Terms terms) throws IOException {
// All shift=0 terms are sorted first, so we don't need
// to filter the incoming terms; we can just get the
- // min:
- return NumericUtils.prefixCodedToLong(terms.getMin());
+ // min:
+ BytesRef min = terms.getMin();
+ return (min != null) ? NumericUtils.prefixCodedToLong(min) : null;
}
- /** Returns the maximum long value indexed into this
- * numeric field. */
- public static long getMaxLong(Terms terms) throws IOException {
- return NumericUtils.prefixCodedToLong(longTerms(terms).getMax());
+ /**
+ * Returns the maximum long value indexed into this
+ * numeric field or null if no terms exist.
+ */
+ public static Long getMaxLong(Terms terms) throws IOException {
+ BytesRef max = longTerms(terms).getMax();
+ return (max != null) ? NumericUtils.prefixCodedToLong(max) : null;
}
}
diff --git a/lucene/core/src/test/org/apache/lucene/index/TestTerms.java b/lucene/core/src/test/org/apache/lucene/index/TestTerms.java
index ac39b1a0ec7..617ec34f837 100644
--- a/lucene/core/src/test/org/apache/lucene/index/TestTerms.java
+++ b/lucene/core/src/test/org/apache/lucene/index/TestTerms.java
@@ -91,6 +91,11 @@ public class TestTerms extends LuceneTestCase {
dir.close();
}
+ public void testEmptyIntFieldMinMax() throws Exception {
+ assertNull(NumericUtils.getMinInt(EMPTY_TERMS));
+ assertNull(NumericUtils.getMaxInt(EMPTY_TERMS));
+ }
+
public void testIntFieldMinMax() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir);
@@ -108,14 +113,19 @@ public class TestTerms extends LuceneTestCase {
IndexReader r = w.getReader();
Terms terms = MultiFields.getTerms(r, "field");
- assertEquals(minValue, NumericUtils.getMinInt(terms));
- assertEquals(maxValue, NumericUtils.getMaxInt(terms));
+ assertEquals(new Integer(minValue), NumericUtils.getMinInt(terms));
+ assertEquals(new Integer(maxValue), NumericUtils.getMaxInt(terms));
r.close();
w.close();
dir.close();
}
+ public void testEmptyLongFieldMinMax() throws Exception {
+ assertNull(NumericUtils.getMinLong(EMPTY_TERMS));
+ assertNull(NumericUtils.getMaxLong(EMPTY_TERMS));
+ }
+
public void testLongFieldMinMax() throws Exception {
Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir);
@@ -134,8 +144,8 @@ public class TestTerms extends LuceneTestCase {
IndexReader r = w.getReader();
Terms terms = MultiFields.getTerms(r, "field");
- assertEquals(minValue, NumericUtils.getMinLong(terms));
- assertEquals(maxValue, NumericUtils.getMaxLong(terms));
+ assertEquals(new Long(minValue), NumericUtils.getMinLong(terms));
+ assertEquals(new Long(maxValue), NumericUtils.getMaxLong(terms));
r.close();
w.close();
@@ -193,4 +203,19 @@ public class TestTerms extends LuceneTestCase {
w.close();
dir.close();
}
+
+ /**
+ * A complete empty Terms instance that has no terms in it and supports no optional statistics
+ */
+ private static Terms EMPTY_TERMS = new Terms() {
+ public TermsEnum iterator() { return TermsEnum.EMPTY; }
+ public long size() { return -1; }
+ public long getSumTotalTermFreq() { return -1; }
+ public long getSumDocFreq() { return -1; }
+ public int getDocCount() { return -1; }
+ public boolean hasFreqs() { return false; }
+ public boolean hasOffsets() { return false; }
+ public boolean hasPositions() { return false; }
+ public boolean hasPayloads() { return false; }
+ };
}