Skip to content

Commit eecac06

Browse files
authored
ESQL: Use Point geometry in tests (#104120)
Replace SpatialPoint abstraction with Point geometries.
1 parent 8500d33 commit eecac06

File tree

13 files changed

+64
-161
lines changed

13 files changed

+64
-161
lines changed

server/src/test/java/org/elasticsearch/common/geo/SpatialPointTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
package org.elasticsearch.common.geo;
1010

11+
import org.apache.lucene.tests.geo.GeoTestUtil;
1112
import org.elasticsearch.test.ESTestCase;
1213

1314
import static org.hamcrest.Matchers.equalTo;
@@ -32,7 +33,7 @@ public void testEqualsAndHashcode() {
3233

3334
public void testCompareTo() {
3435
for (int i = 0; i < 100; i++) {
35-
SpatialPoint point = randomValueOtherThanMany(p -> p.getX() < -170 || p.getX() > 170, ESTestCase::randomGeoPoint);
36+
SpatialPoint point = randomValueOtherThanMany(p -> p.getX() < -170 || p.getX() > 170, SpatialPointTests::randomGeoPoint);
3637
GeoPoint smaller = new GeoPoint(point.getY(), point.getX() - 1);
3738
GeoPoint bigger = new GeoPoint(point.getY(), point.getX() + 1);
3839
TestPoint testSmaller = new TestPoint(smaller);
@@ -58,6 +59,10 @@ private void assertNotEqualsAndHashcode(String message, SpatialPoint a, SpatialP
5859
assertThat("Compare: " + message, a.compareTo(b), not(equalTo(0)));
5960
}
6061

62+
private static GeoPoint randomGeoPoint() {
63+
return new GeoPoint(GeoTestUtil.nextLatitude(), GeoTestUtil.nextLongitude());
64+
}
65+
6166
/**
6267
* This test class used to be trivial, when SpatialPoint was a concrete class.
6368
* If we ever revert back to a concrete class, we can simplify this test class.

test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@
5151
import org.elasticsearch.common.bytes.BytesArray;
5252
import org.elasticsearch.common.bytes.BytesReference;
5353
import org.elasticsearch.common.bytes.CompositeBytesReference;
54-
import org.elasticsearch.common.geo.GeoPoint;
55-
import org.elasticsearch.common.geo.SpatialPoint;
5654
import org.elasticsearch.common.io.stream.BytesStreamOutput;
5755
import org.elasticsearch.common.io.stream.NamedWriteable;
5856
import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
@@ -1194,34 +1192,6 @@ public static String randomDateFormatterPattern() {
11941192
return randomFrom(FormatNames.values()).getName();
11951193
}
11961194

1197-
/**
1198-
* Generate a random valid point constrained to geographic ranges (lat, lon ranges).
1199-
*/
1200-
public static SpatialPoint randomGeoPoint() {
1201-
double lat = randomDoubleBetween(-90, 90, true);
1202-
double lon = randomDoubleBetween(-180, 180, true);
1203-
return new GeoPoint(lat, lon);
1204-
}
1205-
1206-
/**
1207-
* Generate a random valid point constrained to cartesian ranges.
1208-
*/
1209-
public static SpatialPoint randomCartesianPoint() {
1210-
double x = randomDoubleBetween(-Float.MAX_VALUE, Float.MAX_VALUE, true);
1211-
double y = randomDoubleBetween(-Float.MAX_VALUE, Float.MAX_VALUE, true);
1212-
return new SpatialPoint() {
1213-
@Override
1214-
public double getX() {
1215-
return x;
1216-
}
1217-
1218-
@Override
1219-
public double getY() {
1220-
return y;
1221-
}
1222-
};
1223-
}
1224-
12251195
/**
12261196
* helper to randomly perform on <code>consumer</code> with <code>value</code>
12271197
*/

x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/data/BasicBlockTests.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import org.apache.lucene.util.BytesRef;
1111
import org.elasticsearch.common.breaker.CircuitBreaker;
12-
import org.elasticsearch.common.geo.SpatialPoint;
1312
import org.elasticsearch.common.unit.ByteSizeValue;
1413
import org.elasticsearch.common.util.BigArrays;
1514
import org.elasticsearch.common.util.BitArray;
@@ -22,6 +21,9 @@
2221
import org.elasticsearch.core.RefCounted;
2322
import org.elasticsearch.core.Releasable;
2423
import org.elasticsearch.core.Releasables;
24+
import org.elasticsearch.geo.GeometryTestUtils;
25+
import org.elasticsearch.geo.ShapeTestUtils;
26+
import org.elasticsearch.geometry.Point;
2527
import org.elasticsearch.indices.breaker.CircuitBreakerService;
2628
import org.elasticsearch.test.ESTestCase;
2729
import org.junit.After;
@@ -445,11 +447,11 @@ public void testBytesRefBlock() {
445447
}
446448

447449
public void testBytesRefBlockOnGeoPoints() {
448-
testBytesRefBlock(() -> GEO.pointAsWKB(randomGeoPoint()), false, GEO::wkbAsString);
450+
testBytesRefBlock(() -> GEO.pointAsWKB(GeometryTestUtils.randomPoint()), false, GEO::wkbAsString);
449451
}
450452

451453
public void testBytesRefBlockOnCartesianPoints() {
452-
testBytesRefBlock(() -> CARTESIAN.pointAsWKB(randomCartesianPoint()), false, CARTESIAN::wkbAsString);
454+
testBytesRefBlock(() -> CARTESIAN.pointAsWKB(ShapeTestUtils.randomPoint()), false, CARTESIAN::wkbAsString);
453455
}
454456

455457
public void testBytesRefBlockBuilderWithNulls() {
@@ -895,7 +897,7 @@ public static RandomBlock randomBlock(
895897
List<List<Object>> values = new ArrayList<>();
896898
try (var builder = elementType.newBlockBuilder(positionCount, blockFactory)) {
897899
boolean bytesRefFromPoints = randomBoolean();
898-
Supplier<SpatialPoint> pointSupplier = randomBoolean() ? ESTestCase::randomGeoPoint : ESTestCase::randomCartesianPoint;
900+
Supplier<Point> pointSupplier = randomBoolean() ? GeometryTestUtils::randomPoint : ShapeTestUtils::randomPoint;
899901
for (int p = 0; p < positionCount; p++) {
900902
int valueCount = between(minValuesPerPosition, maxValuesPerPosition);
901903
if (valueCount == 0 || nullAllowed && randomBoolean()) {

x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/operator/topn/TopNEncoderTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
import org.apache.lucene.document.InetAddressPoint;
1313
import org.apache.lucene.util.BytesRef;
14-
import org.elasticsearch.common.geo.SpatialPoint;
1514
import org.elasticsearch.compute.operator.BreakingBytesRefBuilder;
15+
import org.elasticsearch.geo.GeometryTestUtils;
16+
import org.elasticsearch.geo.ShapeTestUtils;
1617
import org.elasticsearch.geometry.Point;
1718
import org.elasticsearch.geometry.utils.WellKnownBinary;
1819
import org.elasticsearch.test.ESTestCase;
@@ -139,8 +140,8 @@ static Version randomVersion() {
139140
}
140141

141142
static BytesRef randomPointAsWKB() {
142-
SpatialPoint point = randomBoolean() ? randomGeoPoint() : randomCartesianPoint();
143-
byte[] wkb = WellKnownBinary.toWKB(new Point(point.getX(), point.getY()), ByteOrder.LITTLE_ENDIAN);
143+
Point point = randomBoolean() ? GeometryTestUtils.randomPoint() : ShapeTestUtils.randomPoint();
144+
byte[] wkb = WellKnownBinary.toWKB(point, ByteOrder.LITTLE_ENDIAN);
144145
return new BytesRef(wkb);
145146
}
146147
}

x-pack/plugin/esql/qa/server/src/main/java/org/elasticsearch/xpack/esql/qa/rest/EsqlSpecTestCase.java

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.elasticsearch.Version;
1313
import org.elasticsearch.client.Request;
1414
import org.elasticsearch.client.ResponseException;
15-
import org.elasticsearch.common.geo.SpatialPoint;
1615
import org.elasticsearch.common.xcontent.XContentHelper;
1716
import org.elasticsearch.logging.LogManager;
1817
import org.elasticsearch.logging.Logger;
@@ -162,31 +161,7 @@ protected void assertResults(
162161
Logger logger
163162
) {
164163
assertMetadata(expected, actualColumns, logger);
165-
assertData(expected, actualValues, testCase.ignoreOrder, logger, EsqlSpecTestCase::valueToString);
166-
}
167-
168-
/**
169-
* Unfortunately the GeoPoint.toString method returns the old format, but cannot be changed due to BWC.
170-
* So we need to custom format GeoPoint as well as wrap Lists to ensure this custom conversion applies to multi-value fields
171-
*/
172-
private static String valueToString(Object value) {
173-
if (value == null) {
174-
return "null";
175-
} else if (value instanceof List<?> list) {
176-
StringBuilder sb = new StringBuilder("[");
177-
for (Object field : list) {
178-
if (sb.length() > 1) {
179-
sb.append(", ");
180-
}
181-
sb.append(valueToString(field));
182-
}
183-
return sb.append("]").toString();
184-
} else if (value instanceof SpatialPoint point) {
185-
// Alternatively we could just change GeoPoint.toString() to use WKT, but that has other side-effects
186-
return point.toWKT();
187-
} else {
188-
return value.toString();
189-
}
164+
assertData(expected, actualValues, testCase.ignoreOrder, logger, value -> value == null ? "null" : value.toString());
190165
}
191166

192167
private Throwable reworkException(Throwable th) {

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/action/EsqlQueryResponseTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import org.elasticsearch.compute.operator.DriverStatus;
3434
import org.elasticsearch.core.Nullable;
3535
import org.elasticsearch.core.Releasables;
36+
import org.elasticsearch.geo.GeometryTestUtils;
37+
import org.elasticsearch.geo.ShapeTestUtils;
3638
import org.elasticsearch.test.AbstractChunkedSerializingTestCase;
3739
import org.elasticsearch.xcontent.InstantiatingObjectParser;
3840
import org.elasticsearch.xcontent.ObjectParser;
@@ -148,8 +150,10 @@ private Page randomPage(List<ColumnInfo> columns) {
148150
new BytesRef(UnsupportedValueSource.UNSUPPORTED_OUTPUT)
149151
);
150152
case "version" -> ((BytesRefBlock.Builder) builder).appendBytesRef(new Version(randomIdentifier()).toBytesRef());
151-
case "geo_point" -> ((BytesRefBlock.Builder) builder).appendBytesRef(GEO.pointAsWKB(randomGeoPoint()));
152-
case "cartesian_point" -> ((BytesRefBlock.Builder) builder).appendBytesRef(CARTESIAN.pointAsWKB(randomCartesianPoint()));
153+
case "geo_point" -> ((BytesRefBlock.Builder) builder).appendBytesRef(GEO.pointAsWKB(GeometryTestUtils.randomPoint()));
154+
case "cartesian_point" -> ((BytesRefBlock.Builder) builder).appendBytesRef(
155+
CARTESIAN.pointAsWKB(ShapeTestUtils.randomPoint())
156+
);
153157
case "null" -> builder.appendNull();
154158
case "_source" -> {
155159
try {

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/AbstractFunctionTestCase.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator;
2929
import org.elasticsearch.core.PathUtils;
3030
import org.elasticsearch.core.Releasables;
31+
import org.elasticsearch.geo.GeometryTestUtils;
32+
import org.elasticsearch.geo.ShapeTestUtils;
3133
import org.elasticsearch.indices.CrankyCircuitBreakerService;
3234
import org.elasticsearch.logging.LogManager;
3335
import org.elasticsearch.test.ESTestCase;
@@ -125,8 +127,8 @@ public static Literal randomLiteral(DataType type) {
125127
case "time_duration" -> Duration.ofMillis(randomLongBetween(-604800000L, 604800000L)); // plus/minus 7 days
126128
case "text" -> new BytesRef(randomAlphaOfLength(50));
127129
case "version" -> randomVersion().toBytesRef();
128-
case "geo_point" -> GEO.pointAsWKB(randomGeoPoint());
129-
case "cartesian_point" -> CARTESIAN.pointAsWKB(randomCartesianPoint());
130+
case "geo_point" -> GEO.pointAsWKB(GeometryTestUtils.randomPoint());
131+
case "cartesian_point" -> CARTESIAN.pointAsWKB(ShapeTestUtils.randomPoint());
130132
case "null" -> null;
131133
case "_source" -> {
132134
try {

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/TestCaseSupplier.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import org.apache.lucene.document.InetAddressPoint;
1111
import org.apache.lucene.util.BytesRef;
1212
import org.elasticsearch.common.network.InetAddresses;
13+
import org.elasticsearch.geo.GeometryTestUtils;
14+
import org.elasticsearch.geo.ShapeTestUtils;
1315
import org.elasticsearch.logging.LogManager;
1416
import org.elasticsearch.logging.Logger;
1517
import org.elasticsearch.test.ESTestCase;
@@ -41,8 +43,6 @@
4143
import java.util.function.UnaryOperator;
4244
import java.util.stream.Collectors;
4345

44-
import static org.elasticsearch.test.ESTestCase.randomCartesianPoint;
45-
import static org.elasticsearch.test.ESTestCase.randomGeoPoint;
4646
import static org.elasticsearch.xpack.ql.util.SpatialCoordinateTypes.CARTESIAN;
4747
import static org.elasticsearch.xpack.ql.util.SpatialCoordinateTypes.GEO;
4848
import static org.hamcrest.Matchers.equalTo;
@@ -913,12 +913,18 @@ public static List<TypedDataSupplier> timeDurationCases() {
913913
}
914914

915915
private static List<TypedDataSupplier> geoPointCases() {
916-
return List.of(new TypedDataSupplier("<geo_point>", () -> GEO.pointAsWKB(randomGeoPoint()), EsqlDataTypes.GEO_POINT));
916+
return List.of(
917+
new TypedDataSupplier("<geo_point>", () -> GEO.pointAsWKB(GeometryTestUtils.randomPoint()), EsqlDataTypes.GEO_POINT)
918+
);
917919
}
918920

919921
private static List<TypedDataSupplier> cartesianPointCases() {
920922
return List.of(
921-
new TypedDataSupplier("<cartesian_point>", () -> CARTESIAN.pointAsWKB(randomCartesianPoint()), EsqlDataTypes.CARTESIAN_POINT)
923+
new TypedDataSupplier(
924+
"<cartesian_point>",
925+
() -> CARTESIAN.pointAsWKB(ShapeTestUtils.randomPoint()),
926+
EsqlDataTypes.CARTESIAN_POINT
927+
)
922928
);
923929
}
924930

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToCartesianPointTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
1212

1313
import org.apache.lucene.util.BytesRef;
14+
import org.elasticsearch.geo.ShapeTestUtils;
1415
import org.elasticsearch.xpack.esql.expression.function.AbstractFunctionTestCase;
1516
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
1617
import org.elasticsearch.xpack.esql.type.EsqlDataTypes;
@@ -76,7 +77,7 @@ public static Iterable<Object[]> parameters() {
7677
List.of(
7778
new TestCaseSupplier.TypedDataSupplier(
7879
"<cartesian point as string>",
79-
() -> new BytesRef(CARTESIAN.pointAsString(randomCartesianPoint())),
80+
() -> new BytesRef(CARTESIAN.pointAsString(ShapeTestUtils.randomPoint())),
8081
DataTypes.KEYWORD
8182
)
8283
),

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/convert/ToGeoPointTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
1212

1313
import org.apache.lucene.util.BytesRef;
14+
import org.elasticsearch.geo.GeometryTestUtils;
1415
import org.elasticsearch.xpack.esql.expression.function.AbstractFunctionTestCase;
1516
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
1617
import org.elasticsearch.xpack.esql.type.EsqlDataTypes;
@@ -68,7 +69,7 @@ public static Iterable<Object[]> parameters() {
6869
List.of(
6970
new TestCaseSupplier.TypedDataSupplier(
7071
"<geo point as string>",
71-
() -> new BytesRef(GEO.pointAsString(randomGeoPoint())),
72+
() -> new BytesRef(GEO.pointAsString(GeometryTestUtils.randomPoint())),
7273
DataTypes.KEYWORD
7374
)
7475
),

0 commit comments

Comments
 (0)