Skip to content

Apache Lucene open-source search software

LUCENE-6092: NumberRangePrefixTree toRangeShape normalization bug

David Wayne Smiley29c90c668cba
diff --git a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/NumberRangePrefixTree.java b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/NumberRangePrefixTree.java
index 2d2ded52ff5..bee76cb8e9f 100644
--- a/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/NumberRangePrefixTree.java
+++ b/lucene/spatial/src/java/org/apache/lucene/spatial/prefix/tree/NumberRangePrefixTree.java
@@ -232,20 +232,34 @@ public abstract class NumberRangePrefixTree extends SpatialPrefixTree {
public Shape toRangeShape(Shape start, Shape end) {
if (!(start instanceof LevelledValue && end instanceof LevelledValue))
throw new IllegalArgumentException("Must pass "+LevelledValue.class+" but got "+start.getClass());
- LevelledValue minLV = (LevelledValue) start;
- LevelledValue maxLV = (LevelledValue) end;
- if (minLV.equals(maxLV))
- return minLV;
- //Optimize precision of the range, e.g. April 1st to April 30th is April.
- minLV = minLV.getLVAtLevel(truncateStartVals(minLV, 0));
- maxLV = maxLV.getLVAtLevel(truncateEndVals(maxLV, 0));
- int cmp = comparePrefixLV(minLV, maxLV);
+ LevelledValue startLV = (LevelledValue) start;
+ LevelledValue endLV = (LevelledValue) end;
+ //note: this normalization/optimization process is actually REQUIRED based on assumptions elsewhere.
+ //Normalize start & end
+ startLV = startLV.getLVAtLevel(truncateStartVals(startLV, 0)); // chops off trailing min-vals (zeroes)
+ endLV = endLV.getLVAtLevel(truncateEndVals(endLV, 0)); // chops off trailing max-vals
+ //Optimize to just start or end if it's equivalent, e.g. April to April 1st is April 1st.
+ int cmp = comparePrefixLV(startLV, endLV);
if (cmp > 0) {
throw new IllegalArgumentException("Wrong order: "+start+" TO "+end);
}
- if (cmp == 0 && minLV.getLevel() == maxLV.getLevel())
- return minLV;
- return new NRShape(minLV, maxLV);
+ if (cmp == 0) {//one is a prefix of the other
+ if (startLV.getLevel() == endLV.getLevel()) {
+ //same
+ return startLV;
+ } else if (endLV.getLevel() > startLV.getLevel()) {
+ // e.g. April to April 1st
+ if (truncateStartVals(endLV, startLV.getLevel()) == startLV.getLevel()) {
+ return endLV;
+ }
+ } else {//minLV level > maxLV level
+ // e.g. April 30 to April
+ if (truncateEndVals(startLV, endLV.getLevel()) == endLV.getLevel()) {
+ return startLV;
+ }
+ }
+ }
+ return new NRShape(startLV, endLV);
}
/** From lv.getLevel on up, it returns the first Level seen with val != 0. It doesn't check past endLevel. */
diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/DateNRStrategyTest.java b/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/DateNRStrategyTest.java
index 7b96f517706..0cf05a97283 100644
--- a/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/DateNRStrategyTest.java
+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/DateNRStrategyTest.java
@@ -27,6 +27,7 @@ import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
+import java.text.ParseException;
import java.util.Calendar;
public class DateNRStrategyTest extends RandomSpatialOpStrategyTestCase {
@@ -65,12 +66,6 @@ public class DateNRStrategyTest extends RandomSpatialOpStrategyTestCase {
testOperationRandomShapes(SpatialOperation.Contains);
}
- @Test @Ignore("see LUCENE-5692")
- @Repeat(iterations = ITERATIONS)
- public void testDisjoint() throws IOException {
- testOperationRandomShapes(SpatialOperation.IsDisjointTo);
- }
-
@Test
public void testWithinSame() throws IOException {
final Calendar cal = tree.newCal();
diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/tree/DateRangePrefixTreeTest.java b/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/tree/DateRangePrefixTreeTest.java
index aade583eafd..2faa8e4bc2d 100644
--- a/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/tree/DateRangePrefixTreeTest.java
+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/prefix/tree/DateRangePrefixTreeTest.java
@@ -157,7 +157,9 @@ public class DateRangePrefixTreeTest extends LuceneTestCase {
assertEquals("2014", tree.parseShape("[2014-01-01 TO 2014-12-31]").toString());
- assertEquals("2014", tree.parseShape("[2014-01 TO 2014]").toString());
+ assertEquals("2014", tree.parseShape("[2014-01 TO 2014]").toString());
+ assertEquals("2014-01", tree.parseShape("[2014 TO 2014-01]").toString());
+ assertEquals("2014-12", tree.parseShape("[2014-12 TO 2014]").toString());
assertEquals("[2014 TO 2014-04-06]", tree.parseShape("[2014-01 TO 2014-04-06]").toString());