Skip to content

Commit

Permalink
Merge pull request #5901 from planetscale/master
Browse files Browse the repository at this point in the history
Do not drop leading zeroes in microsecond timestamps for prepared statements.
  • Loading branch information
deepthi authored Mar 9, 2020
2 parents 8a8f10a + 7ddd41a commit 22cae3d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
4 changes: 2 additions & 2 deletions go/mysql/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ func (c *Conn) parseStmtArgs(data []byte, typ querypb.Type, pos int) (sqltypes.V
strconv.Itoa(int(hour)) + ":" +
strconv.Itoa(int(minute)) + ":" +
strconv.Itoa(int(second)) + "." +
strconv.Itoa(int(microSecond))
fmt.Sprintf("%06d", microSecond)

return sqltypes.NewVarChar(val), pos, ok
case 0x07:
Expand Down Expand Up @@ -781,7 +781,7 @@ func (c *Conn) parseStmtArgs(data []byte, typ querypb.Type, pos int) (sqltypes.V
val += strconv.Itoa(int(hours)) + ":" +
strconv.Itoa(int(minute)) + ":" +
strconv.Itoa(int(second)) + "." +
strconv.Itoa(int(microSecond))
fmt.Sprintf("%06d", microSecond)

return sqltypes.NewVarChar(val), pos, ok
case 0x08:
Expand Down
14 changes: 9 additions & 5 deletions go/test/endtoend/preparestmt/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"strings"
"testing"
"time"

"vitess.io/vitess/go/test/endtoend/cluster"

Expand All @@ -32,9 +33,11 @@ import (

// tableData is a temporary structure to hold selected data.
type tableData struct {
Msg string
Data string
TextCol string
Msg string
Data string
TextCol string
DateTime time.Time
DateTimeMicros time.Time
}

// DBInfo information about the database.
Expand Down Expand Up @@ -119,6 +122,7 @@ var (
decimal_unsigned DECIMAL,
t_date DATE,
t_datetime DATETIME,
t_datetime_micros DATETIME(6),
t_time TIME,
t_timestamp TIMESTAMP,
c8 bit(8) DEFAULT NULL,
Expand Down Expand Up @@ -255,7 +259,7 @@ func execErr(dbo *sql.DB, stmt string, params ...interface{}) *mysql.MySQLError
func selectWhere(t *testing.T, dbo *sql.DB, where string, params ...interface{}) []tableData {
var out []tableData
// prepare query
qry := "SELECT msg, data, text_col FROM " + tableName
qry := "SELECT msg, data, text_col, t_datetime, t_datetime_micros FROM " + tableName
if where != "" {
qry += " WHERE (" + where + ")"
}
Expand All @@ -267,7 +271,7 @@ func selectWhere(t *testing.T, dbo *sql.DB, where string, params ...interface{})
// prepare result
for r.Next() {
var t tableData
r.Scan(&t.Msg, &t.Data, &t.TextCol)
r.Scan(&t.Msg, &t.Data, &t.TextCol, &t.DateTime, &t.DateTimeMicros)
out = append(out, t)
}
return out
Expand Down
26 changes: 18 additions & 8 deletions go/test/endtoend/preparestmt/stmt_methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,24 @@ func TestInsertUpdateDelete(t *testing.T) {
dbo := Connect(t)
defer dbo.Close()
// prepare insert statement
insertStmt := `insert into ` + tableName + ` values( ?, ?, ?, ?, ?, ?, ?,
insertStmt := `insert into ` + tableName + ` values( ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`

textValue := fake.FullName()
largeComment := fake.Paragraph()

location, _ := time.LoadLocation("Local")
// inserting multiple rows into test table
for i := 1; i <= 100; i++ {
// preparing value for the insert testing
insertValue := []interface{}{
i, fmt.Sprint(i) + "21", i * 100,
127, 1, 32767, 8388607, 2147483647, 2.55, 64.9, 55.5,
time.Date(2009, 5, 5, 0, 0, 0, 0, time.UTC),
time.Date(2009, 5, 5, 0, 0, 0, 0, time.UTC),
time.Date(2009, 5, 5, 0, 0, 0, 50000, time.UTC),
time.Date(2009, 5, 5, 0, 0, 0, 50000, location),
time.Date(2009, 5, 5, 0, 0, 0, 50000, location),
time.Now(),
time.Date(2009, 5, 5, 0, 0, 0, 0, time.UTC),
time.Date(2009, 5, 5, 0, 0, 0, 50000, time.UTC),
1, 1, 1, 1, 1, 1, 1, 1, 1, jsonExample, textValue, largeComment,
}
exec(t, dbo, insertStmt, insertValue...)
Expand All @@ -75,6 +77,13 @@ func TestInsertUpdateDelete(t *testing.T) {
// validate value of msg column in data
assert.Equal(t, fmt.Sprintf("%d21", testingID), data[0].Msg)

// Validate a datetime field (without micros)
// The 50 microsecs we inserted should have been truncated
assert.Equal(t, time.Date(2009, 5, 5, 0, 0, 0, 0, location), data[0].DateTime)

// Validate a datetime field (with micros)
assert.Equal(t, time.Date(2009, 5, 5, 0, 0, 0, 50000, location), data[0].DateTimeMicros)

// testing record update
updateRecord(t, dbo)

Expand Down Expand Up @@ -108,16 +117,17 @@ func TestAutoIncColumns(t *testing.T) {
insertStmt := "INSERT INTO " + tableName + ` (
msg,keyspace_id,tinyint_unsigned,bool_signed,smallint_unsigned,
mediumint_unsigned,int_unsigned,float_unsigned,double_unsigned,
decimal_unsigned,t_date,t_datetime,t_time,t_timestamp,c8,c16,c24,
decimal_unsigned,t_date,t_datetime,t_datetime_micros,t_time,t_timestamp,c8,c16,c24,
c32,c40,c48,c56,c63,c64,json_col,text_col,data) VALUES (?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`
insertValue := []interface{}{
"21", 0,
127, 1, 32767, 8388607, 2147483647, 2.55, 64.9, 55.5,
time.Date(2009, 5, 5, 0, 0, 0, 0, time.UTC),
time.Date(2009, 5, 5, 0, 0, 0, 0, time.UTC),
time.Date(2009, 5, 5, 0, 0, 0, 50000, time.UTC),
time.Date(2009, 5, 5, 0, 0, 0, 50000, time.UTC),
time.Date(2009, 5, 5, 0, 0, 0, 50000, time.UTC),
time.Now(),
time.Date(2009, 5, 5, 0, 0, 0, 0, time.UTC),
time.Date(2009, 5, 5, 0, 0, 0, 50000, time.UTC),
1, 1, 1, 1, 1, 1, 1, 1, 1, jsonExample, fake.DomainName(), fake.Paragraph(),
}

Expand Down

0 comments on commit 22cae3d

Please sign in to comment.