diff --git a/x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/util/SpatialEnvelopeVisitor.java b/x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/util/SpatialEnvelopeVisitor.java index 16fbe8559ba21..dcdb7c5887815 100644 --- a/x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/util/SpatialEnvelopeVisitor.java +++ b/x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/util/SpatialEnvelopeVisitor.java @@ -8,6 +8,7 @@ package org.elasticsearch.xpack.esql.core.util; 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; @@ -19,6 +20,8 @@ import org.elasticsearch.geometry.Polygon; import org.elasticsearch.geometry.Rectangle; +import java.util.Optional; + public class SpatialEnvelopeVisitor implements GeometryVisitor { private double minX = Double.POSITIVE_INFINITY; @@ -26,6 +29,14 @@ public class SpatialEnvelopeVisitor implements GeometryVisitor visit(Geometry geometry) { + var visitor = new SpatialEnvelopeVisitor(); + if (geometry.visit(visitor)) { + return Optional.of(visitor.getResult()); + } + return Optional.empty(); + } + public Rectangle getResult() { return new Rectangle(minX, maxX, maxY, minY); } diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StXMax.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StXMax.java index df1ec064b779f..ca65cd72c3e67 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StXMax.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StXMax.java @@ -97,9 +97,9 @@ static double fromWellKnownBinary(BytesRef wkb) { if (geometry instanceof Point point) { return point.getX(); } - var envelope = new SpatialEnvelopeVisitor(); - if (geometry.visit(envelope)) { - return envelope.getResult().getMaxX(); + var envelope = SpatialEnvelopeVisitor.visit(geometry); + if (envelope.isPresent()) { + return envelope.get().getMaxX(); } throw new IllegalArgumentException("Cannot determine envelope of geometry"); } diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StXMin.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StXMin.java index 5b26af048772b..d4bd2b346916a 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StXMin.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StXMin.java @@ -97,9 +97,9 @@ static double fromWellKnownBinary(BytesRef wkb) { if (geometry instanceof Point point) { return point.getX(); } - var envelope = new SpatialEnvelopeVisitor(); - if (geometry.visit(envelope)) { - return envelope.getResult().getMinX(); + var envelope = SpatialEnvelopeVisitor.visit(geometry); + if (envelope.isPresent()) { + return envelope.get().getMinX(); } throw new IllegalArgumentException("Cannot determine envelope of geometry"); } diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StYMax.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StYMax.java index 4f10fc540fc55..81d4d3d606054 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StYMax.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StYMax.java @@ -97,9 +97,9 @@ static double fromWellKnownBinary(BytesRef wkb) { if (geometry instanceof Point point) { return point.getY(); } - var envelope = new SpatialEnvelopeVisitor(); - if (geometry.visit(envelope)) { - return envelope.getResult().getMaxY(); + var envelope = SpatialEnvelopeVisitor.visit(geometry); + if (envelope.isPresent()) { + return envelope.get().getMaxY(); } throw new IllegalArgumentException("Cannot determine envelope of geometry"); } diff --git a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StYMin.java b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StYMin.java index 77ec5554cb619..5a761dd128d24 100644 --- a/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StYMin.java +++ b/x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StYMin.java @@ -97,9 +97,9 @@ static double fromWellKnownBinary(BytesRef wkb) { if (geometry instanceof Point point) { return point.getY(); } - var envelope = new SpatialEnvelopeVisitor(); - if (geometry.visit(envelope)) { - return envelope.getResult().getMinY(); + var envelope = SpatialEnvelopeVisitor.visit(geometry); + if (envelope.isPresent()) { + return envelope.get().getMinY(); } throw new IllegalArgumentException("Cannot determine envelope of geometry"); } diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StXMaxTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StXMaxTests.java index ffacf37c19a89..cdf6dbb397288 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StXMaxTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StXMaxTests.java @@ -52,9 +52,9 @@ private static double valueOf(BytesRef wkb) { if (geometry instanceof Point point) { return point.getX(); } - var envelope = new SpatialEnvelopeVisitor(); - if (geometry.visit(envelope)) { - return envelope.getResult().getMaxX(); + var envelope = SpatialEnvelopeVisitor.visit(geometry); + if (envelope.isPresent()) { + return envelope.get().getMaxX(); } throw new IllegalArgumentException("Geometry is empty"); } diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StXMinTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StXMinTests.java index bf388ddd0f77b..a7fd561d3819b 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StXMinTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StXMinTests.java @@ -52,9 +52,9 @@ private static double valueOf(BytesRef wkb) { if (geometry instanceof Point point) { return point.getX(); } - var envelope = new SpatialEnvelopeVisitor(); - if (geometry.visit(envelope)) { - return envelope.getResult().getMinX(); + var envelope = SpatialEnvelopeVisitor.visit(geometry); + if (envelope.isPresent()) { + return envelope.get().getMinX(); } throw new IllegalArgumentException("Geometry is empty"); } diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StYMaxTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StYMaxTests.java index 645cf7c434b1c..77bb21c3a54cc 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StYMaxTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StYMaxTests.java @@ -52,9 +52,9 @@ private static double valueOf(BytesRef wkb) { if (geometry instanceof Point point) { return point.getY(); } - var envelope = new SpatialEnvelopeVisitor(); - if (geometry.visit(envelope)) { - return envelope.getResult().getMaxY(); + var envelope = SpatialEnvelopeVisitor.visit(geometry); + if (envelope.isPresent()) { + return envelope.get().getMaxY(); } throw new IllegalArgumentException("Geometry is empty"); } diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StYMinTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StYMinTests.java index 79396e03ed597..3efea89ce8a0a 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StYMinTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/spatial/StYMinTests.java @@ -52,9 +52,9 @@ private static double valueOf(BytesRef wkb) { if (geometry instanceof Point point) { return point.getY(); } - var envelope = new SpatialEnvelopeVisitor(); - if (geometry.visit(envelope)) { - return envelope.getResult().getMinY(); + var envelope = SpatialEnvelopeVisitor.visit(geometry); + if (envelope.isPresent()) { + return envelope.get().getMinY(); } throw new IllegalArgumentException("Geometry is empty"); }