From d820e34a2600a1232c81e444f770786589449d36 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Fri, 26 Jun 2009 15:02:49 +0000 Subject: [PATCH] Fix the special corner case, that an exclusive range starting/ending with MAX_VALUE/MIN_VALUE overflows the bounds. Return a empty TermEnum in this case (which causes an EMPTY_DOCIDSET returned from getDocIdSet) (see LUCENE-1461 discussions) git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@788728 13f79535-47bb-0310-9956-ffa450edef68 --- .../lucene/search/NumericRangeQuery.java | 20 +++++++++++++++---- .../search/TestNumericRangeQuery32.java | 6 ++++++ .../search/TestNumericRangeQuery64.java | 6 ++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/lucene/search/NumericRangeQuery.java b/src/java/org/apache/lucene/search/NumericRangeQuery.java index 8b4958ca332..dc5cc09cb8d 100644 --- a/src/java/org/apache/lucene/search/NumericRangeQuery.java +++ b/src/java/org/apache/lucene/search/NumericRangeQuery.java @@ -290,7 +290,10 @@ public final class NumericRangeQuery extends MultiTermQuery { } else if (min instanceof Double) { minBound = NumericUtils.doubleToSortableLong(min.doubleValue()); } - if (!minInclusive && min != null) minBound++; + if (!minInclusive && min != null) { + if (minBound == Long.MAX_VALUE) break; + minBound++; + } // upper long maxBound = Long.MAX_VALUE; @@ -299,7 +302,10 @@ public final class NumericRangeQuery extends MultiTermQuery { } else if (max instanceof Double) { maxBound = NumericUtils.doubleToSortableLong(max.doubleValue()); } - if (!maxInclusive && max != null) maxBound--; + if (!maxInclusive && max != null) { + if (maxBound == Long.MIN_VALUE) break; + maxBound--; + } NumericUtils.splitLongRange(new NumericUtils.LongRangeBuilder() { //@Override @@ -319,7 +325,10 @@ public final class NumericRangeQuery extends MultiTermQuery { } else if (min instanceof Float) { minBound = NumericUtils.floatToSortableInt(min.floatValue()); } - if (!minInclusive && min != null) minBound++; + if (!minInclusive && min != null) { + if (minBound == Integer.MAX_VALUE) break; + minBound++; + } // upper int maxBound = Integer.MAX_VALUE; @@ -328,7 +337,10 @@ public final class NumericRangeQuery extends MultiTermQuery { } else if (max instanceof Float) { maxBound = NumericUtils.floatToSortableInt(max.floatValue()); } - if (!maxInclusive && max != null) maxBound--; + if (!maxInclusive && max != null) { + if (maxBound == Integer.MIN_VALUE) break; + maxBound--; + } NumericUtils.splitIntRange(new NumericUtils.IntRangeBuilder() { //@Override diff --git a/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java b/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java index ff7e3b91942..a4f05ab0d5c 100644 --- a/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java +++ b/src/test/org/apache/lucene/search/TestNumericRangeQuery32.java @@ -150,6 +150,12 @@ public class TestNumericRangeQuery32 extends LuceneTestCase { public void testInverseRange() throws Exception { NumericRangeFilter f = NumericRangeFilter.newIntRange("field8", 8, new Integer(1000), new Integer(-1000), true, true); assertSame("A inverse range should return the EMPTY_DOCIDSET instance", DocIdSet.EMPTY_DOCIDSET, f.getDocIdSet(searcher.getIndexReader())); + f = NumericRangeFilter.newIntRange("field8", 8, new Integer(Integer.MAX_VALUE), null, false, false); + assertSame("A exclusive range starting with Integer.MAX_VALUE should return the EMPTY_DOCIDSET instance", + DocIdSet.EMPTY_DOCIDSET, f.getDocIdSet(searcher.getIndexReader())); + f = NumericRangeFilter.newIntRange("field8", 8, null, new Integer(Integer.MIN_VALUE), false, false); + assertSame("A exclusive range ending with Integer.MIN_VALUE should return the EMPTY_DOCIDSET instance", + DocIdSet.EMPTY_DOCIDSET, f.getDocIdSet(searcher.getIndexReader())); } private void testLeftOpenRange(int precisionStep) throws Exception { diff --git a/src/test/org/apache/lucene/search/TestNumericRangeQuery64.java b/src/test/org/apache/lucene/search/TestNumericRangeQuery64.java index 9178556a6f6..ffba3edbb9e 100644 --- a/src/test/org/apache/lucene/search/TestNumericRangeQuery64.java +++ b/src/test/org/apache/lucene/search/TestNumericRangeQuery64.java @@ -150,6 +150,12 @@ public class TestNumericRangeQuery64 extends LuceneTestCase { public void testInverseRange() throws Exception { NumericRangeFilter f = NumericRangeFilter.newLongRange("field8", 8, new Long(1000L), new Long(-1000L), true, true); assertSame("A inverse range should return the EMPTY_DOCIDSET instance", DocIdSet.EMPTY_DOCIDSET, f.getDocIdSet(searcher.getIndexReader())); + f = NumericRangeFilter.newLongRange("field8", 8, new Long(Long.MAX_VALUE), null, false, false); + assertSame("A exclusive range starting with Long.MAX_VALUE should return the EMPTY_DOCIDSET instance", + DocIdSet.EMPTY_DOCIDSET, f.getDocIdSet(searcher.getIndexReader())); + f = NumericRangeFilter.newLongRange("field8", 8, null, new Long(Long.MIN_VALUE), false, false); + assertSame("A exclusive range ending with Long.MIN_VALUE should return the EMPTY_DOCIDSET instance", + DocIdSet.EMPTY_DOCIDSET, f.getDocIdSet(searcher.getIndexReader())); } private void testLeftOpenRange(int precisionStep) throws Exception {