diff --git a/go/mysql/query.go b/go/mysql/query.go index 89dfb5c8c98..8d340a1e6cd 100644 --- a/go/mysql/query.go +++ b/go/mysql/query.go @@ -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: @@ -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: diff --git a/go/test/endtoend/preparestmt/main_test.go b/go/test/endtoend/preparestmt/main_test.go index 082871658a9..1fa24bf62a3 100644 --- a/go/test/endtoend/preparestmt/main_test.go +++ b/go/test/endtoend/preparestmt/main_test.go @@ -23,6 +23,7 @@ import ( "os" "strings" "testing" + "time" "vitess.io/vitess/go/test/endtoend/cluster" @@ -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. @@ -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, @@ -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 + ")" } @@ -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 diff --git a/go/test/endtoend/preparestmt/stmt_methods_test.go b/go/test/endtoend/preparestmt/stmt_methods_test.go index 20c9c97dba7..d631b904068 100644 --- a/go/test/endtoend/preparestmt/stmt_methods_test.go +++ b/go/test/endtoend/preparestmt/stmt_methods_test.go @@ -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...) @@ -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) @@ -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(), }