diff --git a/test/framework/src/main/java/org/elasticsearch/geo/GeometryPointCountVisitor.java b/test/framework/src/main/java/org/elasticsearch/geo/GeometryPointCountVisitor.java new file mode 100644 index 0000000000000..06b468250bd8f --- /dev/null +++ b/test/framework/src/main/java/org/elasticsearch/geo/GeometryPointCountVisitor.java @@ -0,0 +1,83 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +package org.elasticsearch.geo; + +import org.elasticsearch.geometry.Circle; +import org.elasticsearch.geometry.Geometry; +import org.elasticsearch.geometry.GeometryCollection; +import org.elasticsearch.geometry.GeometryVisitor; +import org.elasticsearch.geometry.Line; +import org.elasticsearch.geometry.LinearRing; +import org.elasticsearch.geometry.MultiLine; +import org.elasticsearch.geometry.MultiPoint; +import org.elasticsearch.geometry.MultiPolygon; +import org.elasticsearch.geometry.Point; +import org.elasticsearch.geometry.Polygon; +import org.elasticsearch.geometry.Rectangle; + +public class GeometryPointCountVisitor implements GeometryVisitor { + + @Override + public Integer visit(Circle circle) throws RuntimeException { + return 2; + } + + @Override + public Integer visit(GeometryCollection collection) throws RuntimeException { + int size = 0; + for (Geometry geometry : collection) { + size += geometry.visit(this); + } + return size; + } + + @Override + public Integer visit(Line line) throws RuntimeException { + return line.length(); + } + + @Override + public Integer visit(LinearRing ring) throws RuntimeException { + return ring.length(); + } + + @Override + public Integer visit(MultiLine multiLine) throws RuntimeException { + return visit((GeometryCollection) multiLine); + } + + @Override + public Integer visit(MultiPoint multiPoint) throws RuntimeException { + return multiPoint.size(); + } + + @Override + public Integer visit(MultiPolygon multiPolygon) throws RuntimeException { + return visit((GeometryCollection) multiPolygon); + } + + @Override + public Integer visit(Point point) throws RuntimeException { + return 1; + } + + @Override + public Integer visit(Polygon polygon) throws RuntimeException { + int size = polygon.getPolygon().length(); + for (int i = 0; i < polygon.getNumberOfHoles(); i++) { + size += polygon.getHole(i).length(); + } + return size; + } + + @Override + public Integer visit(Rectangle rectangle) throws RuntimeException { + return 4; + } +} diff --git a/test/framework/src/main/java/org/elasticsearch/geo/GeometryTestUtils.java b/test/framework/src/main/java/org/elasticsearch/geo/GeometryTestUtils.java index ab15204a6095c..1c017a9da18ee 100644 --- a/test/framework/src/main/java/org/elasticsearch/geo/GeometryTestUtils.java +++ b/test/framework/src/main/java/org/elasticsearch/geo/GeometryTestUtils.java @@ -205,6 +205,11 @@ public static Geometry randomGeometry(boolean hasAlt) { return randomGeometry(0, hasAlt); } + public static Geometry randomGeometry(boolean hasAlt, int maxPoints) { + var pointCounter = new GeometryPointCountVisitor(); + return randomValueOtherThanMany(g -> g.visit(pointCounter) > maxPoints, () -> randomGeometry(0, hasAlt)); + } + protected static Geometry randomGeometry(int level, boolean hasAlt) { @SuppressWarnings("unchecked") Function geometry = ESTestCase.randomFrom( diff --git a/test/framework/src/main/java/org/elasticsearch/geo/ShapeTestUtils.java b/test/framework/src/main/java/org/elasticsearch/geo/ShapeTestUtils.java index 1e21ad1acfd08..4918bd92fdfee 100644 --- a/test/framework/src/main/java/org/elasticsearch/geo/ShapeTestUtils.java +++ b/test/framework/src/main/java/org/elasticsearch/geo/ShapeTestUtils.java @@ -184,6 +184,11 @@ public static Geometry randomGeometry(boolean hasAlt) { return randomGeometry(0, hasAlt); } + public static Geometry randomGeometry(boolean hasAlt, int maxPoints) { + var pointCounter = new GeometryPointCountVisitor(); + return randomValueOtherThanMany(g -> g.visit(pointCounter) > maxPoints, () -> randomGeometry(0, hasAlt)); + } + protected static Geometry randomGeometry(int level, boolean hasAlt) { @SuppressWarnings("unchecked") Function geometry = ESTestCase.randomFrom( diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/multivalue/MvAppendTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/multivalue/MvAppendTests.java index bc1a64da1cc73..f95747618dd28 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/multivalue/MvAppendTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/multivalue/MvAppendTests.java @@ -13,18 +13,6 @@ import org.apache.lucene.util.BytesRef; import org.elasticsearch.geo.GeometryTestUtils; import org.elasticsearch.geo.ShapeTestUtils; -import org.elasticsearch.geometry.Circle; -import org.elasticsearch.geometry.Geometry; -import org.elasticsearch.geometry.GeometryCollection; -import org.elasticsearch.geometry.GeometryVisitor; -import org.elasticsearch.geometry.Line; -import org.elasticsearch.geometry.LinearRing; -import org.elasticsearch.geometry.MultiLine; -import org.elasticsearch.geometry.MultiPoint; -import org.elasticsearch.geometry.MultiPolygon; -import org.elasticsearch.geometry.Point; -import org.elasticsearch.geometry.Polygon; -import org.elasticsearch.geometry.Rectangle; import org.elasticsearch.xpack.esql.core.expression.Expression; import org.elasticsearch.xpack.esql.core.tree.Source; import org.elasticsearch.xpack.esql.core.type.DataType; @@ -247,25 +235,8 @@ private static void bytesRefs(List suppliers) { })); suppliers.add(new TestCaseSupplier(List.of(DataType.GEO_SHAPE, DataType.GEO_SHAPE), () -> { - GeometryPointCountVisitor pointCounter = new GeometryPointCountVisitor(); - List field1 = randomList( - 1, - 3, - () -> new BytesRef( - GEO.asWkt( - randomValueOtherThanMany(g -> g.visit(pointCounter) > 500, () -> GeometryTestUtils.randomGeometry(randomBoolean())) - ) - ) - ); - List field2 = randomList( - 1, - 3, - () -> new BytesRef( - GEO.asWkt( - randomValueOtherThanMany(g -> g.visit(pointCounter) > 500, () -> GeometryTestUtils.randomGeometry(randomBoolean())) - ) - ) - ); + var field1 = randomList(1, 3, () -> new BytesRef(GEO.asWkt(GeometryTestUtils.randomGeometry(randomBoolean(), 500)))); + var field2 = randomList(1, 3, () -> new BytesRef(GEO.asWkt(GeometryTestUtils.randomGeometry(randomBoolean(), 500)))); var result = new ArrayList<>(field1); result.addAll(field2); return new TestCaseSupplier.TestCase( @@ -280,25 +251,8 @@ private static void bytesRefs(List suppliers) { })); suppliers.add(new TestCaseSupplier(List.of(DataType.CARTESIAN_SHAPE, DataType.CARTESIAN_SHAPE), () -> { - GeometryPointCountVisitor pointCounter = new GeometryPointCountVisitor(); - List field1 = randomList( - 1, - 3, - () -> new BytesRef( - GEO.asWkt( - randomValueOtherThanMany(g -> g.visit(pointCounter) > 500, () -> ShapeTestUtils.randomGeometry(randomBoolean())) - ) - ) - ); - List field2 = randomList( - 1, - 3, - () -> new BytesRef( - GEO.asWkt( - randomValueOtherThanMany(g -> g.visit(pointCounter) > 500, () -> ShapeTestUtils.randomGeometry(randomBoolean())) - ) - ) - ); + var field1 = randomList(1, 3, () -> new BytesRef(CARTESIAN.asWkt(ShapeTestUtils.randomGeometry(randomBoolean(), 500)))); + var field2 = randomList(1, 3, () -> new BytesRef(CARTESIAN.asWkt(ShapeTestUtils.randomGeometry(randomBoolean(), 500)))); var result = new ArrayList<>(field1); result.addAll(field2); return new TestCaseSupplier.TestCase( @@ -339,65 +293,4 @@ private static void nulls(List suppliers) { ); })); } - - public static class GeometryPointCountVisitor implements GeometryVisitor { - - @Override - public Integer visit(Circle circle) throws RuntimeException { - return 2; - } - - @Override - public Integer visit(GeometryCollection collection) throws RuntimeException { - int size = 0; - for (Geometry geometry : collection) { - size += geometry.visit(this); - } - return size; - } - - @Override - public Integer visit(Line line) throws RuntimeException { - return line.length(); - } - - @Override - public Integer visit(LinearRing ring) throws RuntimeException { - return ring.length(); - } - - @Override - public Integer visit(MultiLine multiLine) throws RuntimeException { - return visit((GeometryCollection) multiLine); - } - - @Override - public Integer visit(MultiPoint multiPoint) throws RuntimeException { - return multiPoint.size(); - } - - @Override - public Integer visit(MultiPolygon multiPolygon) throws RuntimeException { - return visit((GeometryCollection) multiPolygon); - } - - @Override - public Integer visit(Point point) throws RuntimeException { - return 1; - } - - @Override - public Integer visit(Polygon polygon) throws RuntimeException { - int size = polygon.getPolygon().length(); - for (int i = 0; i < polygon.getNumberOfHoles(); i++) { - size += polygon.getHole(i).length(); - } - return size; - } - - @Override - public Integer visit(Rectangle rectangle) throws RuntimeException { - return 4; - } - } } diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/multivalue/MvSliceTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/multivalue/MvSliceTests.java index 0550be25f9d91..3f6fb841f006f 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/multivalue/MvSliceTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/multivalue/MvSliceTests.java @@ -306,16 +306,7 @@ private static void bytesRefs(List suppliers) { })); suppliers.add(new TestCaseSupplier(List.of(DataType.GEO_SHAPE, DataType.INTEGER, DataType.INTEGER), () -> { - var pointCounter = new MvAppendTests.GeometryPointCountVisitor(); - List field = randomList( - 1, - 5, - () -> new BytesRef( - GEO.asWkt( - randomValueOtherThanMany(g -> g.visit(pointCounter) > 500, () -> GeometryTestUtils.randomGeometry(randomBoolean())) - ) - ) - ); + var field = randomList(1, 5, () -> new BytesRef(GEO.asWkt(GeometryTestUtils.randomGeometry(randomBoolean(), 500)))); int length = field.size(); int start = randomIntBetween(0, length - 1); int end = randomIntBetween(start, length - 1); @@ -332,16 +323,7 @@ private static void bytesRefs(List suppliers) { })); suppliers.add(new TestCaseSupplier(List.of(DataType.CARTESIAN_SHAPE, DataType.INTEGER, DataType.INTEGER), () -> { - var pointCounter = new MvAppendTests.GeometryPointCountVisitor(); - List field = randomList( - 1, - 5, - () -> new BytesRef( - CARTESIAN.asWkt( - randomValueOtherThanMany(g -> g.visit(pointCounter) > 500, () -> GeometryTestUtils.randomGeometry(randomBoolean())) - ) - ) - ); + var field = randomList(1, 5, () -> new BytesRef(CARTESIAN.asWkt(GeometryTestUtils.randomGeometry(randomBoolean(), 500)))); int length = field.size(); int start = randomIntBetween(0, length - 1); int end = randomIntBetween(start, length - 1);