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

geo: fix st_segmentize to not panic when passed NaN as param #62828

Merged
merged 1 commit into from
Mar 31, 2021
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
3 changes: 3 additions & 0 deletions pkg/geo/geogfn/segmentize.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import (
// This works by dividing each segment by a power of 2 to find the
// smallest power less than or equal to the segmentMaxLength.
func Segmentize(geography geo.Geography, segmentMaxLength float64) (geo.Geography, error) {
if math.IsNaN(segmentMaxLength) || math.IsInf(segmentMaxLength, 1 /* sign */) {
return geography, nil
}
geometry, err := geography.AsGeomT()
if err != nil {
return geo.Geography{}, err
Expand Down
21 changes: 21 additions & 0 deletions pkg/geo/geogfn/segmentize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package geogfn

import (
"fmt"
"math"
"testing"

"github.com/cockroachdb/cockroach/pkg/geo"
Expand Down Expand Up @@ -100,6 +101,26 @@ func TestSegmentize(t *testing.T) {
maxSegmentLength: 150000.0,
expectedWKT: "LINESTRING Z (0 0 25,0 1 0,0.49878052093921765 2.0003038990352664 25,0.9981696941692514 3.0004561476391296 50,1.4984735304805308 4.000380457593079 75,2 5 100)",
},
{
wkt: "LINESTRING(0 0, 1 1)",
maxSegmentLength: math.NaN(),
expectedWKT: "LINESTRING(0 0, 1 1)",
},
{
wkt: "LINESTRING M (0 0 0, 1 1 1)",
maxSegmentLength: math.Sqrt(-1),
andyyang890 marked this conversation as resolved.
Show resolved Hide resolved
expectedWKT: "LINESTRING M (0 0 0, 1 1 1)",
},
{
wkt: "LINESTRING ZM (0 0 0 0, 1 1 1 1)",
maxSegmentLength: -math.NaN(),
expectedWKT: "LINESTRING(0 0 0 0, 1 1 1 1)",
},
{
wkt: "LINESTRING(0 0, 1 1)",
maxSegmentLength: math.Inf(1),
expectedWKT: "LINESTRING(0 0, 1 1)",
},
}
for _, test := range segmentizeTestCases {
t.Run(fmt.Sprintf("%s, maximum segment length: %f", test.wkt, test.maxSegmentLength), func(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/geo/geomfn/segmentize.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import (
// between given two-points such that each segment has length less
// than or equal to given maximum segment length.
func Segmentize(g geo.Geometry, segmentMaxLength float64) (geo.Geometry, error) {
if math.IsNaN(segmentMaxLength) || math.IsInf(segmentMaxLength, 1 /* sign */) {
return g, nil
}
geometry, err := g.AsGeomT()
if err != nil {
return geo.Geometry{}, err
Expand Down
21 changes: 21 additions & 0 deletions pkg/geo/geomfn/segmentize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package geomfn

import (
"fmt"
"math"
"testing"

"github.com/cockroachdb/cockroach/pkg/geo"
Expand Down Expand Up @@ -125,6 +126,26 @@ func TestSegmentize(t *testing.T) {
maxSegmentLength: -1,
expectedWKT: "MULTIPOINT ((0.0 0.0), (1.0 1.0))",
},
{
wkt: "LINESTRING(0 0, 1 1)",
maxSegmentLength: math.NaN(),
expectedWKT: "LINESTRING(0 0, 1 1)",
},
{
wkt: "LINESTRING M (0 0 0, 1 1 1)",
maxSegmentLength: math.Sqrt(-1),
expectedWKT: "LINESTRING M (0 0 0, 1 1 1)",
},
{
wkt: "LINESTRING ZM (0 0 0 0, 1 1 1 1)",
maxSegmentLength: -math.NaN(),
expectedWKT: "LINESTRING(0 0 0 0, 1 1 1 1)",
},
{
wkt: "LINESTRING(0 0, 1 1)",
maxSegmentLength: math.Inf(1),
expectedWKT: "LINESTRING(0 0, 1 1)",
},
}
for _, test := range segmentizeTestCases {
t.Run(fmt.Sprintf("%s, maximum segment length: %f", test.wkt, test.maxSegmentLength), func(t *testing.T) {
Expand Down