Skip to content

Commit

Permalink
ES|QL: refactor MV tests with geo (elastic#109837)
Browse files Browse the repository at this point in the history
Small refactoring of MV_SLICE and MV_APPEND tests regarding random shape
generation.  Moving the logic to limit the size of random shapes to a
helper class.
  • Loading branch information
luigidellaquila authored Jun 21, 2024
1 parent 47ad822 commit 29172b1
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 131 deletions.
Original file line number Diff line number Diff line change
@@ -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<Integer, RuntimeException> {

@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<Line>) multiLine);
}

@Override
public Integer visit(MultiPoint multiPoint) throws RuntimeException {
return multiPoint.size();
}

@Override
public Integer visit(MultiPolygon multiPolygon) throws RuntimeException {
return visit((GeometryCollection<Polygon>) 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean, Geometry> geometry = ESTestCase.randomFrom(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean, Geometry> geometry = ESTestCase.randomFrom(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -247,25 +235,8 @@ private static void bytesRefs(List<TestCaseSupplier> suppliers) {
}));

suppliers.add(new TestCaseSupplier(List.of(DataType.GEO_SHAPE, DataType.GEO_SHAPE), () -> {
GeometryPointCountVisitor pointCounter = new GeometryPointCountVisitor();
List<Object> field1 = randomList(
1,
3,
() -> new BytesRef(
GEO.asWkt(
randomValueOtherThanMany(g -> g.visit(pointCounter) > 500, () -> GeometryTestUtils.randomGeometry(randomBoolean()))
)
)
);
List<Object> 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(
Expand All @@ -280,25 +251,8 @@ private static void bytesRefs(List<TestCaseSupplier> suppliers) {
}));

suppliers.add(new TestCaseSupplier(List.of(DataType.CARTESIAN_SHAPE, DataType.CARTESIAN_SHAPE), () -> {
GeometryPointCountVisitor pointCounter = new GeometryPointCountVisitor();
List<Object> field1 = randomList(
1,
3,
() -> new BytesRef(
GEO.asWkt(
randomValueOtherThanMany(g -> g.visit(pointCounter) > 500, () -> ShapeTestUtils.randomGeometry(randomBoolean()))
)
)
);
List<Object> 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(
Expand Down Expand Up @@ -339,65 +293,4 @@ private static void nulls(List<TestCaseSupplier> suppliers) {
);
}));
}

public static class GeometryPointCountVisitor implements GeometryVisitor<Integer, RuntimeException> {

@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<Line>) multiLine);
}

@Override
public Integer visit(MultiPoint multiPoint) throws RuntimeException {
return multiPoint.size();
}

@Override
public Integer visit(MultiPolygon multiPolygon) throws RuntimeException {
return visit((GeometryCollection<Polygon>) 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -306,16 +306,7 @@ private static void bytesRefs(List<TestCaseSupplier> suppliers) {
}));

suppliers.add(new TestCaseSupplier(List.of(DataType.GEO_SHAPE, DataType.INTEGER, DataType.INTEGER), () -> {
var pointCounter = new MvAppendTests.GeometryPointCountVisitor();
List<Object> 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);
Expand All @@ -332,16 +323,7 @@ private static void bytesRefs(List<TestCaseSupplier> suppliers) {
}));

suppliers.add(new TestCaseSupplier(List.of(DataType.CARTESIAN_SHAPE, DataType.INTEGER, DataType.INTEGER), () -> {
var pointCounter = new MvAppendTests.GeometryPointCountVisitor();
List<Object> 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);
Expand Down

0 comments on commit 29172b1

Please sign in to comment.