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

support SRID for spatial type column definition #1018

Merged
merged 24 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 18 additions & 1 deletion enginetest/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -7942,7 +7942,7 @@ var InfoSchemaScripts = []ScriptTest{
},
},
{
Name: "information_schema.columns",
Name: "information_schema.columns for view",
SetUpScript: []string{
"USE foo",
"drop table other_table",
Expand Down Expand Up @@ -8029,6 +8029,23 @@ var InfoSchemaScripts = []ScriptTest{
},
},
},
{
Name: "information_schema.columns with srs_id defined in spatial columns",
SetUpScript: []string{
"CREATE TABLE stable (geo GEOMETRY NOT NULL DEFAULT (POINT(2, 5)), line LINESTRING NOT NULL, pnt POINT SRID 4326, pol POLYGON NOT NULL SRID 0);",
},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT TABLE_NAME, COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE, COLUMN_TYPE, COLUMN_KEY, SRS_ID FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'stable'",
Expected: []sql.Row{
{"stable", "geo", "POINT(2, 5)", "NO", "geometry", "geometry", "", "NULL"},
{"stable", "line", nil, "NO", "linestring", "linestring", "", "NULL"},
{"stable", "pnt", nil, "YES", "point", "point", "", "4326"},
{"stable", "pol", nil, "NO", "polygon", "polygon", "", "0"},
},
},
},
},
}

var ExplodeQueries = []QueryTest{
Expand Down
13 changes: 6 additions & 7 deletions sql/expression/function/geojson.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func SliceToPoint(coords interface{}) (interface{}, error) {
if !ok {
return nil, errors.New("coordinate must be of type number")
}
return sql.Point{SRID: GeoSpatialSRID, X: x, Y: y}, nil
return sql.Point{SRID: sql.GeoSpatialSRID, X: x, Y: y}, nil
}

func SliceToLine(coords interface{}) (interface{}, error) {
Expand All @@ -378,7 +378,7 @@ func SliceToLine(coords interface{}) (interface{}, error) {
}
points[i] = p.(sql.Point)
}
return sql.Linestring{SRID: GeoSpatialSRID, Points: points}, nil
return sql.Linestring{SRID: sql.GeoSpatialSRID, Points: points}, nil
}

func SliceToPoly(coords interface{}) (interface{}, error) {
Expand All @@ -401,7 +401,7 @@ func SliceToPoly(coords interface{}) (interface{}, error) {
}
lines[i] = l.(sql.Linestring)
}
return sql.Polygon{SRID: GeoSpatialSRID, Lines: lines}, nil
return sql.Polygon{SRID: sql.GeoSpatialSRID, Lines: lines}, nil
}

// Eval implements the sql.Expression interface.
Expand Down Expand Up @@ -535,12 +535,11 @@ func (g *GeomFromGeoJSON) Eval(ctx *sql.Context, row sql.Row) (interface{}, erro
default:
return nil, errors.New("incorrect srid value")
}
// Check for invalid SRID
if _srid != CartesianSRID && _srid != GeoSpatialSRID {
return nil, ErrInvalidSRID.New(g.FunctionName(), _srid)
if err = ValidateSRID(_srid); err != nil {
return nil, err
}
// If SRID is GeoSpatialSRID (4326), do nothing
if _srid == GeoSpatialSRID {
if _srid == sql.GeoSpatialSRID {
return res, nil
}
// Swap coordinates with SRID 0
Expand Down
10 changes: 2 additions & 8 deletions sql/expression/function/srid.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ var _ sql.FunctionExpression = (*SRID)(nil)

var ErrInvalidSRID = errors.NewKind("There's no spatial reference with SRID %d")

const (
CartesianSRID = uint32(0)
GeoSpatialSRID = uint32(4326)
fulghum marked this conversation as resolved.
Show resolved Hide resolved
)

// NewSRID creates a new STX expression.
func NewSRID(args ...sql.Expression) (sql.Expression, error) {
if len(args) != 1 && len(args) != 2 {
Expand Down Expand Up @@ -160,9 +155,8 @@ func (s *SRID) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
// Type assertion
_srid := srid.(uint32)

// Must be either 0 or 4326
if _srid != CartesianSRID && _srid != GeoSpatialSRID {
return nil, ErrInvalidSRID.New(_srid)
if err = ValidateSRID(_srid); err != nil {
return nil, err
}

// Create new geometry object with matching SRID
Expand Down
29 changes: 16 additions & 13 deletions sql/expression/function/wkb.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,8 @@ func (g *GeomFromWKB) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
srid = s.(uint32)
}

// Must be valid SRID
if srid != CartesianSRID && srid != GeoSpatialSRID {
return nil, ErrInvalidSRID.New(srid)
if err = ValidateSRID(srid); err != nil {
return nil, err
}

// Convert this block to helper function
Expand Down Expand Up @@ -521,7 +520,7 @@ func (p *PointFromWKB) Eval(ctx *sql.Context, row sql.Row) (interface{}, error)

// TODO: convert to this block to helper function
// Determine SRID
srid := CartesianSRID
srid := sql.CartesianSRID
if len(p.ChildExpressions) >= 2 {
s, err := p.ChildExpressions[1].Eval(ctx, row)
if err != nil {
Expand All @@ -537,9 +536,8 @@ func (p *PointFromWKB) Eval(ctx *sql.Context, row sql.Row) (interface{}, error)
srid = s.(uint32)
}

// Must be valid SRID
if srid != CartesianSRID && srid != GeoSpatialSRID {
return nil, ErrInvalidSRID.New(srid)
if err = ValidateSRID(srid); err != nil {
return nil, err
}

// Determine xy order
Expand Down Expand Up @@ -652,9 +650,8 @@ func (l *LineFromWKB) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
srid = s.(uint32)
}

// Must be valid SRID
if srid != CartesianSRID && srid != GeoSpatialSRID {
return nil, ErrInvalidSRID.New(srid)
if err = ValidateSRID(srid); err != nil {
return nil, err
}

// Determine xy order
Expand Down Expand Up @@ -767,9 +764,8 @@ func (p *PolyFromWKB) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
srid = s.(uint32)
}

// Must be valid SRID
if srid != CartesianSRID && srid != GeoSpatialSRID {
return nil, ErrInvalidSRID.New(srid)
if err = ValidateSRID(srid); err != nil {
return nil, err
}

// Determine xy order
Expand All @@ -791,3 +787,10 @@ func (p *PolyFromWKB) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
// Read data
return WKBToPoly(v[WKBHeaderLength:], isBig, srid, order)
}

func ValidateSRID(srid uint32) error {
if srid != sql.CartesianSRID && srid != sql.GeoSpatialSRID {
return ErrInvalidSRID.New(srid)
}
return nil
}
66 changes: 45 additions & 21 deletions sql/expression/function/wkb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,23 @@ func TestGeomFromWKB(t *testing.T) {

v, err := f.Eval(sql.NewEmptyContext(), nil)
require.NoError(err)
require.Equal(sql.Point{SRID: CartesianSRID, X: 1, Y: 2}, v)
require.Equal(sql.Point{SRID: sql.CartesianSRID, X: 1, Y: 2}, v)
})

t.Run("convert point with srid 4326", func(t *testing.T) {
t.Run("convert point with srid valid 4326", func(t *testing.T) {
require := require.New(t)
res, err := hex.DecodeString("0101000000000000000000F03F0000000000000040")
require.NoError(err)
f, err := NewGeomFromWKB(expression.NewLiteral(res, sql.Blob),
expression.NewLiteral(GeoSpatialSRID, sql.Uint32))
expression.NewLiteral(sql.GeoSpatialSRID, sql.Uint32))
require.NoError(err)

v, err := f.Eval(sql.NewEmptyContext(), nil)
require.NoError(err)
require.Equal(sql.Point{SRID: GeoSpatialSRID, X: 1, Y: 2}, v)
require.Equal(sql.Point{SRID: sql.GeoSpatialSRID, X: 1, Y: 2}, v)
})

t.Run("convert point with srid 1234", func(t *testing.T) {
t.Run("convert point with invalid srid 1234", func(t *testing.T) {
require := require.New(t)
res, err := hex.DecodeString("0101000000000000000000F03F0000000000000040")
require.NoError(err)
Expand All @@ -208,95 +208,119 @@ func TestGeomFromWKB(t *testing.T) {
res, err := hex.DecodeString("0101000000000000000000F03F0000000000000040")
require.NoError(err)
f, err := NewGeomFromWKB(expression.NewLiteral(res, sql.Blob),
expression.NewLiteral(GeoSpatialSRID, sql.Uint32),
expression.NewLiteral(sql.GeoSpatialSRID, sql.Uint32),
expression.NewLiteral("axis-order=srid-defined", sql.Blob))
require.NoError(err)

v, err := f.Eval(sql.NewEmptyContext(), nil)
require.NoError(err)
require.Equal(sql.Point{SRID: GeoSpatialSRID, X: 1, Y: 2}, v)
require.Equal(sql.Point{SRID: sql.GeoSpatialSRID, X: 1, Y: 2}, v)
})

t.Run("convert point with srid 4326 axis long-lat", func(t *testing.T) {
require := require.New(t)
res, err := hex.DecodeString("0101000000000000000000F03F0000000000000040")
require.NoError(err)
f, err := NewGeomFromWKB(expression.NewLiteral(res, sql.Blob),
expression.NewLiteral(GeoSpatialSRID, sql.Uint32),
expression.NewLiteral(sql.GeoSpatialSRID, sql.Uint32),
expression.NewLiteral("axis-order=long-lat", sql.Blob))
require.NoError(err)

v, err := f.Eval(sql.NewEmptyContext(), nil)
require.NoError(err)
require.Equal(sql.Point{SRID: GeoSpatialSRID, X: 2, Y: 1}, v)
require.Equal(sql.Point{SRID: sql.GeoSpatialSRID, X: 2, Y: 1}, v)
})

t.Run("convert point with srid 4326 axis long-lat", func(t *testing.T) {
require := require.New(t)
res, err := hex.DecodeString("0101000000000000000000F03F0000000000000040")
require.NoError(err)
f, err := NewGeomFromWKB(expression.NewLiteral(res, sql.Blob),
expression.NewLiteral(GeoSpatialSRID, sql.Uint32),
expression.NewLiteral(sql.GeoSpatialSRID, sql.Uint32),
expression.NewLiteral("axis-order=long-lat", sql.Blob))
require.NoError(err)

v, err := f.Eval(sql.NewEmptyContext(), nil)
require.NoError(err)
require.Equal(sql.Point{SRID: GeoSpatialSRID, X: 2, Y: 1}, v)
require.Equal(sql.Point{SRID: sql.GeoSpatialSRID, X: 2, Y: 1}, v)
})

t.Run("convert linestring with srid 4326", func(t *testing.T) {
t.Run("convert linestring with valid srid 4326", func(t *testing.T) {
require := require.New(t)
res, err := hex.DecodeString("010200000002000000000000000000F03F000000000000004000000000000008400000000000001040")
require.NoError(err)
f, err := NewGeomFromWKB(expression.NewLiteral(res, sql.Blob),
expression.NewLiteral(GeoSpatialSRID, sql.Uint32))
expression.NewLiteral(sql.GeoSpatialSRID, sql.Uint32))
require.NoError(err)

v, err := f.Eval(sql.NewEmptyContext(), nil)
require.NoError(err)
require.Equal(sql.Linestring{SRID: GeoSpatialSRID, Points: []sql.Point{{SRID: GeoSpatialSRID, X: 1, Y: 2}, {SRID: GeoSpatialSRID, X: 3, Y: 4}}}, v)
require.Equal(sql.Linestring{SRID: sql.GeoSpatialSRID, Points: []sql.Point{{SRID: sql.GeoSpatialSRID, X: 1, Y: 2}, {SRID: sql.GeoSpatialSRID, X: 3, Y: 4}}}, v)
})

t.Run("convert linestring with invalid srid 2222", func(t *testing.T) {
require := require.New(t)
res, err := hex.DecodeString("010200000002000000000000000000F03F000000000000004000000000000008400000000000001040")
require.NoError(err)
f, err := NewGeomFromWKB(expression.NewLiteral(res, sql.Blob),
expression.NewLiteral(2222, sql.Uint32))
require.NoError(err)

_, err = f.Eval(sql.NewEmptyContext(), nil)
require.Error(err)
})

t.Run("convert linestring with srid 4326 axis long-lat", func(t *testing.T) {
require := require.New(t)
res, err := hex.DecodeString("010200000002000000000000000000F03F000000000000004000000000000008400000000000001040")
require.NoError(err)
f, err := NewGeomFromWKB(expression.NewLiteral(res, sql.Blob),
expression.NewLiteral(GeoSpatialSRID, sql.Uint32),
expression.NewLiteral(sql.GeoSpatialSRID, sql.Uint32),
expression.NewLiteral("axis-order=long-lat", sql.Blob))
require.NoError(err)

v, err := f.Eval(sql.NewEmptyContext(), nil)
require.NoError(err)
require.Equal(sql.Linestring{SRID: GeoSpatialSRID, Points: []sql.Point{{SRID: GeoSpatialSRID, X: 2, Y: 1}, {SRID: GeoSpatialSRID, X: 4, Y: 3}}}, v)
require.Equal(sql.Linestring{SRID: sql.GeoSpatialSRID, Points: []sql.Point{{SRID: sql.GeoSpatialSRID, X: 2, Y: 1}, {SRID: sql.GeoSpatialSRID, X: 4, Y: 3}}}, v)
})

t.Run("convert polygon with srid 4326", func(t *testing.T) {
t.Run("convert polygon with valid srid 4326", func(t *testing.T) {
require := require.New(t)
res, err := hex.DecodeString("0103000000010000000400000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F000000000000000000000000000000000000000000000000")
require.NoError(err)
f, err := NewGeomFromWKB(expression.NewLiteral(res, sql.Blob),
expression.NewLiteral(GeoSpatialSRID, sql.Uint32))
expression.NewLiteral(sql.GeoSpatialSRID, sql.Uint32))
require.NoError(err)

v, err := f.Eval(sql.NewEmptyContext(), nil)
require.NoError(err)
require.Equal(sql.Polygon{SRID: GeoSpatialSRID, Lines: []sql.Linestring{{SRID: GeoSpatialSRID, Points: []sql.Point{{SRID: GeoSpatialSRID, X: 0, Y: 0}, {SRID: GeoSpatialSRID, X: 1, Y: 1}, {SRID: GeoSpatialSRID, X: 1, Y: 0}, {SRID: GeoSpatialSRID, X: 0, Y: 0}}}}}, v)
require.Equal(sql.Polygon{SRID: sql.GeoSpatialSRID, Lines: []sql.Linestring{{SRID: sql.GeoSpatialSRID, Points: []sql.Point{{SRID: sql.GeoSpatialSRID, X: 0, Y: 0}, {SRID: sql.GeoSpatialSRID, X: 1, Y: 1}, {SRID: sql.GeoSpatialSRID, X: 1, Y: 0}, {SRID: sql.GeoSpatialSRID, X: 0, Y: 0}}}}}, v)
})

t.Run("convert polygon with invalid srid 2", func(t *testing.T) {
require := require.New(t)
res, err := hex.DecodeString("0103000000010000000400000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F000000000000000000000000000000000000000000000000")
require.NoError(err)
f, err := NewGeomFromWKB(expression.NewLiteral(res, sql.Blob),
expression.NewLiteral(2, sql.Uint32))
require.NoError(err)

_, err = f.Eval(sql.NewEmptyContext(), nil)
require.Error(err)
})

t.Run("convert polygon with srid 4326 axis long-lat", func(t *testing.T) {
require := require.New(t)
res, err := hex.DecodeString("0103000000010000000400000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F000000000000000000000000000000000000000000000000")
require.NoError(err)
f, err := NewGeomFromWKB(expression.NewLiteral(res, sql.Blob),
expression.NewLiteral(GeoSpatialSRID, sql.Uint32),
expression.NewLiteral(sql.GeoSpatialSRID, sql.Uint32),
expression.NewLiteral("axis-order=long-lat", sql.Blob))
require.NoError(err)

v, err := f.Eval(sql.NewEmptyContext(), nil)
require.NoError(err)
require.Equal(sql.Polygon{SRID: GeoSpatialSRID, Lines: []sql.Linestring{{SRID: GeoSpatialSRID, Points: []sql.Point{{SRID: GeoSpatialSRID, X: 0, Y: 0}, {SRID: GeoSpatialSRID, X: 1, Y: 1}, {SRID: GeoSpatialSRID, X: 0, Y: 1}, {SRID: GeoSpatialSRID, X: 0, Y: 0}}}}}, v)
require.Equal(sql.Polygon{SRID: sql.GeoSpatialSRID, Lines: []sql.Linestring{{SRID: sql.GeoSpatialSRID, Points: []sql.Point{{SRID: sql.GeoSpatialSRID, X: 0, Y: 0}, {SRID: sql.GeoSpatialSRID, X: 1, Y: 1}, {SRID: sql.GeoSpatialSRID, X: 0, Y: 1}, {SRID: sql.GeoSpatialSRID, X: 0, Y: 0}}}}}, v)
})

t.Run("convert null", func(t *testing.T) {
Expand Down
20 changes: 8 additions & 12 deletions sql/expression/function/wkt.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,8 @@ func (g *GeomFromText) Eval(ctx *sql.Context, row sql.Row) (interface{}, error)
srid = s.(uint32)
}

// Must be valid SRID
if srid != CartesianSRID && srid != GeoSpatialSRID {
return nil, ErrInvalidSRID.New(srid)
if err = ValidateSRID(srid); err != nil {
return nil, err
}

// Determine xy order
Expand Down Expand Up @@ -506,9 +505,8 @@ func (p *PointFromWKT) Eval(ctx *sql.Context, row sql.Row) (interface{}, error)
srid = s.(uint32)
}

// Must be valid SRID
if srid != CartesianSRID && srid != GeoSpatialSRID {
return nil, ErrInvalidSRID.New(srid)
if err = ValidateSRID(srid); err != nil {
return nil, err
}

// Determine xy order
Expand Down Expand Up @@ -619,9 +617,8 @@ func (l *LineFromWKT) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
srid = s.(uint32)
}

// Must be valid SRID
if srid != CartesianSRID && srid != GeoSpatialSRID {
return nil, ErrInvalidSRID.New(srid)
if err = ValidateSRID(srid); err != nil {
return nil, err
}

// Determine xt order
Expand Down Expand Up @@ -732,9 +729,8 @@ func (p *PolyFromWKT) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
srid = s.(uint32)
}

// Must be valid SRID
if srid != CartesianSRID && srid != GeoSpatialSRID {
return nil, ErrInvalidSRID.New(srid)
if err = ValidateSRID(srid); err != nil {
return nil, err
}

// Determine xy order
Expand Down
Loading