Skip to content

Commit

Permalink
add parsing for SRID (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
jennifersp authored May 25, 2022
1 parent 8f50d80 commit 9c94a40
Show file tree
Hide file tree
Showing 5 changed files with 4,709 additions and 4,657 deletions.
18 changes: 17 additions & 1 deletion go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2116,6 +2116,9 @@ type ColumnType struct {
// Generated columns
GeneratedExpr Expr // The expression used to generate this column
Stored BoolVal // Default is Virtual (not stored)

// For spatial types
SRID *SQLVal
}

func (ct *ColumnType) merge(other ColumnType) error {
Expand Down Expand Up @@ -2173,6 +2176,16 @@ func (ct *ColumnType) merge(other ColumnType) error {
ct.Stored = true
}

if other.SRID != nil {
if ct.SRID != nil {
return errors.New("cannot include SRID more than once")
}
if ct.Type != "" && ct.SQLType() != sqltypes.Geometry {
return errors.New("cannot define SRID for non spatial types")
}
ct.SRID = other.SRID
}

return nil
}

Expand Down Expand Up @@ -2207,6 +2220,9 @@ func (ct *ColumnType) Format(buf *TrackedBuffer) {
if ct.NotNull {
opts = append(opts, keywordStrings[NOT], keywordStrings[NULL])
}
if ct.SRID != nil {
opts = append(opts, keywordStrings[SRID], String(ct.SRID))
}
if ct.Default != nil {
opts = append(opts, keywordStrings[DEFAULT], String(ct.Default))
}
Expand Down Expand Up @@ -2284,7 +2300,7 @@ func (ct *ColumnType) DescribeType() string {

// SQLType returns the sqltypes type code for the given column
func (ct *ColumnType) SQLType() querypb.Type {
switch ct.Type {
switch strings.ToLower(ct.Type) {
case keywordStrings[TINYINT]:
if ct.Unsigned {
return sqltypes.Uint8
Expand Down
30 changes: 30 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2015,6 +2015,9 @@ var (
}, {
input: "create table t (c int not null default 0 on update current_timestamp() auto_increment comment 'a comment here' unique)",
output: "create table t (\n\tc int not null default 0 on update current_timestamp() auto_increment comment 'a comment here' unique\n)",
}, {
input: "create table t (c int null default 0 on update current_timestamp() auto_increment comment 'a comment here' unique)",
output: "create table t (\n\tc int default 0 on update current_timestamp() auto_increment comment 'a comment here' unique\n)",
}, {
input: "create table t (c INT NOT NULL DEFAULT 0 ON UPDATE current_timestamp() AUTO_INCREMENT COMMENT 'a comment here' UNIQUE)",
output: "create table t (\n\tc INT not null default 0 on update current_timestamp() auto_increment comment 'a comment here' unique\n)",
Expand Down Expand Up @@ -2561,6 +2564,24 @@ var (
}, {
input: "CREATE PROCEDURE proc (IN p_store_id INT) SELECT * FROM inventory WHERE store_id = p_store_id INTO DUMPFILE 'dumpfile.txt'",
output: "create procedure proc (in p_store_id INT) select * from inventory where store_id = p_store_id into dumpfile 'dumpfile.txt'",
}, {
input: "CREATE TABLE t (id INT PRIMARY KEY, col1 GEOMETRY SRID 0)",
output: "create table t (\n\tid INT primary key,\n\tcol1 GEOMETRY srid 0\n)",
}, {
input: "CREATE TABLE t (id INT PRIMARY KEY, col1 POLYGON NULL SRID 0)",
output: "create table t (\n\tid INT primary key,\n\tcol1 POLYGON srid 0\n)",
}, {
input: "CREATE TABLE t (id INT PRIMARY KEY, col1 LINESTRING NULL SRID 0 COMMENT 'my comment')",
output: "create table t (\n\tid INT primary key,\n\tcol1 LINESTRING srid 0 comment 'my comment'\n)",
}, {
input: "CREATE TABLE t (id INT PRIMARY KEY, col1 GEOMETRYCOLLECTION NOT NULL SRID 0)",
output: "create table t (\n\tid INT primary key,\n\tcol1 GEOMETRYCOLLECTION not null srid 0\n)",
}, {
input: "ALTER TABLE t ADD COLUMN col1 POINT NOT NULL SRID 0 DEFAULT (POINT(1, 2))",
output: "alter table t add column (\n\tcol1 POINT not null srid 0 default (POINT(1, 2))\n)",
}, {
input: "ALTER TABLE t MODIFY COLUMN col1 POINT NOT NULL DEFAULT (POINT(1, 2)) SRID 1234",
output: "alter table t modify column col1 (\n\tcol1 POINT not null srid 1234 default (POINT(1, 2))\n)",
},
}
// Any tests that contain multiple statements within the body (such as BEGIN/END blocks) should go here.
Expand Down Expand Up @@ -4932,6 +4953,15 @@ var (
}, {
input: "insert into a select * into @a from b",
output: "INTO clause is not allowed at position 38 near 'b'",
}, {
input: "create table t (id int primary key, col1 FLOAT SRID 0)",
output: "cannot define SRID for non spatial types at position 55 near '0'",
}, {
input: "create table t (id int primary key, col1 geometry SRID -1)",
output: "syntax error at position 57 near 'SRID'",
}, {
input: "create table t (id int primary key, col1 geometry null SRID 0 default null SRID 4236)",
output: "cannot include SRID more than once at position 85 near '4236'",
},
}
)
Expand Down
Loading

0 comments on commit 9c94a40

Please sign in to comment.