-
-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1512 from dolthub/james/st-equals
partially implementing st_equals
- Loading branch information
Showing
4 changed files
with
265 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright 2023 Dolthub, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package spatial | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/dolthub/go-mysql-server/sql" | ||
"github.com/dolthub/go-mysql-server/sql/expression" | ||
"github.com/dolthub/go-mysql-server/sql/types" | ||
) | ||
|
||
// STEquals is a function that returns the STEquals of a LineString | ||
type STEquals struct { | ||
expression.BinaryExpression | ||
} | ||
|
||
var _ sql.FunctionExpression = (*STEquals)(nil) | ||
|
||
// NewSTEquals creates a new STEquals expression. | ||
func NewSTEquals(g1, g2 sql.Expression) sql.Expression { | ||
return &STEquals{ | ||
expression.BinaryExpression{ | ||
Left: g1, | ||
Right: g2, | ||
}, | ||
} | ||
} | ||
|
||
// FunctionName implements sql.FunctionExpression | ||
func (s *STEquals) FunctionName() string { | ||
return "st_equals" | ||
} | ||
|
||
// Description implements sql.FunctionExpression | ||
func (s *STEquals) Description() string { | ||
return "returns 1 or 0 to indicate whether g1 is spatially equal to g2." | ||
} | ||
|
||
// Type implements the sql.Expression interface. | ||
func (s *STEquals) Type() sql.Type { | ||
return types.Boolean | ||
} | ||
|
||
func (s *STEquals) String() string { | ||
return fmt.Sprintf("ST_EQUALS(%s, %s)", s.Left, s.Right) | ||
} | ||
|
||
// WithChildren implements the Expression interface. | ||
func (s *STEquals) WithChildren(children ...sql.Expression) (sql.Expression, error) { | ||
if len(children) != 2 { | ||
return nil, sql.ErrInvalidChildrenNumber.New(s, len(children), 2) | ||
} | ||
return NewSTEquals(children[0], children[1]), nil | ||
} | ||
|
||
// isEqual checks if the set of types.Points in g1 is spatially equal to g2 | ||
// This is equivalent to checking if g1 within g2 and g2 within g1 | ||
func isEqual(g1 types.GeometryValue, g2 types.GeometryValue) bool { | ||
return isWithin(g1, g2) && isWithin(g2, g1) | ||
} | ||
|
||
// Eval implements the sql.Expression interface. | ||
func (s *STEquals) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { | ||
geom1, err := s.Left.Eval(ctx, row) | ||
if err != nil { | ||
return nil, err | ||
} | ||
geom2, err := s.Right.Eval(ctx, row) | ||
if err != nil { | ||
return nil, err | ||
} | ||
g1, g2, err := validateGeomComp(geom1, geom2, s.FunctionName()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if g1 == nil || g2 == nil { | ||
return nil, nil | ||
} | ||
|
||
// TODO (james): remove this switch block when the other comparisons are implemented | ||
switch geom1.(type) { | ||
case types.LineString: | ||
return nil, sql.ErrUnsupportedGISTypeForSpatialFunc.New("LineString", s.FunctionName()) | ||
case types.Polygon: | ||
return nil, sql.ErrUnsupportedGISTypeForSpatialFunc.New("Polygon", s.FunctionName()) | ||
case types.MultiPoint: | ||
return nil, sql.ErrUnsupportedGISTypeForSpatialFunc.New("MultiPoint", s.FunctionName()) | ||
case types.MultiLineString: | ||
return nil, sql.ErrUnsupportedGISTypeForSpatialFunc.New("MultiLineString", s.FunctionName()) | ||
case types.MultiPolygon: | ||
return nil, sql.ErrUnsupportedGISTypeForSpatialFunc.New("MultiPolygon", s.FunctionName()) | ||
case types.GeomColl: | ||
return nil, sql.ErrUnsupportedGISTypeForSpatialFunc.New("GeomColl", s.FunctionName()) | ||
} | ||
|
||
// TODO (james): remove this switch block when the other comparisons are implemented | ||
switch geom2.(type) { | ||
case types.LineString: | ||
return nil, sql.ErrUnsupportedGISTypeForSpatialFunc.New("LineString", s.FunctionName()) | ||
case types.Polygon: | ||
return nil, sql.ErrUnsupportedGISTypeForSpatialFunc.New("Polygon", s.FunctionName()) | ||
case types.MultiPoint: | ||
return nil, sql.ErrUnsupportedGISTypeForSpatialFunc.New("MultiPoint", s.FunctionName()) | ||
case types.MultiLineString: | ||
return nil, sql.ErrUnsupportedGISTypeForSpatialFunc.New("MultiLineString", s.FunctionName()) | ||
case types.MultiPolygon: | ||
return nil, sql.ErrUnsupportedGISTypeForSpatialFunc.New("MultiPolygon", s.FunctionName()) | ||
case types.GeomColl: | ||
return nil, sql.ErrUnsupportedGISTypeForSpatialFunc.New("GeomColl", s.FunctionName()) | ||
} | ||
|
||
return isEqual(g1, g2), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Copyright 2023 Dolthub, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package spatial | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/dolthub/go-mysql-server/sql" | ||
"github.com/dolthub/go-mysql-server/sql/expression" | ||
"github.com/dolthub/go-mysql-server/sql/types" | ||
) | ||
|
||
func TestSTEquals(t *testing.T) { | ||
t.Run("point vs point equals", func(t *testing.T) { | ||
require := require.New(t) | ||
p1 := types.Point{X: 123, Y: 456} | ||
p2 := types.Point{X: 123, Y: 456} | ||
f := NewSTEquals(expression.NewLiteral(p1, types.PointType{}), expression.NewLiteral(p2, types.PointType{})) | ||
v, err := f.Eval(sql.NewEmptyContext(), nil) | ||
require.NoError(err) | ||
require.Equal(true, v) | ||
}) | ||
|
||
t.Run("point vs point not equals", func(t *testing.T) { | ||
require := require.New(t) | ||
p1 := types.Point{X: 123, Y: 456} | ||
p2 := types.Point{X: 789, Y: 321} | ||
f := NewSTEquals(expression.NewLiteral(p1, types.PointType{}), expression.NewLiteral(p2, types.PointType{})) | ||
v, err := f.Eval(sql.NewEmptyContext(), nil) | ||
require.NoError(err) | ||
require.Equal(false, v) | ||
}) | ||
|
||
t.Run("null vs point is null", func(t *testing.T) { | ||
require := require.New(t) | ||
p := types.Point{X: 789, Y: 321} | ||
f := NewSTEquals(expression.NewLiteral(nil, types.Null), expression.NewLiteral(p, types.PointType{})) | ||
v, err := f.Eval(sql.NewEmptyContext(), nil) | ||
require.NoError(err) | ||
require.Equal(nil, v) | ||
}) | ||
|
||
t.Run("different SRIDs error", func(t *testing.T) { | ||
require := require.New(t) | ||
p1 := types.Point{SRID: 0, X: 123, Y: 456} | ||
p2 := types.Point{SRID: 4326, X: 123, Y: 456} | ||
f := NewSTEquals(expression.NewLiteral(p1, types.PointType{}), expression.NewLiteral(p2, types.PointType{})) | ||
_, err := f.Eval(sql.NewEmptyContext(), nil) | ||
require.Error(err) | ||
}) | ||
} | ||
|
||
func TestSTEqualsSkipped(t *testing.T) { | ||
t.Skip("comparisons that aren't point vs point are unsupported") | ||
|
||
t.Run("linestring vs linestring equals", func(t *testing.T) { | ||
require := require.New(t) | ||
l1 := types.LineString{Points: []types.Point{{X: 12, Y: 34}, {X: 56, Y: 78}, {X: 56, Y: 78}}} | ||
l2 := types.LineString{Points: []types.Point{{X: 56, Y: 78}, {X: 12, Y: 34}, {X: 12, Y: 34}}} | ||
f := NewSTEquals(expression.NewLiteral(l1, types.LineStringType{}), expression.NewLiteral(l2, types.LineStringType{})) | ||
v, err := f.Eval(sql.NewEmptyContext(), nil) | ||
require.NoError(err) | ||
require.Equal(true, v) | ||
}) | ||
|
||
t.Run("linestring vs linestring not equals", func(t *testing.T) { | ||
require := require.New(t) | ||
l1 := types.LineString{Points: []types.Point{{X: 12, Y: 34}, {X: 56, Y: 78}}} | ||
l2 := types.LineString{Points: []types.Point{{X: 56, Y: 78}, {X: 12, Y: 34}, {X: 123, Y: 349}}} | ||
f := NewSTEquals(expression.NewLiteral(l1, types.LineStringType{}), expression.NewLiteral(l2, types.LineStringType{})) | ||
v, err := f.Eval(sql.NewEmptyContext(), nil) | ||
require.NoError(err) | ||
require.Equal(false, v) | ||
}) | ||
|
||
t.Run("polygon vs multilinestring not equal", func(t *testing.T) { | ||
require := require.New(t) | ||
p1 := types.Point{X: 0, Y: 0} | ||
p2 := types.Point{X: 0, Y: 1} | ||
p3 := types.Point{X: 1, Y: 0} | ||
p4 := types.Point{X: 1, Y: 1} | ||
l1 := types.LineString{Points: []types.Point{p1, p2, p3, p4}} | ||
p := types.Polygon{Lines: []types.LineString{l1}} | ||
ml := types.MultiLineString{Lines: []types.LineString{l1}} | ||
f := NewSTEquals(expression.NewLiteral(p, types.PolygonType{}), expression.NewLiteral(ml, types.MultiLineStringType{})) | ||
v, err := f.Eval(sql.NewEmptyContext(), nil) | ||
require.NoError(err) | ||
require.Equal(false, v) | ||
}) | ||
|
||
t.Run("empty geometry vs empty geometry equal", func(t *testing.T) { | ||
require := require.New(t) | ||
g1 := types.GeomColl{} | ||
g2 := types.GeomColl{} | ||
f := NewSTEquals(expression.NewLiteral(g1, types.GeomCollType{}), expression.NewLiteral(g2, types.GeomCollType{})) | ||
v, err := f.Eval(sql.NewEmptyContext(), nil) | ||
require.NoError(err) | ||
require.Equal(true, v) | ||
}) | ||
|
||
t.Run("point geometry vs linestring geometry not equal", func(t *testing.T) { | ||
require := require.New(t) | ||
p1 := types.Point{X: 0, Y: 0} | ||
p2 := types.Point{X: 0, Y: 1} | ||
l1 := types.LineString{Points: []types.Point{p1, p2}} | ||
g1 := types.GeomColl{Geoms: []types.GeometryValue{p1, p2}} | ||
g2 := types.GeomColl{Geoms: []types.GeometryValue{l1}} | ||
f := NewSTEquals(expression.NewLiteral(g1, types.GeomCollType{}), expression.NewLiteral(g2, types.GeomCollType{})) | ||
v, err := f.Eval(sql.NewEmptyContext(), nil) | ||
require.NoError(err) | ||
require.Equal(false, v) | ||
}) | ||
} |