Skip to content

Commit

Permalink
geo: fix st_segmentize to not panic when passed NaN as param
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
Andy Yang authored and rafiss committed Apr 20, 2021
1 parent 31ef476 commit 1771b13
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
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
11 changes: 11 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 @@ -95,6 +96,16 @@ func TestSegmentize(t *testing.T) {
maxSegmentLength: -1,
expectedWKT: "MULTIPOINT((0 0), (1 1))",
},
{
wkt: "LINESTRING(0 0, 1 1)",
maxSegmentLength: math.NaN(),
expectedWKT: "LINESTRING(0 0, 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
11 changes: 11 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 @@ -105,6 +106,16 @@ 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(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

0 comments on commit 1771b13

Please sign in to comment.