Skip to content

Commit f3f0d2e

Browse files
Always check crsType when folding spatial functions (elastic#112090) (elastic#112099)
* Always check crsType when folding spatial functions * Update docs/changelog/112090.yaml * Only require capability for fixed test The other tests passed on older versions anyway.
1 parent 918fa01 commit f3f0d2e

File tree

9 files changed

+102
-14
lines changed

9 files changed

+102
-14
lines changed

docs/changelog/112090.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 112090
2+
summary: Always check `crsType` when folding spatial functions
3+
area: Geo
4+
type: bug
5+
issues:
6+
- 112089

x-pack/plugin/esql/qa/testFixtures/src/main/resources/spatial.csv-spec

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,17 @@ POINT (42.97109629958868 14.7552534006536) | 1
487487
###############################################
488488
# Tests for ST_INTERSECTS on GEO_POINT type
489489

490+
literalGeoPointIntersectsLiteralPolygon
491+
required_capability: st_intersects
492+
493+
ROW pt = TO_GEOPOINT("POINT(0 85)"), polygon = TO_GEOSHAPE("POLYGON((-10 70, 10 70, 10 85, -10 85, -10 70))")
494+
| EVAL intersects = ST_INTERSECTS(pt, polygon)
495+
;
496+
497+
pt:geo_point | polygon:geo_shape | intersects:boolean
498+
POINT(0 85) | POLYGON((-10 70, 10 70, 10 85, -10 85, -10 70)) | true
499+
;
500+
490501
pointIntersectsLiteralPolygon
491502
required_capability: st_intersects
492503

@@ -889,6 +900,34 @@ wkt:keyword | pt:geo_point | distance:double
889900
"POINT(1 -1)" | POINT(1 -1) | 157249.59498573805
890901
;
891902

903+
literalGeoPointDistanceOneDegree
904+
required_capability: st_distance
905+
906+
ROW wkt = ["POINT(1 0)", "POINT(-1 0)", "POINT(0 1)", "POINT(0 -1)"]
907+
| MV_EXPAND wkt
908+
| EVAL pt = TO_GEOPOINT(wkt)
909+
| EVAL distance = ST_DISTANCE(pt, TO_GEOPOINT("POINT(0 0)"))
910+
;
911+
912+
wkt:keyword | pt:geo_point | distance:double
913+
"POINT(1 0)" | POINT(1 0) | 111195.07310665186
914+
"POINT(-1 0)" | POINT(-1 0) | 111195.08242688453
915+
"POINT(0 1)" | POINT(0 1) | 111195.07776676829
916+
"POINT(0 -1)" | POINT(0 -1) | 111195.08242688453
917+
;
918+
919+
twoCitiesPointDistanceGeo
920+
required_capability: st_distance
921+
required_capability: spatial_functions_fix_crstype_folding
922+
923+
ROW p1 = TO_GEOPOINT("POINT(-90.82814 29.79511)"), p2 = TO_GEOPOINT("POINT(-90.79731509999999 29.8835389)")
924+
| EVAL d = ST_DISTANCE(p1, p2)
925+
;
926+
927+
p1:geo_point | p2:geo_point | d:double
928+
POINT (-90.82814 29.79511) | POINT (-90.79731509999999 29.8835389) | 10272.529272836206
929+
;
930+
892931
airportCityLocationPointDistance
893932
required_capability: st_distance
894933

@@ -1433,6 +1472,17 @@ POINT (726480.0130685265 3359566.331716279) | 849
14331472
###############################################
14341473
# Tests for ST_INTERSECTS on CARTESIAN_POINT type
14351474

1475+
literalCartesianPointIntersectsLiteralPolygon
1476+
required_capability: st_intersects
1477+
1478+
ROW pt = TO_CARTESIANPOINT("POINT(0 85)"), polygon = TO_CARTESIANSHAPE("POLYGON((-10 70, 10 70, 10 85, -10 85, -10 70))")
1479+
| EVAL intersects = ST_INTERSECTS(pt, polygon)
1480+
;
1481+
1482+
pt:cartesian_point | polygon:cartesian_shape | intersects:boolean
1483+
POINT(0 85) | POLYGON((-10 70, 10 70, 10 85, -10 85, -10 70)) | true
1484+
;
1485+
14361486
cartesianCentroidFromAirportsAfterIntersectsPredicate
14371487
required_capability: st_intersects
14381488

@@ -1996,6 +2046,33 @@ wkt:keyword | pt:cartesian_point | distance:double
19962046
"POINT(1 -1)" | POINT(1 -1) | 1.4142135623730951
19972047
;
19982048

2049+
literalCartesianPointDistanceOneUnit
2050+
required_capability: st_distance
2051+
2052+
ROW wkt = ["POINT(1 0)", "POINT(-1 0)", "POINT(0 1)", "POINT(0 -1)"]
2053+
| MV_EXPAND wkt
2054+
| EVAL pt = TO_CARTESIANPOINT(wkt)
2055+
| EVAL distance = ST_DISTANCE(pt, TO_CARTESIANPOINT("POINT(0 0)"))
2056+
;
2057+
2058+
wkt:keyword | pt:cartesian_point | distance:double
2059+
"POINT(1 0)" | POINT(1 0) | 1.0
2060+
"POINT(-1 0)" | POINT(-1 0) | 1.0
2061+
"POINT(0 1)" | POINT(0 1) | 1.0
2062+
"POINT(0 -1)" | POINT(0 -1) | 1.0
2063+
;
2064+
2065+
twoCitiesPointDistanceCartesian
2066+
required_capability: st_distance
2067+
2068+
ROW p1 = TO_CARTESIANPOINT("POINT(-90.82814 29.79511)"), p2 = TO_CARTESIANPOINT("POINT(-90.79731509999999 29.8835389)")
2069+
| EVAL d = ST_DISTANCE(p1, p2)
2070+
;
2071+
2072+
p1:cartesian_point | p2:cartesian_point | d:double
2073+
POINT (-90.82814 29.79511) | POINT (-90.79731509999999 29.8835389) | 0.09364744959271905
2074+
;
2075+
19992076
airportCartesianCityLocationPointDistance
20002077
required_capability: st_distance
20012078

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlCapabilities.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ public enum Cap {
9292
*/
9393
ST_DISTANCE,
9494

95+
/**
96+
* Fix determination of CRS types in spatial functions when folding.
97+
*/
98+
SPATIAL_FUNCTIONS_FIX_CRSTYPE_FOLDING,
99+
95100
/**
96101
* Fix to GROK and DISSECT that allows extracting attributes with the same name as the input
97102
* https://github.com/elastic/elasticsearch/issues/110184

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static List<NamedWriteableRegistry.Entry> getNamedWriteables() {
4343
}
4444

4545
private final SpatialTypeResolver spatialTypeResolver;
46-
protected SpatialCrsType crsType;
46+
private SpatialCrsType crsType;
4747
protected final boolean leftDocValues;
4848
protected final boolean rightDocValues;
4949

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ protected NodeInfo<? extends Expression> info() {
177177
@Override
178178
public Object fold() {
179179
try {
180-
GeometryDocValueReader docValueReader = asGeometryDocValueReader(crsType, left());
180+
GeometryDocValueReader docValueReader = asGeometryDocValueReader(crsType(), left());
181181
Geometry rightGeom = makeGeometryFromLiteral(right());
182-
Component2D[] components = asLuceneComponent2Ds(crsType, rightGeom);
183-
return (crsType == SpatialCrsType.GEO)
182+
Component2D[] components = asLuceneComponent2Ds(crsType(), rightGeom);
183+
return (crsType() == SpatialCrsType.GEO)
184184
? GEO.geometryRelatesGeometries(docValueReader, components)
185185
: CARTESIAN.geometryRelatesGeometries(docValueReader, components);
186186
} catch (IOException e) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ protected NodeInfo<? extends Expression> info() {
132132
@Override
133133
public Object fold() {
134134
try {
135-
GeometryDocValueReader docValueReader = asGeometryDocValueReader(crsType, left());
136-
Component2D component2D = asLuceneComponent2D(crsType, right());
137-
return (crsType == SpatialCrsType.GEO)
135+
GeometryDocValueReader docValueReader = asGeometryDocValueReader(crsType(), left());
136+
Component2D component2D = asLuceneComponent2D(crsType(), right());
137+
return (crsType() == SpatialCrsType.GEO)
138138
? GEO.geometryRelatesGeometry(docValueReader, component2D)
139139
: CARTESIAN.geometryRelatesGeometry(docValueReader, component2D);
140140
} catch (IOException e) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ protected NodeInfo<? extends Expression> info() {
130130
@Override
131131
public Object fold() {
132132
try {
133-
GeometryDocValueReader docValueReader = asGeometryDocValueReader(crsType, left());
134-
Component2D component2D = asLuceneComponent2D(crsType, right());
135-
return (crsType == SpatialCrsType.GEO)
133+
GeometryDocValueReader docValueReader = asGeometryDocValueReader(crsType(), left());
134+
Component2D component2D = asLuceneComponent2D(crsType(), right());
135+
return (crsType() == SpatialCrsType.GEO)
136136
? GEO.geometryRelatesGeometry(docValueReader, component2D)
137137
: CARTESIAN.geometryRelatesGeometry(docValueReader, component2D);
138138
} catch (IOException e) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ protected NodeInfo<? extends Expression> info() {
132132
@Override
133133
public Object fold() {
134134
try {
135-
GeometryDocValueReader docValueReader = asGeometryDocValueReader(crsType, left());
136-
Component2D component2D = asLuceneComponent2D(crsType, right());
137-
return (crsType == SpatialCrsType.GEO)
135+
GeometryDocValueReader docValueReader = asGeometryDocValueReader(crsType(), left());
136+
Component2D component2D = asLuceneComponent2D(crsType(), right());
137+
return (crsType() == SpatialCrsType.GEO)
138138
? GEO.geometryRelatesGeometry(docValueReader, component2D)
139139
: CARTESIAN.geometryRelatesGeometry(docValueReader, component2D);
140140
} catch (IOException e) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ protected NodeInfo<? extends Expression> info() {
173173
public Object fold() {
174174
var leftGeom = makeGeometryFromLiteral(left());
175175
var rightGeom = makeGeometryFromLiteral(right());
176-
return (crsType == SpatialCrsType.GEO) ? GEO.distance(leftGeom, rightGeom) : CARTESIAN.distance(leftGeom, rightGeom);
176+
return (crsType() == SpatialCrsType.GEO) ? GEO.distance(leftGeom, rightGeom) : CARTESIAN.distance(leftGeom, rightGeom);
177177
}
178178

179179
@Override

0 commit comments

Comments
 (0)