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

partially implementing st_equals #1512

Merged
merged 13 commits into from
Mar 31, 2023
13 changes: 11 additions & 2 deletions sql/analyzer/indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ type indexLookup struct {

type indexLookupsByTable map[string]*indexLookup

func isSpatialAnalysisFunc(e sql.Expression) bool {
switch e.(type) {
case *spatial.Intersects, *spatial.Within, *spatial.STEquals:
return true
default:
return false
}
}

// getIndexes returns indexes applicable to all tables in the node given for the expression given, keyed by the name of
// the table (aliased as appropriate). If more than one index per table is usable for the expression given, chooses a
// best match (typically the longest prefix by column count).
Expand Down Expand Up @@ -252,7 +261,7 @@ func getIndexes(
// Next try to match the remaining expressions individually
for _, e := range unusedExprs {
// TODO: eventually support merging spatial lookups
if _, ok := e.(*spatial.Intersects); ok {
if isSpatialAnalysisFunc(e) {
continue
}
indexes, err := getIndexes(ctx, ia, e, tableAliases)
Expand All @@ -279,7 +288,7 @@ func getIndexes(
// TODO (james): add all other spatial index supported functions here
// TODO: make generalizable to all functions?
switch e := e.(type) {
case *spatial.Intersects, *spatial.Within:
case *spatial.Intersects, *spatial.Within, *spatial.STEquals:
// don't pushdown functions with bindvars
if exprHasBindVar(e) {
return nil, nil
Expand Down
1 change: 1 addition & 0 deletions sql/expression/function/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ var BuiltIns = []sql.Function{
sql.Function1{Name: "st_astext", Fn: spatial.NewAsWKT},
sql.FunctionN{Name: "st_distance", Fn: spatial.NewDistance},
sql.Function1{Name: "st_dimension", Fn: spatial.NewDimension},
sql.Function2{Name: "st_equal", Fn: spatial.NewSTEquals},
sql.Function1{Name: "st_endpoint", Fn: spatial.NewEndPoint},
sql.FunctionN{Name: "st_geomcollfromtext", Fn: spatial.NewGeomCollFromText},
sql.FunctionN{Name: "st_geomcollfromtxt", Fn: spatial.NewGeomCollFromText},
Expand Down
126 changes: 126 additions & 0 deletions sql/expression/function/spatial/st_equals.go
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
}
127 changes: 127 additions & 0 deletions sql/expression/function/spatial/st_equals_test.go
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)
})
}