Skip to content

Commit

Permalink
Merge pull request #62834 from andyyang890/backport21.1-62828
Browse files Browse the repository at this point in the history
release-21.1: geo: fix st_segmentize to not panic when passed NaN as param
  • Loading branch information
andyyang890 authored Mar 31, 2021
2 parents b4ae375 + de41e30 commit e6d07fa
Show file tree
Hide file tree
Showing 4 changed files with 48 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
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),
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

0 comments on commit e6d07fa

Please sign in to comment.