Skip to content

Commit

Permalink
Merge pull request #9011 from ritwizsinha/Issue-8982
Browse files Browse the repository at this point in the history
Added brackets in default value if column is of type TEXT, BLOB, GEOMETRY or JSON
  • Loading branch information
GuptaManan100 authored Oct 19, 2021
2 parents 315d907 + aafde7c commit 9c4b4db
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 13 deletions.
9 changes: 3 additions & 6 deletions go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,10 @@ func (ct *ColumnType) Format(buf *TrackedBuffer) {
}
if ct.Options.Default != nil {
buf.astPrintf(ct, " %s", keywordStrings[DEFAULT])
_, isLiteral := ct.Options.Default.(*Literal)
_, isBool := ct.Options.Default.(BoolVal)
_, isNullVal := ct.Options.Default.(*NullVal)
if isLiteral || isNullVal || isBool || isExprAliasForCurrentTimeStamp(ct.Options.Default) {
buf.astPrintf(ct, " %v", ct.Options.Default)
} else {
if defaultRequiresParens(ct) {
buf.astPrintf(ct, " (%v)", ct.Options.Default)
} else {
buf.astPrintf(ct, " %v", ct.Options.Default)
}
}
if ct.Options.OnUpdate != nil {
Expand Down
11 changes: 4 additions & 7 deletions go/vt/sqlparser/ast_format_fast.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1518,3 +1518,21 @@ func (es *ExtractedSubquery) updateAlternative() {
es.alternative = expr
}
}

func defaultRequiresParens(ct *ColumnType) bool {
switch ct.Type {
case "TINYTEXT", "TEXT", "MEDIUMTEXT", "LONGTEXT", "TINYBLOB", "BLOB", "MEDIUMBLOB", "LONGBLOB", "JSON", "GEOMETRY", "POINT", "LINESTRING", "POLYGON", "MULTIPOINT", "MULTILINESTRING", "MULTIPOLYGON", "GEOMETRYCOLLECTION":
return true

}

_, isLiteral := ct.Options.Default.(*Literal)
_, isBool := ct.Options.Default.(BoolVal)
_, isNullVal := ct.Options.Default.(*NullVal)

if isLiteral || isNullVal || isBool || isExprAliasForCurrentTimeStamp(ct.Options.Default) {
return false
}

return true
}
102 changes: 102 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,108 @@ var (
}{{
input: "select 1",
output: "select 1 from dual",
}, {
input: "CREATE TABLE t2 (b BLOB DEFAULT 'abc')",
output: "create table t2 (\n\tb BLOB default ('abc')\n)",
}, {
input: "CREATE TABLE t2 (b BLOB DEFAULT ('abc'))",
output: "create table t2 (\n\tb BLOB default ('abc')\n)",
}, {
input: "CREATE TABLE t2 (b TINYBLOB DEFAULT 'abc')",
output: "create table t2 (\n\tb TINYBLOB default ('abc')\n)",
}, {
input: "CREATE TABLE t2 (b TINYBLOB DEFAULT ('abc'))",
output: "create table t2 (\n\tb TINYBLOB default ('abc')\n)",
}, {
input: "CREATE TABLE t2 (b MEDIUMBLOB DEFAULT 'abc')",
output: "create table t2 (\n\tb MEDIUMBLOB default ('abc')\n)",
}, {
input: "CREATE TABLE t2 (b MEDIUMBLOB DEFAULT ('abc'))",
output: "create table t2 (\n\tb MEDIUMBLOB default ('abc')\n)",
}, {
input: "CREATE TABLE t2 (b LONGBLOB DEFAULT 'abc')",
output: "create table t2 (\n\tb LONGBLOB default ('abc')\n)",
}, {
input: "CREATE TABLE t2 (b LONGBLOB DEFAULT ('abc'))",
output: "create table t2 (\n\tb LONGBLOB default ('abc')\n)",
}, {
input: "CREATE TABLE t2 (b TEXT DEFAULT 'abc')",
output: "create table t2 (\n\tb TEXT default ('abc')\n)",
}, {
input: "CREATE TABLE t2 (b TEXT DEFAULT ('abc'))",
output: "create table t2 (\n\tb TEXT default ('abc')\n)",
}, {
input: "CREATE TABLE t2 (b TINYTEXT DEFAULT 'abc')",
output: "create table t2 (\n\tb TINYTEXT default ('abc')\n)",
}, {
input: "CREATE TABLE t2 (b TINYTEXT DEFAULT ('abc'))",
output: "create table t2 (\n\tb TINYTEXT default ('abc')\n)",
}, {
input: "CREATE TABLE t2 (b MEDIUMTEXT DEFAULT 'abc')",
output: "create table t2 (\n\tb MEDIUMTEXT default ('abc')\n)",
}, {
input: "CREATE TABLE t2 (b MEDIUMTEXT DEFAULT ('abc'))",
output: "create table t2 (\n\tb MEDIUMTEXT default ('abc')\n)",
}, {
input: "CREATE TABLE t2 (b LONGTEXT DEFAULT 'abc')",
output: "create table t2 (\n\tb LONGTEXT default ('abc')\n)",
}, {
input: "CREATE TABLE t2 (b LONGTEXT DEFAULT ('abc'))",
output: "create table t2 (\n\tb LONGTEXT default ('abc')\n)",
}, {
input: "CREATE TABLE t2 (b JSON DEFAULT '{name:abc}')",
output: "create table t2 (\n\tb JSON default ('{name:abc}')\n)",
}, {
input: "CREATE TABLE t2 (b JSON DEFAULT ('{name:abc}'))",
output: "create table t2 (\n\tb JSON default ('{name:abc}')\n)",
}, {
input: "create table x(location POINT DEFAULT 7.0)",
output: "create table x (\n\tlocation POINT default (7.0)\n)",
}, {
input: "create table x(location POINT DEFAULT (7.0))",
output: "create table x (\n\tlocation POINT default (7.0)\n)",
}, {
input: "create table x(location GEOMETRY DEFAULT (POINT(7.0, 3.0)))",
output: "create table x (\n\tlocation GEOMETRY default (POINT(7.0, 3.0))\n)",
}, {
input: "create table x(location GEOMETRY DEFAULT POINT(7.0, 3.0))",
output: "create table x (\n\tlocation GEOMETRY default (POINT(7.0, 3.0))\n)",
}, {
input: "create table x(location LINESTRING DEFAULT (POINT(7.0, 3.0)))",
output: "create table x (\n\tlocation LINESTRING default (POINT(7.0, 3.0))\n)",
}, {
input: "create table x(location LINESTRING DEFAULT POINT(7.0, 3.0))",
output: "create table x (\n\tlocation LINESTRING default (POINT(7.0, 3.0))\n)",
}, {
input: "create table x(location POLYGON DEFAULT (POINT(7.0, 3.0)))",
output: "create table x (\n\tlocation POLYGON default (POINT(7.0, 3.0))\n)",
}, {
input: "create table x(location POLYGON DEFAULT POINT(7.0, 3.0))",
output: "create table x (\n\tlocation POLYGON default (POINT(7.0, 3.0))\n)",
}, {
input: "create table x(location MULTIPOINT DEFAULT (POINT(7.0, 3.0)))",
output: "create table x (\n\tlocation MULTIPOINT default (POINT(7.0, 3.0))\n)",
}, {
input: "create table x(location MULTIPOINT DEFAULT POINT(7.0, 3.0))",
output: "create table x (\n\tlocation MULTIPOINT default (POINT(7.0, 3.0))\n)",
}, {
input: "create table x(location MULTILINESTRING DEFAULT (POINT(7.0, 3.0)))",
output: "create table x (\n\tlocation MULTILINESTRING default (POINT(7.0, 3.0))\n)",
}, {
input: "create table x(location MULTILINESTRING DEFAULT POINT(7.0, 3.0))",
output: "create table x (\n\tlocation MULTILINESTRING default (POINT(7.0, 3.0))\n)",
}, {
input: "create table x(location MULTIPOLYGON DEFAULT (POINT(7.0, 3.0)))",
output: "create table x (\n\tlocation MULTIPOLYGON default (POINT(7.0, 3.0))\n)",
}, {
input: "create table x(location MULTIPOLYGON DEFAULT POINT(7.0, 3.0))",
output: "create table x (\n\tlocation MULTIPOLYGON default (POINT(7.0, 3.0))\n)",
}, {
input: "create table x(location GEOMETRYCOLLECTION DEFAULT (POINT(7.0, 3.0)))",
output: "create table x (\n\tlocation GEOMETRYCOLLECTION default (POINT(7.0, 3.0))\n)",
}, {
input: "create table x(location GEOMETRYCOLLECTION DEFAULT POINT(7.0, 3.0))",
output: "create table x (\n\tlocation GEOMETRYCOLLECTION default (POINT(7.0, 3.0))\n)",
}, {
input: "WITH RECURSIVE odd_num_cte (id, n) AS (SELECT 1, 1 union all SELECT id+1, n+2 from odd_num_cte where id < 5) SELECT * FROM odd_num_cte",
output: "with recursive odd_num_cte(id, n) as (select 1, 1 from dual union all select id + 1, n + 2 from odd_num_cte where id < 5) select * from odd_num_cte",
Expand Down

0 comments on commit 9c4b4db

Please sign in to comment.