go-mysql-server
This is a short tutorial about how to make and test changes in Dolt and its Go MySQL engine. We're going to work through fixing and testing a recent bug that was filed against Dolt. In the bug report, a user is using RMariaDB, a database connector library for R, and is seeing some values of certain timestamps fail to insert. The server is returning an error:
1
time = data.frame(time = as.POSIXct("2014-01-01 19:00:00"))
2
try(dbWriteTable(dolt_conn, "time", time, overwrite = TRUE))
3
#> Error : value "2014-1-2" can't be converted to time.Time [1105]
Copied!

Setup

Follow these setup instructions.

Reproduce the Bug

Let's see if we can make progress on reproducing the bug. Here, we just attempt to insert the same string that appears in the error message into a timestamp column:
1
~/dolt_workspace $ mkdir test_db
2
~/dolt_workspace $ cd test_db
3
~/dolt_workspace/test_db $ dolt init
4
Successfully initialized dolt data repository.
5
~/dolt_workspace/test_db $ dolt sql
6
# Welcome to the DoltSQL shell.
7
# Statements must be terminated with ';'.
8
# "exit" or "quit" (or Ctrl-D) to exit.
9
test_db> create table has_timestamp (t timestamp);
10
test_db> insert into has_timestamp values ('2014-1-2');
11
value "2014-1-2" can't be converted to time.Time
Copied!
Brilliant, we've managed to reproduce the bug. Let's track down where that error is coming from.

Tracking Down the Error

Hopefully this isn't too hard:
1
$ ~/dolt_workspace $ grep -R "can't be converted to time.Time"
2
[email protected] dolt_workspace % grep -R "can\'t be converted to time.Time" .
3
./go-mysql-server/benchmark/metadata.go: // TODO: value "1996-01-02" can't be converted to time.Time
4
./go-mysql-server/benchmark/metadata.go: // TODO: value "1996-03-13" can't be converted to time.Time
5
./go-mysql-server/benchmark/metadata.go: // TODO: value "1996-03-13" can't be converted to time.Time
6
./go-mysql-server/benchmark/metadata.go: // TODO: value "1996-03-13" can't be converted to time.Time
7
./go-mysql-server/sql/datetimetype.go: ErrConvertingToTime = errors.NewKind("value %q can't be converted to time.Time")
Copied!
so we can open go-mysql-server/sql/datetimetype.go in an editor and look for uses of ErrConvertingToTime. We quickly find the only place it is returned:
1
func (t datetimeType) ConvertWithoutRangeCheck(v interface{}) (time.Time, error) {
2
var res time.Time
3
switch value := v.(type) {
4
case string:
5
if value == zeroDateStr || value == zeroTimestampDatetimeStr {
6
return zeroTime, nil
7
}
8
parsed := false
9
for _, fmt := range TimestampDatetimeLayouts {
10
if t, err := time.Parse(fmt, value); err == nil {
11
res = t.UTC()
12
parsed = true
13
break
14
}
15
}
16
if !parsed {
17
return zeroTime, ErrConvertingToTime.New(v)
18
}
Copied!
and going to the definition of TimestampDatetimeLayouts, we find:
1
// TimestampDatetimeLayouts hold extra timestamps allowed for parsing. It does
2
// not have all the layouts supported by mysql. Missing are two digit year
3
// versions of common cases and dates that use non common separators.
4
//
5
// https://github.com/MariaDB/server/blob/mysql-5.5.36/sql-common/my_time.c#L124
6
TimestampDatetimeLayouts = []string{
7
"2006-01-02 15:04:05.999999",
8
"2006-01-02",
9
"2006-1-2 15:4:5.999999",
10
time.RFC3339,
11
time.RFC3339Nano,
12
"2006-01-02T15:04:05",
13
"20060102150405",
14
"20060102",
15
"2006/01/02",
16
"2006-01-02 15:04:05.999999999 -0700 MST", // represents standard Time.time.UTC()
17
}
Copied!
Notably present is a "2006-1-2 15:4:5.999999", which looks like it might handle single-digit months and days in a date that comes with a timestamp. But there is no corresponding "2006-1-2" in that list, which would possibly handle the case we're trying to fix. Let's start by looking for a unit test we could write that would demonstrate the bug in this ConvertWithoutRangeCheck function. We open up datetimetype_test.go, and look through the top level functions:
1
func TestDatetimeCompare(t *testing.T) {
2
func TestDatetimeCreate(t *testing.T) {
3
func TestDatetimeCreateInvalidBaseTypes(t *testing.T) {
4
func TestDatetimeConvert(t *testing.T) {
5
func TestDatetimeString(t *testing.T) {
Copied!
for our purposes, TestDatetimeConvert looks the most promising, and digging into the implementation, it's a table driven test with a pretty straight forward structure of giving an input to Convert and asserting a matching output and error value. We can just add tests for each of the three handled time types:
1
diff --git a/sql/datetimetype_test.go b/sql/datetimetype_test.go
2
index d9551262..a3d385a9 100644
3
--- a/sql/datetimetype_test.go
4
+++ b/sql/datetimetype_test.go
5
@@ -150,6 +150,9 @@ func TestDatetimeConvert(t *testing.T) {
6
{Date, time.Date(2012, 12, 12, 12, 12, 12, 12, time.UTC),
7
time.Date(2012, 12, 12, 0, 0, 0, 0, time.UTC), false},
8
{Date, "2010-06-03", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false},
9
+ {Date, "2010-6-3", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false},
10
+ {Date, "2010-6-03", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false},
11
+ {Date, "2010-06-3", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false},
12
{Date, "2010-06-03 12:12:12", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false},
13
{Date, "2010-06-03 12:12:12.000012", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false},
14
{Date, "2010-06-03T12:12:12Z", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false},
15
@@ -161,6 +164,9 @@ func TestDatetimeConvert(t *testing.T) {
16
{Datetime, time.Date(2012, 12, 12, 12, 12, 12, 12, time.UTC),
17
time.Date(2012, 12, 12, 12, 12, 12, 12, time.UTC), false},
18
{Datetime, "2010-06-03", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false},
19
+ {Datetime, "2010-6-3", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false},
20
+ {Datetime, "2010-06-3", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false},
21
+ {Datetime, "2010-6-03", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false},
22
{Datetime, "2010-06-03 12:12:12", time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC), false},
23
{Datetime, "2010-06-03 12:12:12.000012", time.Date(2010, 6, 3, 12, 12, 12, 12000, time.UTC), false},
24
{Datetime, "2010-06-03T12:12:12Z", time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC), false},
25
@@ -177,6 +183,9 @@ func TestDatetimeConvert(t *testing.T) {
26
{Timestamp, time.Date(2012, 12, 12, 12, 12, 12, 12, time.UTC),
27
time.Date(2012, 12, 12, 12, 12, 12, 12, time.UTC), false},
28
{Timestamp, "2010-06-03", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false},
29
+ {Timestamp, "2010-6-3", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false},
30
+ {Timestamp, "2010-6-03", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false},
31
+ {Timestamp, "2010-06-3", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false},
32
{Timestamp, "2010-06-03 12:12:12", time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC), false},
33
{Timestamp, "2010-06-03 12:12:12.000012", time.Date(2010, 6, 3, 12, 12, 12, 12000, time.UTC), false},
34
{Timestamp, "2010-06-03T12:12:12Z", time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC), false},
Copied!
and then try running the tests:
1
dolt_workspace/go-mysql-server/sql $ go test .
2
[email protected] sql % go test . -count 1
3
--- FAIL: TestDatetimeConvert (0.00s)
4
--- FAIL: TestDatetimeConvert/DATE_2010-6-3_2010-06-03_00:00:00_+0000_UTC (0.00s)
5
datetimetype_test.go:255:
6
Error Trace: datetimetype_test.go:255
7
Error: Received unexpected error:
8
value "2010-6-3" can't be converted to time.Time
9
...
10
--- FAIL: TestDatetimeConvert/DATE_2010-6-03_2010-06-03_00:00:00_+0000_UTC (0.00s)
11
datetimetype_test.go:255:
12
Error Trace: datetimetype_test.go:255
13
Error: Received unexpected error:
14
value "2010-6-03" can't be converted to time.Time
15
...
16
...
17
FAIL
18
FAIL github.com/dolthub/go-mysql-server/sql 0.181s
19
FAIL
Copied!
Perfect. Let's go ahead and see if we can fix it by adding the new pattern in TimestampDatetimeLayouts.
1
diff --git a/sql/datetimetype.go b/sql/datetimetype.go
2
index 2f00e324..f50b82af 100644
3
--- a/sql/datetimetype.go
4
+++ b/sql/datetimetype.go
5
@@ -62,6 +62,7 @@ var (
6
TimestampDatetimeLayouts = []string{
7
"2006-01-02 15:04:05.999999",
8
"2006-01-02",
9
+ "2006-1-2",
10
"2006-1-2 15:4:5.999999",
11
time.RFC3339,
12
time.RFC3339Nano,
Copied!
and test again:
1
dolt_workspace/go-mysql-server/sql $ go test .
2
ok github.com/dolthub/go-mysql-server/sql 0.327s
Copied!
Seems to work. So now we have a unit test demonstrating the problem and we've made that test pass. There's one other type of test we can add if we want to. The original bug was reported in the context of an insert, and it would be great to codify the fact that INSERT INTO table_with_datetime (timestamp) VALUES ("2006-6-3") should always work. For that kind of test, go-mysql-server has a directory of integration tests that lives at //enginetest. If we look around a little, we will find a file called insert_queries.go and we can look through that file for places where we're doing integration tests of inserts into datetime columns. Eventually, we might come with something like:
1
diff --git a/enginetest/insert_queries.go b/enginetest/insert_queries.go
2
index 5b88fad5..a4daf1f3 100644
3
--- a/enginetest/insert_queries.go
4
+++ b/enginetest/insert_queries.go
5
@@ -190,6 +190,12 @@ var InsertQueries = []WriteQueryTest{
6
SelectQuery: "SELECT * FROM typestable WHERE id = 999;",
7
ExpectedSelect: []sql.Row{{int64(999), nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil}},
8
},
9
+ {
10
+ WriteQuery: `INSERT INTO typestable (id, ti, da) VALUES (999, '2021-09-1', '2021-9-01');`,
11
+ ExpectedWriteResult: []sql.Row{{sql.NewOkResult(1)}},
12
+ SelectQuery: "SELECT id, ti, da FROM typestable WHERE id = 999;",
13
+ ExpectedSelect: []sql.Row{{int64(999), sql.MustConvert(sql.Timestamp.Convert("2021-09-01")), sql.MustConvert(sql.Date.Convert("2021-09-01"))}},
14
+ },
15
{
16
WriteQuery: `INSERT INTO typestable SET id=999, i8=null, i16=null, i32=null, i64=null, u8=null, u16=null, u32=null, u64=null,
17
f32=null, f64=null, ti=null, da=null, te=null, bo=null, js=null, bl=null;`,
Copied!
which hopefully captures the spirit of the behavior we want to verify. We can also verify that this fixes the behavior all the way at the dolt layer:
1
~/dolt_workspace/dolt/go $ go install ./cmd/dolt
2
~/dolt_workspace/dolt/go $ cd ../../test_db
3
~/dolt_workspace/test_db $ dolt sql
4
# Welcome to the DoltSQL shell.
5
# Statements must be terminated with ';'.
6
# "exit" or "quit" (or Ctrl-D) to exit.
7
test_db> insert into has_timestamp values ('2014-1-2');
8
Query OK, 1 row affected
Copied!

Committing and Opening a Pull Request

At this point, the bug is fixed in Dolt's SQL engine. Commit and push your changes through git.
1
~/dolt_workspace/go-mysql-server $ git commit -am 'Fix string to datetime convert of YYYY-M-D.'
2
[aaron/fix-2088-time-convert-bug 81dce573] Fix string to datetime convert of YYYY-M-D.
3
3 files changed, 16 insertions(+)
4
~/dolt_workspace/go-mysql-server $ git push origin aaron/fix-2088-time-convert-bug:aaron/fix-2088-time-convert-bug
5
Enumerating objects: 13, done.
6
Counting objects: 100% (13/13), done.
7
Delta compression using up to 8 threads
8
Compressing objects: 100% (3/3), done.
9
Writing objects: 100% (7/7), 803 bytes | 803.00 KiB/s, done.
10
Total 7 (delta 6), reused 4 (delta 4)
11
remote: Resolving deltas: 100% (6/6), completed with 6 local objects.
12
remote:
13
remote: Create a pull request for 'aaron/fix-2088-time-convert-bug' on GitHub by visiting:
14
remote: https://github.com/reltuk/go-mysql-server/pull/new/aaron/fix-2088-time-convert-bug
15
remote:
16
To github.com:reltuk/go-mysql-server.git
17
* [new branch] aaron/fix-2088-time-convert-bug -> aaron/fix-2088-time-convert-bug
Copied!
Finally, we just need to submit a pull request.