Apache Lucene open-source search software
LUCENE-8540: Better handling of min/max values for Geo3d encoding
iverase07b93a97f04e
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 9b0382b6444..dc1336d4b98 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -236,6 +236,10 @@ Improvements:first 4 are index dimensions defining the bounding box of the Triangle and theremaining 3 data dimensions define the vertices of the triangle. (Nick Knize)+Bug Fixes:++* LUCENE-8540: Better handling of min/max values for Geo3d encoding. (Ignacio Vera)+Other:* LUCENE-8523: Correct typo in JapaneseNumberFilterFactory javadocs (Ankush Jhalanidiff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/Geo3DUtil.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/Geo3DUtil.javaindex 88537f47943..4e1ecb5dd94 100644--- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/Geo3DUtil.java+++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/Geo3DUtil.java@@ -80,6 +80,10 @@ class Geo3DUtil {/** Returns smallest double that would encode to int x. */// NOTE: keep this package private!!static double decodeValueFloor(int x) {+ assert x <= MAX_ENCODED_VALUE && x >= MIN_ENCODED_VALUE;+ if (x == MIN_ENCODED_VALUE) {+ return -MAX_VALUE;+ }return x * DECODE;}@@ -105,7 +109,10 @@ class Geo3DUtil {/** Returns largest double that would encode to int x. */// NOTE: keep this package private!!static double decodeValueCeil(int x) {- assert x < Integer.MAX_VALUE;+ assert x <= MAX_ENCODED_VALUE && x >= MIN_ENCODED_VALUE;+ if (x == MAX_ENCODED_VALUE) {+ return MAX_VALUE;+ }return Math.nextDown((x+1) * DECODE);}diff --git a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/TestGeo3DPoint.java b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/TestGeo3DPoint.javaindex df67a8dbe04..66fa3cd13a3 100644--- a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/TestGeo3DPoint.java+++ b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/TestGeo3DPoint.java@@ -1204,6 +1204,40 @@ public class TestGeo3DPoint extends LuceneTestCase {}}+ public void testMinValueQuantization(){+ int encoded = Geo3DUtil.MIN_ENCODED_VALUE;+ double minValue= -PlanetModel.WGS84.getMaximumMagnitude();+ //Normal encoding+ double decoded = Geo3DUtil.decodeValue(encoded);+ assertEquals(minValue, decoded, 0d);+ assertEquals(encoded, Geo3DUtil.encodeValue(decoded));+ //Encoding floor+ double decodedFloor = Geo3DUtil.decodeValueFloor(encoded);+ assertEquals(minValue, decodedFloor, 0d);+ assertEquals(encoded, Geo3DUtil.encodeValue(decodedFloor));+ //Encoding ceiling+ double decodedCeiling = Geo3DUtil.decodeValueCeil(encoded);+ assertTrue(decodedCeiling > minValue);+ assertEquals(encoded, Geo3DUtil.encodeValue(decodedCeiling));+ }++ public void testMaxValueQuantization(){+ int encoded = Geo3DUtil.MAX_ENCODED_VALUE;+ double maxValue= PlanetModel.WGS84.getMaximumMagnitude();+ //Normal encoding+ double decoded = Geo3DUtil.decodeValue(encoded);+ assertEquals(maxValue, decoded, 0d);+ assertEquals(encoded, Geo3DUtil.encodeValue(decoded));+ //Encoding floor+ double decodedFloor = Geo3DUtil.decodeValueFloor(encoded);+ assertTrue(decodedFloor < maxValue);+ assertEquals(encoded, Geo3DUtil.encodeValue(decodedFloor));+ //Encoding ceiling+ double decodedCeiling = Geo3DUtil.decodeValueCeil(encoded);+ assertEquals(maxValue, decodedCeiling, 0d);+ assertEquals(encoded, Geo3DUtil.encodeValue(decodedCeiling));+ }+// poached from TestGeoEncodingUtils.testLatitudeQuantization:/**@@ -1215,10 +1249,10 @@ public class TestGeo3DPoint extends LuceneTestCase {Random random = random();for (int i = 0; i < 10000; i++) {int encoded = random.nextInt();- if (encoded < Geo3DUtil.MIN_ENCODED_VALUE) {+ if (encoded <= Geo3DUtil.MIN_ENCODED_VALUE) {continue;}- if (encoded > Geo3DUtil.MAX_ENCODED_VALUE) {+ if (encoded >= Geo3DUtil.MAX_ENCODED_VALUE) {continue;}double min = encoded * Geo3DUtil.DECODE;