Apache Solr open-source search software
LUCENE-7133: check field name in equals/hashCode for point queries
Mike McCandless97b62703379e
diff --git a/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java b/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.javaindex bee864f8966..3314e29d49f 100644--- a/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java@@ -304,6 +304,7 @@ public abstract class PointInSetQuery extends Query {@Overridepublic final int hashCode() {int hash = super.hashCode();+ hash = 31 * hash + field.hashCode();hash = 31 * hash + sortedPackedPointsHashCode;hash = 31 * hash + numDims;hash = 31 * hash + bytesPerDim;@@ -314,7 +315,8 @@ public abstract class PointInSetQuery extends Query {public final boolean equals(Object other) {if (super.equals(other)) {final PointInSetQuery q = (PointInSetQuery) other;- return q.numDims == numDims &&+ return q.field.equals(field) &&+ q.numDims == numDims &&q.bytesPerDim == bytesPerDim &&q.sortedPackedPointsHashCode == sortedPackedPointsHashCode &&q.sortedPackedPoints.equals(sortedPackedPoints);diff --git a/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java b/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.javaindex cfa644fea9b..eb183e25840 100644--- a/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java@@ -219,6 +219,7 @@ public abstract class PointRangeQuery extends Query {@Overridepublic final int hashCode() {int hash = super.hashCode();+ hash = 31 * hash + field.hashCode();hash = 31 * hash + Arrays.hashCode(lowerPoint);hash = 31 * hash + Arrays.hashCode(upperPoint);hash = 31 * hash + numDims;@@ -233,6 +234,10 @@ public abstract class PointRangeQuery extends Query {}final PointRangeQuery q = (PointRangeQuery) other;+ if (field.equals(q.field) == false) {+ return false;+ }+if (q.numDims != numDims) {return false;}@@ -241,11 +246,11 @@ public abstract class PointRangeQuery extends Query {return false;}- if (!Arrays.equals(lowerPoint, q.lowerPoint)) {+ if (Arrays.equals(lowerPoint, q.lowerPoint) == false) {return false;}- if (!Arrays.equals(upperPoint, q.upperPoint)) {+ if (Arrays.equals(upperPoint, q.upperPoint) == false) {return false;}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java b/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.javaindex b4a586d6b37..c2136c4d663 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java@@ -1896,11 +1896,14 @@ public class TestPointQueries extends LuceneTestCase {}public void testPointRangeEquals() {- Query q1 = IntPoint.newRangeQuery("a", 0, 1000);- Query q2 = IntPoint.newRangeQuery("a", 0, 1000);+ Query q1, q2;++ q1 = IntPoint.newRangeQuery("a", 0, 1000);+ q2 = IntPoint.newRangeQuery("a", 0, 1000);assertEquals(q1, q2);assertEquals(q1.hashCode(), q2.hashCode());assertFalse(q1.equals(IntPoint.newRangeQuery("a", 1, 1000)));+ assertFalse(q1.equals(IntPoint.newRangeQuery("b", 0, 1000)));q1 = LongPoint.newRangeQuery("a", 0, 1000);q2 = LongPoint.newRangeQuery("a", 0, 1000);@@ -1933,11 +1936,14 @@ public class TestPointQueries extends LuceneTestCase {}public void testPointExactEquals() {- Query q1 = IntPoint.newExactQuery("a", 1000);- Query q2 = IntPoint.newExactQuery("a", 1000);+ Query q1, q2;++ q1 = IntPoint.newExactQuery("a", 1000);+ q2 = IntPoint.newExactQuery("a", 1000);assertEquals(q1, q2);assertEquals(q1.hashCode(), q2.hashCode());assertFalse(q1.equals(IntPoint.newExactQuery("a", 1)));+ assertFalse(q1.equals(IntPoint.newExactQuery("b", 1000)));q1 = LongPoint.newExactQuery("a", 1000);q2 = LongPoint.newExactQuery("a", 1000);@@ -1969,11 +1975,13 @@ public class TestPointQueries extends LuceneTestCase {}public void testPointInSetEquals() {- Query q1 = IntPoint.newSetQuery("a", 0, 1000, 17);- Query q2 = IntPoint.newSetQuery("a", 17, 0, 1000);+ Query q1, q2;+ q1 = IntPoint.newSetQuery("a", 0, 1000, 17);+ q2 = IntPoint.newSetQuery("a", 17, 0, 1000);assertEquals(q1, q2);assertEquals(q1.hashCode(), q2.hashCode());assertFalse(q1.equals(IntPoint.newSetQuery("a", 1, 17, 1000)));+ assertFalse(q1.equals(IntPoint.newSetQuery("b", 0, 1000, 17)));q1 = LongPoint.newSetQuery("a", 0, 1000, 17);q2 = LongPoint.newSetQuery("a", 17, 0, 1000);diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestPointQueryParser.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestPointQueryParser.javaindex 323b0ffba9f..a301de0a7f1 100644--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestPointQueryParser.java+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestPointQueryParser.java@@ -75,7 +75,7 @@ public class TestPointQueryParser extends LuceneTestCase {assertEquals(DoublePoint.newRangeQuery("doubleField", 1.5D, 3.6D),parser.parse("doubleField:[1.5 TO 3.6]", "body"));- assertEquals(DoublePoint.newRangeQuery("floatField", 1.5D, 1.5D),+ assertEquals(DoublePoint.newRangeQuery("doubleField", 1.5D, 1.5D),parser.parse("doubleField:1.5", "body"));}diff --git a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointDistanceQuery.java b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointDistanceQuery.javaindex bba92b3e854..10d11c6f27b 100644--- a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointDistanceQuery.java+++ b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointDistanceQuery.java@@ -295,7 +295,7 @@ final class LatLonPointDistanceQuery extends Query {if (!super.equals(obj)) return false;if (getClass() != obj.getClass()) return false;LatLonPointDistanceQuery other = (LatLonPointDistanceQuery) obj;- if (!field.equals(other.field)) return false;+ if (field.equals(other.field) == false) return false;if (Double.doubleToLongBits(latitude) != Double.doubleToLongBits(other.latitude)) return false;if (Double.doubleToLongBits(longitude) != Double.doubleToLongBits(other.longitude)) return false;if (Double.doubleToLongBits(radiusMeters) != Double.doubleToLongBits(other.radiusMeters)) return false;diff --git a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointInPolygonQuery.java b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointInPolygonQuery.javaindex 68c734e9a49..494f42733c9 100644--- a/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointInPolygonQuery.java+++ b/lucene/sandbox/src/java/org/apache/lucene/document/LatLonPointInPolygonQuery.java@@ -258,6 +258,9 @@ final class LatLonPointInPolygonQuery extends Query {LatLonPointInPolygonQuery that = (LatLonPointInPolygonQuery) o;+ if (field.equals(that.field) == false) {+ return false;+ }if (Arrays.equals(polyLons, that.polyLons) == false) {return false;}@@ -269,8 +272,9 @@ final class LatLonPointInPolygonQuery extends Query {}@Override- public final int hashCode() {+ public int hashCode() {int result = super.hashCode();+ result = 31 * result + field.hashCode();result = 31 * result + Arrays.hashCode(polyLons);result = 31 * result + Arrays.hashCode(polyLats);return result;diff --git a/lucene/sandbox/src/test/org/apache/lucene/document/TestBigIntegerPoint.java b/lucene/sandbox/src/test/org/apache/lucene/document/TestBigIntegerPoint.javaindex 7d9281b77da..98c6a679857 100644--- a/lucene/sandbox/src/test/org/apache/lucene/document/TestBigIntegerPoint.java+++ b/lucene/sandbox/src/test/org/apache/lucene/document/TestBigIntegerPoint.java@@ -96,11 +96,13 @@ public class TestBigIntegerPoint extends LuceneTestCase {}public void testQueryEquals() throws Exception {- Query q1 = BigIntegerPoint.newRangeQuery("a", BigInteger.valueOf(0), BigInteger.valueOf(1000));- Query q2 = BigIntegerPoint.newRangeQuery("a", BigInteger.valueOf(0), BigInteger.valueOf(1000));+ Query q1, q2;+ q1 = BigIntegerPoint.newRangeQuery("a", BigInteger.valueOf(0), BigInteger.valueOf(1000));+ q2 = BigIntegerPoint.newRangeQuery("a", BigInteger.valueOf(0), BigInteger.valueOf(1000));assertEquals(q1, q2);assertEquals(q1.hashCode(), q2.hashCode());assertFalse(q1.equals(BigIntegerPoint.newRangeQuery("a", BigInteger.valueOf(1), BigInteger.valueOf(1000))));+ assertFalse(q1.equals(BigIntegerPoint.newRangeQuery("b", BigInteger.valueOf(0), BigInteger.valueOf(1000))));q1 = BigIntegerPoint.newExactQuery("a", BigInteger.valueOf(1000));q2 = BigIntegerPoint.newExactQuery("a", BigInteger.valueOf(1000));diff --git a/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.java b/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.javaindex ee34ecc5b44..673ee296a48 100644--- a/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.java+++ b/lucene/sandbox/src/test/org/apache/lucene/document/TestInetAddressPoint.java@@ -92,11 +92,13 @@ public class TestInetAddressPoint extends LuceneTestCase {}public void testQueryEquals() throws Exception {- Query q1 = InetAddressPoint.newRangeQuery("a", InetAddress.getByName("1.2.3.3"), InetAddress.getByName("1.2.3.5"));- Query q2 = InetAddressPoint.newRangeQuery("a", InetAddress.getByName("1.2.3.3"), InetAddress.getByName("1.2.3.5"));+ Query q1, q2;+ q1 = InetAddressPoint.newRangeQuery("a", InetAddress.getByName("1.2.3.3"), InetAddress.getByName("1.2.3.5"));+ q2 = InetAddressPoint.newRangeQuery("a", InetAddress.getByName("1.2.3.3"), InetAddress.getByName("1.2.3.5"));assertEquals(q1, q2);assertEquals(q1.hashCode(), q2.hashCode());assertFalse(q1.equals(InetAddressPoint.newRangeQuery("a", InetAddress.getByName("1.2.3.3"), InetAddress.getByName("1.2.3.7"))));+ assertFalse(q1.equals(InetAddressPoint.newRangeQuery("b", InetAddress.getByName("1.2.3.3"), InetAddress.getByName("1.2.3.5"))));q1 = InetAddressPoint.newPrefixQuery("a", InetAddress.getByName("1.2.3.3"), 16);q2 = InetAddressPoint.newPrefixQuery("a", InetAddress.getByName("1.2.3.3"), 16);diff --git a/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonPointQueries.java b/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonPointQueries.javaindex 71ef7efa955..b1367768993 100644--- a/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonPointQueries.java+++ b/lucene/sandbox/src/test/org/apache/lucene/search/TestLatLonPointQueries.java@@ -45,7 +45,7 @@ public class TestLatLonPointQueries extends BaseGeoPointTestCase {@Overrideprotected Query newPolygonQuery(String field, double[] lats, double[] lons) {- return LatLonPoint.newPolygonQuery(FIELD_NAME, lats, lons);+ return LatLonPoint.newPolygonQuery(field, lats, lons);}@Overridediff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/util/BaseGeoPointTestCase.java b/lucene/spatial/src/test/org/apache/lucene/spatial/util/BaseGeoPointTestCase.javaindex 0efe7d6165c..3943a92039f 100644--- a/lucene/spatial/src/test/org/apache/lucene/spatial/util/BaseGeoPointTestCase.java+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/util/BaseGeoPointTestCase.java@@ -971,4 +971,47 @@ public abstract class BaseGeoPointTestCase extends LuceneTestCase {writer.close();dir.close();}++ public void testEquals() throws Exception {+ Query q1, q2;++ GeoRect rect = randomRect(false, true);++ q1 = newRectQuery("field", rect);+ q2 = newRectQuery("field", rect);+ assertEquals(q1, q2);+ assertFalse(q1.equals(newRectQuery("field2", rect)));++ double lat = randomLat(false);+ double lon = randomLon(false);+ q1 = newDistanceQuery("field", lat, lon, 10000.0);+ q2 = newDistanceQuery("field", lat, lon, 10000.0);+ assertEquals(q1, q2);+ assertFalse(q1.equals(newDistanceQuery("field2", lat, lon, 10000.0)));++ q1 = newDistanceRangeQuery("field", lat, lon, 10000.0, 100000.0);+ if (q1 != null) {+ // Not all subclasses can make distance range query!+ q2 = newDistanceRangeQuery("field", lat, lon, 10000.0, 100000.0);+ assertEquals(q1, q2);+ assertFalse(q1.equals(newDistanceRangeQuery("field2", lat, lon, 10000.0, 100000.0)));+ }++ double[] lats = new double[5];+ double[] lons = new double[5];+ lats[0] = rect.minLat;+ lons[0] = rect.minLon;+ lats[1] = rect.maxLat;+ lons[1] = rect.minLon;+ lats[2] = rect.maxLat;+ lons[2] = rect.maxLon;+ lats[3] = rect.minLat;+ lons[3] = rect.maxLon;+ lats[4] = rect.minLat;+ lons[4] = rect.minLon;+ q1 = newPolygonQuery("field", lats, lons);+ q2 = newPolygonQuery("field", lats, lons);+ assertEquals(q1, q2);+ assertFalse(q1.equals(newPolygonQuery("field2", lats, lons)));+ }}diff --git a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/PointInGeo3DShapeQuery.java b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/PointInGeo3DShapeQuery.javaindex c9b5e4e635b..3cc95304a67 100644--- a/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/PointInGeo3DShapeQuery.java+++ b/lucene/spatial3d/src/java/org/apache/lucene/spatial3d/PointInGeo3DShapeQuery.java@@ -187,6 +187,9 @@ final class PointInGeo3DShapeQuery extends Query {if (!super.equals(o)) return false;PointInGeo3DShapeQuery that = (PointInGeo3DShapeQuery) o;+ if (field.equals(that.field) == false) {+ return false;+ }return shape.equals(that.shape);}@@ -194,6 +197,7 @@ final class PointInGeo3DShapeQuery extends Query {@Overridepublic int hashCode() {int result = super.hashCode();+ result = 31 * result + field.hashCode();result = 31 * result + shape.hashCode();return result;}diff --git a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/TestGeo3DPoint.java b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/TestGeo3DPoint.javaindex 3061b763f85..5c0044f4371 100644--- a/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/TestGeo3DPoint.java+++ b/lucene/spatial3d/src/test/org/apache/lucene/spatial3d/TestGeo3DPoint.java@@ -812,6 +812,7 @@ public class TestGeo3DPoint extends LuceneTestCase {GeoShape shape = randomShape(PlanetModel.WGS84);Query q = Geo3DPoint.newShapeQuery("point", shape);assertEquals(q, Geo3DPoint.newShapeQuery("point", shape));+ assertFalse(q.equals(Geo3DPoint.newShapeQuery("point2", shape)));// make a different random shape:GeoShape shape2;