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 9 commits
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
4 changes: 2 additions & 2 deletions _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func createTestDatabase() *memory.Database {

db := memory.NewDatabase(dbName)
table := memory.NewTable(tableName, sql.NewPrimaryKeySchema(sql.Schema{
{Name: "name", Type: sql.Text, Nullable: false, Source: tableName,PrimaryKey:true},
{Name: "email", Type: sql.Text, Nullable: false, Source: tableName,PrimaryKey:true},
{Name: "name", Type: sql.Text, Nullable: false, Source: tableName, PrimaryKey: true},
{Name: "email", Type: sql.Text, Nullable: false, Source: tableName, PrimaryKey: true},
{Name: "phone_numbers", Type: sql.JSON, Nullable: false, Source: tableName},
{Name: "created_at", Type: sql.Timestamp, Nullable: false, Source: tableName},
}), db.GetForeignKeyCollection())
Expand Down
198 changes: 198 additions & 0 deletions enginetest/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -2135,6 +2135,204 @@ var SpatialScriptTests = []ScriptTest{
},
},
},
{
Name: "create table using SRID value for geometry type",
SetUpScript: []string{
"CREATE TABLE tab0 (i int primary key, g geometry srid 4326 default (point(1,1)));",
},
Assertions: []ScriptTestAssertion{
{
Query: "show create table tab0",
Expected: []sql.Row{{"tab0", "CREATE TABLE `tab0` (\n `i` int NOT NULL,\n `g` geometry SRID 4326 DEFAULT (POINT(1, 1)),\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
},
{
Query: "INSERT INTO tab0 VALUES (1, ST_GEOMFROMTEXT(ST_ASWKT(POINT(1,2)), 4326))",
Expected: []sql.Row{{sql.NewOkResult(1)}},
},
{
Query: "select i, ST_ASWKT(g) FROM tab0",
Expected: []sql.Row{{1, "POINT(1 2)"}},
},
{
Query: "INSERT INTO tab0 VALUES (2, ST_GEOMFROMTEXT(ST_ASWKT(POINT(2,4))))",
ExpectedErr: sql.ErrNotMatchingSRIDWithColName,
},
{
Query: "INSERT INTO tab0 VALUES (2, ST_GEOMFROMTEXT(ST_ASWKT(LINESTRING(POINT(1, 6),POINT(4, 3))), 4326))",
Expected: []sql.Row{{sql.NewOkResult(1)}},
},
{
Query: "select i, ST_ASWKT(g) FROM tab0",
Expected: []sql.Row{{1, "POINT(1 2)"}, {2, "LINESTRING(1 6,4 3)"}},
},
},
},
{
Name: "create table using SRID value for linestring type",
SetUpScript: []string{
"CREATE TABLE tab1 (i int primary key, l linestring srid 0);",
},
Assertions: []ScriptTestAssertion{
{
Query: "show create table tab1",
Expected: []sql.Row{{"tab1", "CREATE TABLE `tab1` (\n `i` int NOT NULL,\n `l` linestring SRID 0,\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
},
{
Query: "INSERT INTO tab1 VALUES (1, LINESTRING(POINT(0, 0),POINT(2, 2)))",
Expected: []sql.Row{{sql.NewOkResult(1)}},
},
{
Query: "select i, ST_ASWKT(l) FROM tab1",
Expected: []sql.Row{{1, "LINESTRING(0 0,2 2)"}},
},
{
Query: "INSERT INTO tab1 VALUES (2, ST_GEOMFROMTEXT(ST_ASWKT(LINESTRING(POINT(1, 6),POINT(4, 3))), 4326))",
ExpectedErr: sql.ErrNotMatchingSRIDWithColName,
},
{
Query: "select i, ST_ASWKT(l) FROM tab1",
Expected: []sql.Row{{1, "LINESTRING(0 0,2 2)"}},
},
},
},
{
Name: "create table using SRID value for point type",
SetUpScript: []string{
"CREATE TABLE tab2 (i int primary key);",
},
Assertions: []ScriptTestAssertion{
{
Query: "ALTER TABLE tab2 ADD COLUMN p POINT NOT NULL SRID 0",
Expected: []sql.Row{{sql.NewOkResult(0)}},
},
{
Query: "show create table tab2",
Expected: []sql.Row{{"tab2", "CREATE TABLE `tab2` (\n `i` int NOT NULL,\n `p` point NOT NULL SRID 0,\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
},
{
Query: "INSERT INTO tab2 VALUES (1, POINT(2, 2))",
Expected: []sql.Row{{sql.NewOkResult(1)}},
},
{
Query: "select i, ST_ASWKT(p) FROM tab2",
Expected: []sql.Row{{1, "POINT(2 2)"}},
},
{
Query: "INSERT INTO tab2 VALUES (2, ST_GEOMFROMTEXT(ST_ASWKT(POINT(1, 6)), 4326))",
ExpectedErr: sql.ErrNotMatchingSRIDWithColName,
},
{
Query: "select i, ST_ASWKT(p) FROM tab2",
Expected: []sql.Row{{1, "POINT(2 2)"}},
},
{
Query: "ALTER TABLE tab2 CHANGE COLUMN p p POINT NOT NULL",
Expected: []sql.Row{{sql.NewOkResult(0)}},
},
{
Query: "INSERT INTO tab2 VALUES (2, ST_GEOMFROMTEXT(ST_ASWKT(POINT(1, 6)), 4326))",
Expected: []sql.Row{{sql.NewOkResult(1)}},
},
{
Query: "select i, ST_ASWKT(p) FROM tab2",
Expected: []sql.Row{{1, "POINT(2 2)"}, {2, "POINT(1 6)"}},
},
{
Query: "ALTER TABLE tab2 CHANGE COLUMN p p POINT NOT NULL SRID 4326",
ExpectedErr: sql.ErrNotMatchingSRIDWithColName,
},
{
Query: "delete from tab2 where i = 1",
Expected: []sql.Row{{sql.NewOkResult(1)}},
},
{
Query: "ALTER TABLE tab2 CHANGE COLUMN p p POINT NOT NULL SRID 4326",
Expected: []sql.Row{{sql.NewOkResult(0)}},
},
{
Query: "show create table tab2",
Expected: []sql.Row{{"tab2", "CREATE TABLE `tab2` (\n `i` int NOT NULL,\n `p` point NOT NULL SRID 4326,\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
},
},
},
{
Name: "create table using SRID value for polygon type",
SetUpScript: []string{
"CREATE TABLE tab3 (i int primary key, y polygon NOT NULL);",
},
Assertions: []ScriptTestAssertion{
{
Query: "show create table tab3",
Expected: []sql.Row{{"tab3", "CREATE TABLE `tab3` (\n `i` int NOT NULL,\n `y` polygon NOT NULL,\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
},
{
Query: "INSERT INTO tab3 VALUES (1, polygon(linestring(point(0,0),point(8,0),point(12,9),point(0,9),point(0,0))))",
Expected: []sql.Row{{sql.NewOkResult(1)}},
},
{
Query: "select i, ST_ASWKT(y) FROM tab3",
Expected: []sql.Row{{1, "POLYGON((0 0,8 0,12 9,0 9,0 0))"}},
},
{
Query: "ALTER TABLE tab3 MODIFY COLUMN y POLYGON NOT NULL SRID 0",
Expected: []sql.Row{{sql.NewOkResult(0)}},
},
{
Query: "ALTER TABLE tab3 MODIFY COLUMN y POLYGON NOT NULL SRID 4326",
ExpectedErr: sql.ErrNotMatchingSRIDWithColName,
},
},
},
{
Name: "invalid cases of SRID value",
SetUpScript: []string{
"CREATE TABLE table1 (i int primary key, p point srid 4326);",
"INSERT INTO table1 VALUES (1, ST_SRID(POINT(1, 5), 4326))",
},
Assertions: []ScriptTestAssertion{
{
Query: "CREATE TABLE table2 (i int primary key, p point srid 1);",
// error should be ErrInvalidSRID once we support all mysql valid srid values
ExpectedErr: sql.ErrUnsupportedFeature,
},
{
Query: "SELECT i, ST_ASWKT(p) FROM table1;",
Expected: []sql.Row{{1, "POINT(1 5)"}},
},
{
Query: "INSERT INTO table1 VALUES (2, POINT(2, 5))",
ExpectedErr: sql.ErrNotMatchingSRIDWithColName,
},
{
Query: "SELECT i, ST_ASWKT(p) FROM table1;",
Expected: []sql.Row{{1, "POINT(1 5)"}},
},
{
Query: "ALTER TABLE table1 CHANGE COLUMN p p linestring srid 4326",
ExpectedErr: sql.ErrSpatialTypeConversion,
},
{
Query: "ALTER TABLE table1 CHANGE COLUMN p p geometry srid 0",
ExpectedErr: sql.ErrNotMatchingSRIDWithColName,
},
{
Query: "ALTER TABLE table1 CHANGE COLUMN p p geometry srid 4326",
Expected: []sql.Row{{sql.NewOkResult(0)}},
},
{
Query: "show create table table1",
Expected: []sql.Row{{"table1", "CREATE TABLE `table1` (\n `i` int NOT NULL,\n `p` geometry SRID 4326,\n PRIMARY KEY (`i`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}},
},
{
Query: "INSERT INTO table1 VALUES (2, ST_SRID(LINESTRING(POINT(0, 0),POINT(2, 2)),4326))",
Expected: []sql.Row{{sql.NewOkResult(1)}},
},
{
Query: "ALTER TABLE table1 CHANGE COLUMN p p point srid 4326",
ExpectedErr: sql.ErrSpatialTypeConversion,
},
},
},
}

var CreateCheckConstraintsScripts = []ScriptTest{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/dolthub/go-mysql-server
require (
github.com/cespare/xxhash v1.1.0
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81
github.com/dolthub/vitess v0.0.0-20220517011201-8f50d80eae58
github.com/go-kit/kit v0.10.0
github.com/go-sql-driver/mysql v1.6.0
github.com/gocraft/dbr/v2 v2.7.2
Expand All @@ -28,6 +27,7 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dolthub/vitess v0.0.0-20220517170641-e9c641e2d8c0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ github.com/dolthub/jsonpath v0.0.0-20210609232853-d49537a30474 h1:xTrR+l5l+1Lfq0
github.com/dolthub/jsonpath v0.0.0-20210609232853-d49537a30474/go.mod h1:kMz7uXOXq4qRriCEyZ/LUeTqraLJCjf0WVZcUi6TxUY=
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81 h1:7/v8q9XGFa6q5Ap4Z/OhNkAMBaK5YeuEzwJt+NZdhiE=
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81/go.mod h1:siLfyv2c92W1eN/R4QqG/+RjjX5W2+gCTRjZxBjI3TY=
github.com/dolthub/vitess v0.0.0-20220517011201-8f50d80eae58 h1:v7uMbJKhb9zi2Nz3pxDOUVfWO30E5wbSckVq7AjgXRw=
github.com/dolthub/vitess v0.0.0-20220517011201-8f50d80eae58/go.mod h1:jxgvpEvrTNw2i4BKlwT75E775eUXBeMv5MPeQkIb9zI=
github.com/dolthub/vitess v0.0.0-20220517170641-e9c641e2d8c0 h1:NysD5NZ33G7lsZl7WhsIWoYYDf00FnkQTGqA5/bYU/I=
github.com/dolthub/vitess v0.0.0-20220517170641-e9c641e2d8c0/go.mod h1:jxgvpEvrTNw2i4BKlwT75E775eUXBeMv5MPeQkIb9zI=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
Expand Down
3 changes: 3 additions & 0 deletions memory/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,9 @@ func (t *Table) ModifyColumn(ctx *sql.Context, columnName string, column *sql.Co
oldRowWithoutVal = append(oldRowWithoutVal, row[oldIdx+1:]...)
newVal, err := column.Type.Convert(row[oldIdx])
if err != nil {
if sql.ErrNotMatchingSRID.Is(err) {
err = sql.ErrNotMatchingSRIDWithColName.New(columnName, err)
}
return err
}
var newRow sql.Row
Expand Down
2 changes: 1 addition & 1 deletion sql/analyzer/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func validateCreateTrigger(ctx *sql.Context, a *Analyzer, node sql.Node, scope *
}

// Check to see if the columns with "new" and "old" table reference are valid columns from the trigger table.
transform.InspectExpressionsWithNode(ct.Body, func(n sql.Node, e sql.Expression) bool {
transform.InspectExpressions(ct.Body, func(e sql.Expression) bool {
switch e := e.(type) {
case *expression.UnresolvedColumn:
if strings.ToLower(e.Table()) == "old" || strings.ToLower(e.Table()) == "new" {
Expand Down
9 changes: 9 additions & 0 deletions sql/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,15 @@ var (

// ErrUnsupportedJoinFactorCount is returned for a query with more commutable join tables than we support
ErrUnsupportedJoinFactorCount = errors.NewKind("unsupported join factor count: expected fewer than %d tables, found %d")

// ErrNotMatchingSRID is returned for SRID values not matching
ErrNotMatchingSRID = errors.NewKind("The SRID of the geometry is %v, but the SRID of the column is %v. Consider changing the SRID of the geometry or the SRID property of the column.")

// ErrNotMatchingSRIDWithColName is returned for error of SRID values not matching with column name detail
ErrNotMatchingSRIDWithColName = errors.NewKind("The SRID of the geometry does not match the SRID of the column '%s'. %v")

// ErrSpatialTypeConversion is returned when one spatial type cannot be converted to the other spatial type
ErrSpatialTypeConversion = errors.NewKind("Cannot get geometry object from data you send to the GEOMETRY field")
jennifersp marked this conversation as resolved.
Show resolved Hide resolved
)

func CastSQLError(err error) (*mysql.SQLError, error, bool) {
Expand Down
12 changes: 6 additions & 6 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: 4326, X: x, Y: y}, nil
return sql.Point{SRID: 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: 4326, Points: points}, nil
return sql.Linestring{SRID: 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: 4326, Lines: lines}, nil
return sql.Polygon{SRID: GeoSpatialSRID, Lines: lines}, nil
}

// Eval implements the sql.Expression interface.
Expand Down Expand Up @@ -536,11 +536,11 @@ func (g *GeomFromGeoJSON) Eval(ctx *sql.Context, row sql.Row) (interface{}, erro
return nil, errors.New("incorrect srid value")
}
// Check for invalid SRID
if _srid != 0 && _srid != 4326 {
if _srid != CartesianSRID && _srid != GeoSpatialSRID {
return nil, ErrInvalidSRID.New(g.FunctionName(), _srid)
}
// If SRID 4326, do nothing
if _srid == 4326 {
// If SRID is GeoSpatialSRID (4326), do nothing
if _srid == GeoSpatialSRID {
return res, nil
}
// Swap coordinates with SRID 0
Expand Down
4 changes: 2 additions & 2 deletions sql/expression/function/srid.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ var _ sql.FunctionExpression = (*SRID)(nil)
var ErrInvalidSRID = errors.NewKind("There's no spatial reference with SRID %d")

const (
CartesianSRID = 0
GeoSpatialSRID = 4326
CartesianSRID = uint32(0)
GeoSpatialSRID = uint32(4326)
)

// NewSRID creates a new STX expression.
Expand Down
10 changes: 5 additions & 5 deletions sql/expression/function/wkb.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func (g *GeomFromWKB) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
}

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

Expand Down Expand Up @@ -521,7 +521,7 @@ func (p *PointFromWKB) Eval(ctx *sql.Context, row sql.Row) (interface{}, error)

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

// Must be valid SRID
if srid != 0 && srid != 4230 {
if srid != CartesianSRID && srid != GeoSpatialSRID {
jennifersp marked this conversation as resolved.
Show resolved Hide resolved
return nil, ErrInvalidSRID.New(srid)
}

Expand Down Expand Up @@ -653,7 +653,7 @@ func (l *LineFromWKB) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
}

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

Expand Down Expand Up @@ -768,7 +768,7 @@ func (p *PolyFromWKB) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
}

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

Expand Down
Loading