Skip to content

Commit

Permalink
added st_makeenvelope builtin
Browse files Browse the repository at this point in the history
* fixes cockroachdb#80357

Release note (sql change): added builtin st_makeenvelope

Signed-off-by: Frédéric BIDON <[email protected]>
  • Loading branch information
fredbi authored and otan committed Apr 28, 2022
1 parent 2ce801c commit ff86650
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2203,6 +2203,10 @@ calculated, the result is transformed back into a Geography with SRID 4326.</p>
</span></td></tr>
<tr><td><a name="st_makebox2d"></a><code>st_makebox2d(geometry_a: geometry, geometry_b: geometry) &rarr; box2d</code></td><td><span class="funcdesc"><p>Creates a box2d from two points. Errors if arguments are not two non-empty points.</p>
</span></td></tr>
<tr><td><a name="st_makeenvelope"></a><code>st_makeenvelope(xmin: <a href="float.html">float</a>, ymin: <a href="float.html">float</a>, xmax: <a href="float.html">float</a>, ymax: <a href="float.html">float</a>) &rarr; geometry</code></td><td><span class="funcdesc"><p>Creates a rectangular Polygon from the minimum and maximum values for X and Y with SRID 0.</p>
</span></td></tr>
<tr><td><a name="st_makeenvelope"></a><code>st_makeenvelope(xmin: <a href="float.html">float</a>, ymin: <a href="float.html">float</a>, xmax: <a href="float.html">float</a>, ymax: <a href="float.html">float</a>, srid: <a href="int.html">int</a>) &rarr; geometry</code></td><td><span class="funcdesc"><p>Creates a rectangular Polygon from the minimum and maximum values for X and Y with the given SRID.</p>
</span></td></tr>
<tr><td><a name="st_makepoint"></a><code>st_makepoint(x: <a href="float.html">float</a>, y: <a href="float.html">float</a>) &rarr; geometry</code></td><td><span class="funcdesc"><p>Returns a new Point with the given X and Y coordinates.</p>
</span></td></tr>
<tr><td><a name="st_makepoint"></a><code>st_makepoint(x: <a href="float.html">float</a>, y: <a href="float.html">float</a>, z: <a href="float.html">float</a>) &rarr; geometry</code></td><td><span class="funcdesc"><p>Returns a new Point with the given X, Y, and Z coordinates.</p>
Expand Down
11 changes: 11 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/geospatial
Original file line number Diff line number Diff line change
Expand Up @@ -5892,3 +5892,14 @@ FROM ( VALUES
----
false false false
false false false

# ST_MakeEnvelope
query T
SELECT ST_AsEWKT(ST_MakeEnvelope(30.01,50.01,72.01,52.01,4326))
----
SRID=4326;POLYGON ((30.010000000000002 50.009999999999998, 30.010000000000002 52.009999999999998, 72.010000000000005 52.009999999999998, 72.010000000000005 50.009999999999998, 30.010000000000002 50.009999999999998))

query T
SELECT ST_AsEWKT(ST_MakeEnvelope(30.01,50.01,72.01,52.01))
----
POLYGON ((30.010000000000002 50.009999999999998, 30.010000000000002 52.009999999999998, 72.010000000000005 52.009999999999998, 72.010000000000005 50.009999999999998, 30.010000000000002 50.009999999999998))
65 changes: 63 additions & 2 deletions pkg/sql/sem/builtins/geo_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -5302,7 +5302,7 @@ The calculations are done on a sphere.`,
},
Info: infoBuilder{
info: `Snaps the vertices and segments of input geometry the target geometry's vertices.
Tolerance is used to control where snapping is performed. The result geometry is the input geometry with the vertices snapped.
Tolerance is used to control where snapping is performed. The result geometry is the input geometry with the vertices snapped.
If no snapping occurs then the input geometry is returned unchanged.`,
}.String(),
Volatility: volatility.Immutable,
Expand Down Expand Up @@ -5558,6 +5558,42 @@ Bottom Left.`,
Volatility: volatility.Immutable,
},
),
"st_makeenvelope": makeBuiltin(
defProps(),
tree.Overload{
Types: tree.ArgTypes{
{"xmin", types.Float},
{"ymin", types.Float},
{"xmax", types.Float},
{"ymax", types.Float},
{"srid", types.Int},
},
ReturnType: tree.FixedReturnType(types.Geometry),
Fn: func(_ *eval.Context, args tree.Datums) (tree.Datum, error) {
return stEnvelopeFromArgs(args)
},
Info: infoBuilder{
info: "Creates a rectangular Polygon from the minimum and maximum values for X and Y with the given SRID.",
}.String(),
Volatility: volatility.Immutable,
},
tree.Overload{
Types: tree.ArgTypes{
{"xmin", types.Float},
{"ymin", types.Float},
{"xmax", types.Float},
{"ymax", types.Float},
},
ReturnType: tree.FixedReturnType(types.Geometry),
Fn: func(_ *eval.Context, args tree.Datums) (tree.Datum, error) {
return stEnvelopeFromArgs(args)
},
Info: infoBuilder{
info: "Creates a rectangular Polygon from the minimum and maximum values for X and Y with SRID 0.",
}.String(),
Volatility: volatility.Immutable,
},
),
"st_flipcoordinates": makeBuiltin(
defProps(),
tree.Overload{
Expand Down Expand Up @@ -6768,7 +6804,7 @@ May return a Point or LineString in the case of degenerate inputs.`,
return tree.NewDInt(tree.DInt(ret)), nil
},
Info: infoBuilder{
info: `Returns an interger value defining behavior of crossing of lines:
info: `Returns an interger value defining behavior of crossing of lines:
0: lines do not cross,
-1: linestring_b crosses linestring_a from right to left,
1: linestring_b crosses linestring_a from left to right,
Expand Down Expand Up @@ -7502,3 +7538,28 @@ func applyGeoindexConfigStorageParams(
}
return indexDesc.GeoConfig, nil
}

// stEnvelopeFromArgs builds a rectangle geometry from types.Float bounds as datums
func stEnvelopeFromArgs(args tree.Datums) (tree.Datum, error) {
xmin := float64(tree.MustBeDFloat(args[0]))
ymin := float64(tree.MustBeDFloat(args[1]))
xmax := float64(tree.MustBeDFloat(args[2]))
ymax := float64(tree.MustBeDFloat(args[3]))

var srid int
if len(args) > 4 {
srid = int(tree.MustBeDInt(args[4]))
}

extent, err := geo.MakeGeometryFromGeomT(
geom.NewBounds(geom.XY).
Set(xmin, ymin, xmax, ymax).
Polygon().
SetSRID(srid),
)
if err != nil {
return nil, err
}

return tree.NewDGeometry(extent), nil
}

0 comments on commit ff86650

Please sign in to comment.