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

Nullable Timestamp Column Fix #7740

Merged
merged 5 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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)",
GuptaManan100 marked this conversation as resolved.
Show resolved Hide resolved
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