LUCENE-7133: check field name in equals/hashCode for point queries

This commit is contained in:
Mike McCandless 2016-03-23 17:12:14 -04:00
parent dbee659174
commit 97b6270337
12 changed files with 88 additions and 17 deletions

View File

@ -304,6 +304,7 @@ public abstract class PointInSetQuery extends Query {
@Override @Override
public final int hashCode() { public final int hashCode() {
int hash = super.hashCode(); int hash = super.hashCode();
hash = 31 * hash + field.hashCode();
hash = 31 * hash + sortedPackedPointsHashCode; hash = 31 * hash + sortedPackedPointsHashCode;
hash = 31 * hash + numDims; hash = 31 * hash + numDims;
hash = 31 * hash + bytesPerDim; hash = 31 * hash + bytesPerDim;
@ -314,7 +315,8 @@ public abstract class PointInSetQuery extends Query {
public final boolean equals(Object other) { public final boolean equals(Object other) {
if (super.equals(other)) { if (super.equals(other)) {
final PointInSetQuery q = (PointInSetQuery) other; final PointInSetQuery q = (PointInSetQuery) other;
return q.numDims == numDims && return q.field.equals(field) &&
q.numDims == numDims &&
q.bytesPerDim == bytesPerDim && q.bytesPerDim == bytesPerDim &&
q.sortedPackedPointsHashCode == sortedPackedPointsHashCode && q.sortedPackedPointsHashCode == sortedPackedPointsHashCode &&
q.sortedPackedPoints.equals(sortedPackedPoints); q.sortedPackedPoints.equals(sortedPackedPoints);

View File

@ -219,6 +219,7 @@ public abstract class PointRangeQuery extends Query {
@Override @Override
public final int hashCode() { public final int hashCode() {
int hash = super.hashCode(); int hash = super.hashCode();
hash = 31 * hash + field.hashCode();
hash = 31 * hash + Arrays.hashCode(lowerPoint); hash = 31 * hash + Arrays.hashCode(lowerPoint);
hash = 31 * hash + Arrays.hashCode(upperPoint); hash = 31 * hash + Arrays.hashCode(upperPoint);
hash = 31 * hash + numDims; hash = 31 * hash + numDims;
@ -233,6 +234,10 @@ public abstract class PointRangeQuery extends Query {
} }
final PointRangeQuery q = (PointRangeQuery) other; final PointRangeQuery q = (PointRangeQuery) other;
if (field.equals(q.field) == false) {
return false;
}
if (q.numDims != numDims) { if (q.numDims != numDims) {
return false; return false;
} }
@ -241,11 +246,11 @@ public abstract class PointRangeQuery extends Query {
return false; return false;
} }
if (!Arrays.equals(lowerPoint, q.lowerPoint)) { if (Arrays.equals(lowerPoint, q.lowerPoint) == false) {
return false; return false;
} }
if (!Arrays.equals(upperPoint, q.upperPoint)) { if (Arrays.equals(upperPoint, q.upperPoint) == false) {
return false; return false;
} }

View File

@ -1896,11 +1896,14 @@ public class TestPointQueries extends LuceneTestCase {
} }
public void testPointRangeEquals() { public void testPointRangeEquals() {
Query q1 = IntPoint.newRangeQuery("a", 0, 1000); Query q1, q2;
Query q2 = IntPoint.newRangeQuery("a", 0, 1000);
q1 = IntPoint.newRangeQuery("a", 0, 1000);
q2 = IntPoint.newRangeQuery("a", 0, 1000);
assertEquals(q1, q2); assertEquals(q1, q2);
assertEquals(q1.hashCode(), q2.hashCode()); assertEquals(q1.hashCode(), q2.hashCode());
assertFalse(q1.equals(IntPoint.newRangeQuery("a", 1, 1000))); assertFalse(q1.equals(IntPoint.newRangeQuery("a", 1, 1000)));
assertFalse(q1.equals(IntPoint.newRangeQuery("b", 0, 1000)));
q1 = LongPoint.newRangeQuery("a", 0, 1000); q1 = LongPoint.newRangeQuery("a", 0, 1000);
q2 = LongPoint.newRangeQuery("a", 0, 1000); q2 = LongPoint.newRangeQuery("a", 0, 1000);
@ -1933,11 +1936,14 @@ public class TestPointQueries extends LuceneTestCase {
} }
public void testPointExactEquals() { public void testPointExactEquals() {
Query q1 = IntPoint.newExactQuery("a", 1000); Query q1, q2;
Query q2 = IntPoint.newExactQuery("a", 1000);
q1 = IntPoint.newExactQuery("a", 1000);
q2 = IntPoint.newExactQuery("a", 1000);
assertEquals(q1, q2); assertEquals(q1, q2);
assertEquals(q1.hashCode(), q2.hashCode()); assertEquals(q1.hashCode(), q2.hashCode());
assertFalse(q1.equals(IntPoint.newExactQuery("a", 1))); assertFalse(q1.equals(IntPoint.newExactQuery("a", 1)));
assertFalse(q1.equals(IntPoint.newExactQuery("b", 1000)));
q1 = LongPoint.newExactQuery("a", 1000); q1 = LongPoint.newExactQuery("a", 1000);
q2 = LongPoint.newExactQuery("a", 1000); q2 = LongPoint.newExactQuery("a", 1000);
@ -1969,11 +1975,13 @@ public class TestPointQueries extends LuceneTestCase {
} }
public void testPointInSetEquals() { public void testPointInSetEquals() {
Query q1 = IntPoint.newSetQuery("a", 0, 1000, 17); Query q1, q2;
Query q2 = IntPoint.newSetQuery("a", 17, 0, 1000); q1 = IntPoint.newSetQuery("a", 0, 1000, 17);
q2 = IntPoint.newSetQuery("a", 17, 0, 1000);
assertEquals(q1, q2); assertEquals(q1, q2);
assertEquals(q1.hashCode(), q2.hashCode()); assertEquals(q1.hashCode(), q2.hashCode());
assertFalse(q1.equals(IntPoint.newSetQuery("a", 1, 17, 1000))); 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); q1 = LongPoint.newSetQuery("a", 0, 1000, 17);
q2 = LongPoint.newSetQuery("a", 17, 0, 1000); q2 = LongPoint.newSetQuery("a", 17, 0, 1000);

View File

@ -75,7 +75,7 @@ public class TestPointQueryParser extends LuceneTestCase {
assertEquals(DoublePoint.newRangeQuery("doubleField", 1.5D, 3.6D), assertEquals(DoublePoint.newRangeQuery("doubleField", 1.5D, 3.6D),
parser.parse("doubleField:[1.5 TO 3.6]", "body")); 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")); parser.parse("doubleField:1.5", "body"));
} }

View File

@ -295,7 +295,7 @@ final class LatLonPointDistanceQuery extends Query {
if (!super.equals(obj)) return false; if (!super.equals(obj)) return false;
if (getClass() != obj.getClass()) return false; if (getClass() != obj.getClass()) return false;
LatLonPointDistanceQuery other = (LatLonPointDistanceQuery) obj; 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(latitude) != Double.doubleToLongBits(other.latitude)) return false;
if (Double.doubleToLongBits(longitude) != Double.doubleToLongBits(other.longitude)) return false; if (Double.doubleToLongBits(longitude) != Double.doubleToLongBits(other.longitude)) return false;
if (Double.doubleToLongBits(radiusMeters) != Double.doubleToLongBits(other.radiusMeters)) return false; if (Double.doubleToLongBits(radiusMeters) != Double.doubleToLongBits(other.radiusMeters)) return false;

View File

@ -258,6 +258,9 @@ final class LatLonPointInPolygonQuery extends Query {
LatLonPointInPolygonQuery that = (LatLonPointInPolygonQuery) o; LatLonPointInPolygonQuery that = (LatLonPointInPolygonQuery) o;
if (field.equals(that.field) == false) {
return false;
}
if (Arrays.equals(polyLons, that.polyLons) == false) { if (Arrays.equals(polyLons, that.polyLons) == false) {
return false; return false;
} }
@ -269,8 +272,9 @@ final class LatLonPointInPolygonQuery extends Query {
} }
@Override @Override
public final int hashCode() { public int hashCode() {
int result = super.hashCode(); int result = super.hashCode();
result = 31 * result + field.hashCode();
result = 31 * result + Arrays.hashCode(polyLons); result = 31 * result + Arrays.hashCode(polyLons);
result = 31 * result + Arrays.hashCode(polyLats); result = 31 * result + Arrays.hashCode(polyLats);
return result; return result;

View File

@ -96,11 +96,13 @@ public class TestBigIntegerPoint extends LuceneTestCase {
} }
public void testQueryEquals() throws Exception { public void testQueryEquals() throws Exception {
Query q1 = BigIntegerPoint.newRangeQuery("a", BigInteger.valueOf(0), BigInteger.valueOf(1000)); Query q1, q2;
Query q2 = BigIntegerPoint.newRangeQuery("a", BigInteger.valueOf(0), BigInteger.valueOf(1000)); 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, q2);
assertEquals(q1.hashCode(), q2.hashCode()); assertEquals(q1.hashCode(), q2.hashCode());
assertFalse(q1.equals(BigIntegerPoint.newRangeQuery("a", BigInteger.valueOf(1), BigInteger.valueOf(1000)))); 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)); q1 = BigIntegerPoint.newExactQuery("a", BigInteger.valueOf(1000));
q2 = BigIntegerPoint.newExactQuery("a", BigInteger.valueOf(1000)); q2 = BigIntegerPoint.newExactQuery("a", BigInteger.valueOf(1000));

View File

@ -92,11 +92,13 @@ public class TestInetAddressPoint extends LuceneTestCase {
} }
public void testQueryEquals() throws Exception { public void testQueryEquals() throws Exception {
Query q1 = InetAddressPoint.newRangeQuery("a", InetAddress.getByName("1.2.3.3"), InetAddress.getByName("1.2.3.5")); Query q1, q2;
Query q2 = InetAddressPoint.newRangeQuery("a", InetAddress.getByName("1.2.3.3"), InetAddress.getByName("1.2.3.5")); 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, q2);
assertEquals(q1.hashCode(), q2.hashCode()); 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("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); q1 = InetAddressPoint.newPrefixQuery("a", InetAddress.getByName("1.2.3.3"), 16);
q2 = InetAddressPoint.newPrefixQuery("a", InetAddress.getByName("1.2.3.3"), 16); q2 = InetAddressPoint.newPrefixQuery("a", InetAddress.getByName("1.2.3.3"), 16);

View File

@ -45,7 +45,7 @@ public class TestLatLonPointQueries extends BaseGeoPointTestCase {
@Override @Override
protected Query newPolygonQuery(String field, double[] lats, double[] lons) { protected Query newPolygonQuery(String field, double[] lats, double[] lons) {
return LatLonPoint.newPolygonQuery(FIELD_NAME, lats, lons); return LatLonPoint.newPolygonQuery(field, lats, lons);
} }
@Override @Override

View File

@ -971,4 +971,47 @@ public abstract class BaseGeoPointTestCase extends LuceneTestCase {
writer.close(); writer.close();
dir.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)));
}
} }

View File

@ -187,6 +187,9 @@ final class PointInGeo3DShapeQuery extends Query {
if (!super.equals(o)) return false; if (!super.equals(o)) return false;
PointInGeo3DShapeQuery that = (PointInGeo3DShapeQuery) o; PointInGeo3DShapeQuery that = (PointInGeo3DShapeQuery) o;
if (field.equals(that.field) == false) {
return false;
}
return shape.equals(that.shape); return shape.equals(that.shape);
} }
@ -194,6 +197,7 @@ final class PointInGeo3DShapeQuery extends Query {
@Override @Override
public int hashCode() { public int hashCode() {
int result = super.hashCode(); int result = super.hashCode();
result = 31 * result + field.hashCode();
result = 31 * result + shape.hashCode(); result = 31 * result + shape.hashCode();
return result; return result;
} }

View File

@ -812,6 +812,7 @@ public class TestGeo3DPoint extends LuceneTestCase {
GeoShape shape = randomShape(PlanetModel.WGS84); GeoShape shape = randomShape(PlanetModel.WGS84);
Query q = Geo3DPoint.newShapeQuery("point", shape); Query q = Geo3DPoint.newShapeQuery("point", shape);
assertEquals(q, Geo3DPoint.newShapeQuery("point", shape)); assertEquals(q, Geo3DPoint.newShapeQuery("point", shape));
assertFalse(q.equals(Geo3DPoint.newShapeQuery("point2", shape)));
// make a different random shape: // make a different random shape:
GeoShape shape2; GeoShape shape2;