Skip to content

Commit

Permalink
Merge pull request #7740 from planetscale/nullable-timestamp
Browse files Browse the repository at this point in the history
Nullable Timestamp Column Fix
  • Loading branch information
systay authored Mar 30, 2021
2 parents 67b3f33 + 70626f7 commit 2fcfe29
Show file tree
Hide file tree
Showing 22 changed files with 867 additions and 817 deletions.
9 changes: 8 additions & 1 deletion go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,14 @@ type ColumnType struct {

// ColumnTypeOptions are generic field options for a column type
type ColumnTypeOptions struct {
NotNull bool
/* We need Null to be *bool to distinguish 3 cases -
1. When Not Null is specified (Null = false)
2. When Null is specified (Null = true)
3. When nothing is specified (Null = nil)
The complexity arises from the fact that we do not know whether the column will be nullable or not if nothing is specified.
Therefore we do not know whether the column is nullable or not in case 3.
*/
Null *bool
Autoincrement bool
Default Expr
OnUpdate Expr
Expand Down
1 change: 1 addition & 0 deletions go/vt/sqlparser/ast_clone.go

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

4 changes: 2 additions & 2 deletions go/vt/sqlparser/ast_equals.go

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

8 changes: 6 additions & 2 deletions go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,12 @@ func (ct *ColumnType) Format(buf *TrackedBuffer) {
if ct.Collate != "" {
buf.astPrintf(ct, " %s %s", keywordStrings[COLLATE], ct.Collate)
}
if ct.Options.NotNull {
buf.astPrintf(ct, " %s %s", keywordStrings[NOT], keywordStrings[NULL])
if ct.Options.Null != nil {
if *ct.Options.Null {
buf.astPrintf(ct, " %s", keywordStrings[NULL])
} else {
buf.astPrintf(ct, " %s %s", keywordStrings[NOT], keywordStrings[NULL])
}
}
if ct.Options.Default != nil {
buf.astPrintf(ct, " %s %v", keywordStrings[DEFAULT], ct.Options.Default)
Expand Down
15 changes: 10 additions & 5 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.

4 changes: 3 additions & 1 deletion go/vt/sqlparser/cached_size.go

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

14 changes: 14 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,12 @@ var (
}, {
input: "CREATE TABLE aipk (id INT AUTO_INCREMENT PRIMARY KEY)",
output: "create table aipk (\n\tid INT auto_increment primary key\n)",
}, {
// This test case is added because MySQL supports this behaviour.
// It allows the user to specify null and not null multiple times.
// The last value specified is used.
input: "create table foo (f timestamp null not null , g timestamp not null null)",
output: "create table foo (\n\tf timestamp not null,\n\tg timestamp null\n)",
}, {
input: "alter vschema create vindex hash_vdx using hash",
}, {
Expand Down Expand Up @@ -2569,6 +2575,14 @@ func TestCreateTable(t *testing.T) {
" col_multipolygon2 multipolygon not null\n" +
")",

// test null columns
"create table foo (\n" +
" id int primary key,\n" +
" a varchar(255) null,\n" +
" b varchar(255) null default 'foo',\n" +
" c timestamp null default current_timestamp()\n" +
")",

// test defining indexes separately
"create table t (\n" +
" id int auto_increment,\n" +
Expand Down
Loading

0 comments on commit 2fcfe29

Please sign in to comment.