Skip to content

Commit

Permalink
geo: allow st_makeenvelope min/max bounds to be reversed
Browse files Browse the repository at this point in the history
Postgis allows xmin/ymin to be greater than xmax/ymax respectively in
st_makeenvelope, but the geom library does not allow this when
converting Bounds to Polygon. Instead, we build the polygon explicitly
from the provided min/max coordinates.

Epic: None
Fixes: #111157

Release note (bug fix): The geospatial st_makeenvelope built-in now
correctly supports xmin or ymin to be greater than xmax or ymax,
respectively.
  • Loading branch information
rharding6373 committed Nov 3, 2023
1 parent eb85843 commit d929408
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 12 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/geospatial
Original file line number Diff line number Diff line change
Expand Up @@ -6244,4 +6244,16 @@ SELECT ST_Extent(ST_PointFromGeoHash(NULL::TEXT, 1::INT4)::GEOMETRY);
----
NULL

# X min > X max
query T
SELECT ST_AsEWKT(ST_MakeEnvelope(8.0::FLOAT8, 2.0::FLOAT8, 5.0::FLOAT8, 4.0::FLOAT8)::GEOMETRY);
----
POLYGON ((8 2, 8 4, 5 4, 5 2, 8 2))

# Y min > Y max
query T
SELECT ST_AsEWKT(ST_MakeEnvelope(5.0::FLOAT8, 4.0::FLOAT8, 8.0::FLOAT8, 2.0::FLOAT8)::GEOMETRY);
----
POLYGON ((5 4, 5 2, 8 2, 8 4, 5 4))

subtest end
12 changes: 8 additions & 4 deletions pkg/sql/sem/builtins/geo_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -7878,12 +7878,16 @@ func stEnvelopeFromArgs(args tree.Datums) (tree.Datum, error) {
if len(args) > 4 {
srid = int(tree.MustBeDInt(args[4]))
}
coords := []float64{
xmin, ymin,
xmin, ymax,
xmax, ymax,
xmax, ymin,
xmin, ymin,
}

extent, err := geo.MakeGeometryFromGeomT(
geom.NewBounds(geom.XY).
Set(xmin, ymin, xmax, ymax).
Polygon().
SetSRID(srid),
geom.NewPolygonFlat(geom.XY, coords, []int{len(coords)}).SetSRID(srid),
)
if err != nil {
return nil, err
Expand Down

0 comments on commit d929408

Please sign in to comment.