Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builtins: implement ST_MakeBox2D #52961

Merged
merged 1 commit into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,8 @@ calculated, the result is transformed back into a Geography with SRID 4326.</p>
<tr><td><a name="st_longestline"></a><code>st_longestline(geometry_a: geometry, geometry_b: geometry) &rarr; geometry</code></td><td><span class="funcdesc"><p>Returns the LineString corresponds to the max distance across every pair of points comprising the given geometries.</p>
<p>Note if geometries are the same, it will return the LineString with the maximum distance between the geometry’s vertexes. The function will return the longest line that was discovered first when comparing maximum distances if more than one is found.</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_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_makepolygon"></a><code>st_makepolygon(geometry: geometry) &rarr; geometry</code></td><td><span class="funcdesc"><p>Returns a new Polygon with the given outer LineString.</p>
Expand Down
26 changes: 26 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/geospatial_bbox
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,32 @@ SELECT * FROM box2d_encoding_test ORDER BY id ASC
1 BOX(1 2,3 4) BOX(3 4,5 6) {"BOX(-1 -2,-3 -4)"}
2 BOX(10.1 20.1,30.5 40.6) BOX(30 40,50 60) {"BOX(-1 -2,-3 -4)","BOX(3 -4,5 -6)"}

subtest st_makebox2d

statement error first argument is not a POINT
SELECT ST_MakeBox2D('LINESTRING(0 0, 1 1)', 'POINT(1 0)')

statement error second argument is not a POINT
SELECT ST_MakeBox2D('POINT(1 0)', 'LINESTRING(0 0, 1 1)')

statement error cannot use POINT EMPTY
SELECT ST_MakeBox2D('POINT(1 0)', 'POINT EMPTY')

statement error cannot use POINT EMPTY
SELECT ST_MakeBox2D('POINT EMPTY', 'POINT (1 0)')

statement error operation on mixed SRIDs forbidden
SELECT ST_MakeBox2D('SRID=4326;POINT(1 0)', 'SRID=3857;POINT(1 0)')

query T
SELECT ST_MakeBox2D(a::geometry, b::geometry) FROM ( VALUES
('POINT (1 0)', 'POINT(5 6)'),
('POINT (1 0)', 'POINT(1 0)')
) tbl(a, b)
----
BOX(1 0,5 6)
BOX(1 0,1 0)

subtest comparison_ops

statement ok
Expand Down
40 changes: 40 additions & 0 deletions pkg/sql/sem/builtins/geo_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -3564,6 +3564,46 @@ Bottom Left.`,
// BoundingBox
//

"st_makebox2d": makeBuiltin(
defProps(),
geometryOverload2(
func(ctx *tree.EvalContext, a *tree.DGeometry, b *tree.DGeometry) (tree.Datum, error) {
if a.Geometry.SRID() != b.Geometry.SRID() {
return nil, geo.NewMismatchingSRIDsError(a, b)
}
aGeomT, err := a.AsGeomT()
if err != nil {
return nil, err
}
bGeomT, err := b.AsGeomT()
if err != nil {
return nil, err
}

switch aGeomT := aGeomT.(type) {
case *geom.Point:
switch bGeomT := bGeomT.(type) {
case *geom.Point:
if aGeomT.Empty() || bGeomT.Empty() {
return nil, errors.Newf("cannot use POINT EMPTY")
}
return tree.NewDBox2D(
a.CartesianBoundingBox().Combine(b.CartesianBoundingBox()),
), nil
default:
return nil, errors.Newf("second argument is not a POINT")
}
default:
return nil, errors.Newf("first argument is not a POINT")
}
},
types.Box2D,
infoBuilder{
info: "Creates a box2d from two points. Errors if arguments are not two non-empty points.",
},
tree.VolatilityImmutable,
),
),
"st_combinebbox": makeBuiltin(
tree.FunctionProperties{NullableArgs: true},
tree.Overload{
Expand Down