Skip to content

Commit e610b48

Browse files
committed
Added tests for invalid precision
1 parent 16072c8 commit e610b48

File tree

6 files changed

+52
-2
lines changed

6 files changed

+52
-2
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StGeohash.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public int precision() {
8585
private static int checkPrecisionRange(int precision) {
8686
if (precision < 1 || precision > Geohash.PRECISION) {
8787
throw new IllegalArgumentException(
88-
"Invalid geohash precision of " + precision + ". Must be between 1 and " + Geohash.PRECISION + "."
88+
"Invalid geohash_grid precision of " + precision + ". Must be between 1 and " + Geohash.PRECISION + "."
8989
);
9090
}
9191
return precision;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StGeohex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public int precision() {
8181
private static int checkPrecisionRange(int precision) {
8282
if (precision < 1 || precision > H3.MAX_H3_RES) {
8383
throw new IllegalArgumentException(
84-
"Invalid geohex precision of " + precision + ". Must be between 0 and " + H3.MAX_H3_RES + "."
84+
"Invalid geohex_grid precision of " + precision + ". Must be between 0 and " + H3.MAX_H3_RES + "."
8585
);
8686
}
8787
return precision;

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
package org.elasticsearch.xpack.esql.expression.function.scalar.spatial;
99

1010
import org.apache.lucene.util.BytesRef;
11+
import org.elasticsearch.compute.data.Block;
12+
import org.elasticsearch.compute.operator.EvalOperator;
13+
import org.elasticsearch.xpack.esql.core.expression.Literal;
14+
import org.elasticsearch.xpack.esql.core.tree.Source;
1115
import org.elasticsearch.xpack.esql.core.type.DataType;
1216
import org.elasticsearch.xpack.esql.expression.function.AbstractScalarFunctionTestCase;
1317
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
@@ -18,6 +22,7 @@
1822
import static org.elasticsearch.xpack.esql.core.type.DataType.LONG;
1923
import static org.elasticsearch.xpack.esql.core.type.DataType.isSpatialGeo;
2024
import static org.hamcrest.Matchers.equalTo;
25+
import static org.junit.Assume.assumeNotNull;
2126

2227
public abstract class SpatialGridFunctionTestCase extends AbstractScalarFunctionTestCase {
2328

@@ -87,4 +92,22 @@ public static TestCaseSupplier.TypedDataSupplier testCaseSupplier(DataType dataT
8792
};
8893
}
8994
}
95+
96+
protected Long process(int precision, BiFunction<BytesRef, Integer, Long> expectedValue) {
97+
Object spatialObj = this.testCase.getDataValues().getFirst();
98+
assumeNotNull(spatialObj);
99+
assumeTrue("Expected a BytesRef, but got " + spatialObj.getClass(), spatialObj instanceof BytesRef);
100+
BytesRef wkb = (BytesRef) spatialObj;
101+
try (
102+
EvalOperator.ExpressionEvaluator eval = evaluator(
103+
build(
104+
Source.EMPTY,
105+
List.of(new Literal(Source.EMPTY, wkb, DataType.GEO_POINT), new Literal(Source.EMPTY, precision, DataType.INTEGER))
106+
)
107+
).get(driverContext());
108+
Block block = eval.eval(row(List.of(wkb, precision)))
109+
) {
110+
return block.isNull(0) ? null : expectedValue.apply(wkb, precision);
111+
}
112+
}
90113
}

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

Lines changed: 9 additions & 0 deletions
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.geometry.utils.Geohash;
1415
import org.elasticsearch.xpack.esql.core.expression.Expression;
1516
import org.elasticsearch.xpack.esql.core.tree.Source;
1617
import org.elasticsearch.xpack.esql.core.type.DataType;
@@ -21,6 +22,7 @@
2122
import java.util.function.Supplier;
2223

2324
import static org.elasticsearch.xpack.esql.core.util.SpatialCoordinateTypes.UNSPECIFIED;
25+
import static org.hamcrest.Matchers.containsString;
2426

2527
public class StGeohashTests extends SpatialGridFunctionTestCase {
2628
public StGeohashTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
@@ -43,4 +45,11 @@ protected Expression build(Source source, List<Expression> args) {
4345
Expression bounds = args.size() > 2 ? args.get(2) : null;
4446
return new StGeohash(source, args.get(0), args.get(1), bounds);
4547
}
48+
49+
public void testInvalidPrecision() {
50+
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> process(-1, StGeohashTests::valueOf));
51+
assertThat(ex.getMessage(), containsString("Invalid geohash_grid precision of -1. Must be between 1 and 12."));
52+
ex = expectThrows(IllegalArgumentException.class, () -> process(Geohash.PRECISION + 1, StGeohashTests::valueOf));
53+
assertThat(ex.getMessage(), containsString("Invalid geohash_grid precision of 13. Must be between 1 and 12."));
54+
}
4655
}

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

Lines changed: 9 additions & 0 deletions
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.h3.H3;
1415
import org.elasticsearch.xpack.esql.core.expression.Expression;
1516
import org.elasticsearch.xpack.esql.core.tree.Source;
1617
import org.elasticsearch.xpack.esql.core.type.DataType;
@@ -21,6 +22,7 @@
2122
import java.util.function.Supplier;
2223

2324
import static org.elasticsearch.xpack.esql.core.util.SpatialCoordinateTypes.UNSPECIFIED;
25+
import static org.hamcrest.Matchers.containsString;
2426

2527
public class StGeohexTests extends SpatialGridFunctionTestCase {
2628
public StGeohexTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
@@ -43,4 +45,11 @@ protected Expression build(Source source, List<Expression> args) {
4345
Expression bounds = args.size() > 2 ? args.get(2) : null;
4446
return new StGeohex(source, args.get(0), args.get(1), bounds);
4547
}
48+
49+
public void testInvalidPrecision() {
50+
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> process(-1, StGeohexTests::valueOf));
51+
assertThat(ex.getMessage(), containsString("Invalid geohex_grid precision of -1. Must be between 0 and 15."));
52+
ex = expectThrows(IllegalArgumentException.class, () -> process(H3.MAX_H3_RES + 1, StGeohexTests::valueOf));
53+
assertThat(ex.getMessage(), containsString("Invalid geohex_grid precision of 16. Must be between 0 and 15."));
54+
}
4655
}

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

Lines changed: 9 additions & 0 deletions
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.search.aggregations.bucket.geogrid.GeoTileUtils;
1415
import org.elasticsearch.xpack.esql.core.expression.Expression;
1516
import org.elasticsearch.xpack.esql.core.tree.Source;
1617
import org.elasticsearch.xpack.esql.core.type.DataType;
@@ -21,6 +22,7 @@
2122
import java.util.function.Supplier;
2223

2324
import static org.elasticsearch.xpack.esql.core.util.SpatialCoordinateTypes.UNSPECIFIED;
25+
import static org.hamcrest.Matchers.containsString;
2426

2527
public class StGeotileTests extends SpatialGridFunctionTestCase {
2628
public StGeotileTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
@@ -43,4 +45,11 @@ protected Expression build(Source source, List<Expression> args) {
4345
Expression bounds = args.size() > 2 ? args.get(2) : null;
4446
return new StGeotile(source, args.get(0), args.get(1), bounds);
4547
}
48+
49+
public void testInvalidPrecision() {
50+
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> process(-1, StGeotileTests::valueOf));
51+
assertThat(ex.getMessage(), containsString("Invalid geotile_grid precision of -1. Must be between 0 and 29."));
52+
ex = expectThrows(IllegalArgumentException.class, () -> process(GeoTileUtils.MAX_ZOOM + 1, StGeotileTests::valueOf));
53+
assertThat(ex.getMessage(), containsString("Invalid geotile_grid precision of 30. Must be between 0 and 29."));
54+
}
4655
}

0 commit comments

Comments
 (0)