Skip to content

Commit

Permalink
Merge branch 'master' into txn-stat-table
Browse files Browse the repository at this point in the history
  • Loading branch information
longfangsong committed May 12, 2021
2 parents 388d0b6 + 3eedd40 commit 1015efc
Show file tree
Hide file tree
Showing 91 changed files with 1,562 additions and 714 deletions.
6 changes: 3 additions & 3 deletions cmd/explaintest/r/explain_easy.result
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ HashJoin 9990.00 root inner join, equal:[eq(test.t1.c1, test.t2.c2)]
└─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo
explain format = 'brief' select (select count(1) k from t1 s where s.c1 = t1.c1 having k != 0) from t1;
id estRows task access object operator info
Projection 10000.00 root ifnull(Column#10, 0)->Column#10
└─MergeJoin 10000.00 root left outer join, left key:test.t1.c1, right key:test.t1.c1
├─Projection(Build) 8000.00 root 1->Column#10, test.t1.c1
Projection 12500.00 root ifnull(Column#10, 0)->Column#10
└─MergeJoin 12500.00 root left outer join, left key:test.t1.c1, right key:test.t1.c1
├─Projection(Build) 10000.00 root 1->Column#10, test.t1.c1
│ └─TableReader 10000.00 root data:TableFullScan
│ └─TableFullScan 10000.00 cop[tikv] table:s keep order:true, stats:pseudo
└─TableReader(Probe) 10000.00 root data:TableFullScan
Expand Down
188 changes: 143 additions & 45 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4801,52 +4801,9 @@ func (s *testSerialDBSuite) TestModifyColumnCharset(c *C) {

}

func (s *testDBSuite1) TestModifyColumnTime(c *C) {
limit := variable.GetDDLErrorCountLimit()
variable.SetDDLErrorCountLimit(3)

tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test_db")
enableChangeColumnType := tk.Se.GetSessionVars().EnableChangeColumnType
tk.Se.GetSessionVars().EnableChangeColumnType = true

// Set time zone to UTC.
originalTz := tk.Se.GetSessionVars().TimeZone
tk.Se.GetSessionVars().TimeZone = time.UTC
defer func() {
variable.SetDDLErrorCountLimit(limit)
tk.Se.GetSessionVars().EnableChangeColumnType = enableChangeColumnType
tk.Se.GetSessionVars().TimeZone = originalTz
}()

now := time.Now().UTC()
now = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
timeToDate1 := now.Format("2006-01-02")
timeToDate2 := now.AddDate(0, 0, 30).Format("2006-01-02")

timeToDatetime1 := now.Add(20 * time.Hour).Add(12 * time.Second).Format("2006-01-02 15:04:05")
timeToDatetime2 := now.Add(20 * time.Hour).Format("2006-01-02 15:04:05")
timeToDatetime3 := now.Add(12 * time.Second).Format("2006-01-02 15:04:05")
timeToDatetime4 := now.AddDate(0, 0, 30).Add(20 * time.Hour).Add(12 * time.Second).Format("2006-01-02 15:04:05")
timeToDatetime5 := now.AddDate(0, 0, 30).Add(20 * time.Hour).Format("2006-01-02 15:04:05")

timeToTimestamp1 := now.Add(20 * time.Hour).Add(12 * time.Second).Format("2006-01-02 15:04:05")
timeToTimestamp2 := now.Add(20 * time.Hour).Format("2006-01-02 15:04:05")
timeToTimestamp3 := now.Add(12 * time.Second).Format("2006-01-02 15:04:05")
timeToTimestamp4 := now.AddDate(0, 0, 30).Add(20 * time.Hour).Add(12 * time.Second).Format("2006-01-02 15:04:05")
timeToTimestamp5 := now.AddDate(0, 0, 30).Add(20 * time.Hour).Format("2006-01-02 15:04:05")
func (s *testDBSuite1) TestModifyColumnTime_TimeToYear(c *C) {
currentYear := strconv.Itoa(time.Now().Year())

// 1. In conversion between date/time, fraction parts are taken into account
// Refer to doc: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-type-conversion.html
// 2. Failed tests are commentd to pass unit-test
tests := []struct {
from string
value string
to string
expect string
err uint16
}{
tests := []testModifyColumnTimeCase{
// time to year, it's reasonable to return current year and discard the time (even if MySQL may get data out of range error).
{"time", `"30 20:00:12"`, "year", currentYear, 0},
{"time", `"30 20:00"`, "year", currentYear, 0},
Expand All @@ -4862,7 +4819,16 @@ func (s *testDBSuite1) TestModifyColumnTime(c *C) {
{"time", `"20:00:12.498"`, "year", currentYear, 0},
{"time", `"200012.498"`, "year", currentYear, 0},
{"time", `200012.498`, "year", currentYear, 0},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_TimeToDate(c *C) {
now := time.Now().UTC()
now = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
timeToDate1 := now.Format("2006-01-02")
timeToDate2 := now.AddDate(0, 0, 30).Format("2006-01-02")
tests := []testModifyColumnTimeCase{
// time to date
{"time", `"30 20:00:12"`, "date", timeToDate2, 0},
{"time", `"30 20:00"`, "date", timeToDate2, 0},
Expand All @@ -4878,7 +4844,19 @@ func (s *testDBSuite1) TestModifyColumnTime(c *C) {
{"time", `"20:00:12.498"`, "date", timeToDate1, 0},
{"time", `"200012.498"`, "date", timeToDate1, 0},
{"time", `200012.498`, "date", timeToDate1, 0},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_TimeToDatetime(c *C) {
now := time.Now().UTC()
now = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
timeToDatetime1 := now.Add(20 * time.Hour).Add(12 * time.Second).Format("2006-01-02 15:04:05")
timeToDatetime2 := now.Add(20 * time.Hour).Format("2006-01-02 15:04:05")
timeToDatetime3 := now.Add(12 * time.Second).Format("2006-01-02 15:04:05")
timeToDatetime4 := now.AddDate(0, 0, 30).Add(20 * time.Hour).Add(12 * time.Second).Format("2006-01-02 15:04:05")
timeToDatetime5 := now.AddDate(0, 0, 30).Add(20 * time.Hour).Format("2006-01-02 15:04:05")
tests := []testModifyColumnTimeCase{
// time to datetime
{"time", `"30 20:00:12"`, "datetime", timeToDatetime4, 0},
{"time", `"30 20:00"`, "datetime", timeToDatetime5, 0},
Expand All @@ -4894,7 +4872,19 @@ func (s *testDBSuite1) TestModifyColumnTime(c *C) {
{"time", `"20:00:12.498"`, "datetime", timeToDatetime1, 0},
{"time", `"200012.498"`, "datetime", timeToDatetime1, 0},
{"time", `200012.498`, "datetime", timeToDatetime1, 0},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_TimeToTimestamp(c *C) {
now := time.Now().UTC()
now = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
timeToTimestamp1 := now.Add(20 * time.Hour).Add(12 * time.Second).Format("2006-01-02 15:04:05")
timeToTimestamp2 := now.Add(20 * time.Hour).Format("2006-01-02 15:04:05")
timeToTimestamp3 := now.Add(12 * time.Second).Format("2006-01-02 15:04:05")
timeToTimestamp4 := now.AddDate(0, 0, 30).Add(20 * time.Hour).Add(12 * time.Second).Format("2006-01-02 15:04:05")
timeToTimestamp5 := now.AddDate(0, 0, 30).Add(20 * time.Hour).Format("2006-01-02 15:04:05")
tests := []testModifyColumnTimeCase{
// time to timestamp
{"time", `"30 20:00:12"`, "timestamp", timeToTimestamp4, 0},
{"time", `"30 20:00"`, "timestamp", timeToTimestamp5, 0},
Expand All @@ -4910,39 +4900,64 @@ func (s *testDBSuite1) TestModifyColumnTime(c *C) {
{"time", `"20:00:12.498"`, "timestamp", timeToTimestamp1, 0},
{"time", `"200012.498"`, "timestamp", timeToTimestamp1, 0},
{"time", `200012.498`, "timestamp", timeToTimestamp1, 0},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_DateToTime(c *C) {
tests := []testModifyColumnTimeCase{
// date to time
{"date", `"2019-01-02"`, "time", "00:00:00", 0},
{"date", `"19-01-02"`, "time", "00:00:00", 0},
{"date", `"20190102"`, "time", "00:00:00", 0},
{"date", `"190102"`, "time", "00:00:00", 0},
{"date", `20190102`, "time", "00:00:00", 0},
{"date", `190102`, "time", "00:00:00", 0},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_DateToYear(c *C) {
tests := []testModifyColumnTimeCase{
// date to year
{"date", `"2019-01-02"`, "year", "2019", 0},
{"date", `"19-01-02"`, "year", "2019", 0},
{"date", `"20190102"`, "year", "2019", 0},
{"date", `"190102"`, "year", "2019", 0},
{"date", `20190102`, "year", "2019", 0},
{"date", `190102`, "year", "2019", 0},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_DateToDatetime(c *C) {
tests := []testModifyColumnTimeCase{
// date to datetime
{"date", `"2019-01-02"`, "datetime", "2019-01-02 00:00:00", 0},
{"date", `"19-01-02"`, "datetime", "2019-01-02 00:00:00", 0},
{"date", `"20190102"`, "datetime", "2019-01-02 00:00:00", 0},
{"date", `"190102"`, "datetime", "2019-01-02 00:00:00", 0},
{"date", `20190102`, "datetime", "2019-01-02 00:00:00", 0},
{"date", `190102`, "datetime", "2019-01-02 00:00:00", 0},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_DateToTimestamp(c *C) {
tests := []testModifyColumnTimeCase{
// date to timestamp
{"date", `"2019-01-02"`, "timestamp", "2019-01-02 00:00:00", 0},
{"date", `"19-01-02"`, "timestamp", "2019-01-02 00:00:00", 0},
{"date", `"20190102"`, "timestamp", "2019-01-02 00:00:00", 0},
{"date", `"190102"`, "timestamp", "2019-01-02 00:00:00", 0},
{"date", `20190102`, "timestamp", "2019-01-02 00:00:00", 0},
{"date", `190102`, "timestamp", "2019-01-02 00:00:00", 0},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_TimestampToYear(c *C) {
tests := []testModifyColumnTimeCase{
// timestamp to year
{"timestamp", `"2006-01-02 15:04:05"`, "year", "2006", 0},
{"timestamp", `"06-01-02 15:04:05"`, "year", "2006", 0},
Expand All @@ -4951,7 +4966,12 @@ func (s *testDBSuite1) TestModifyColumnTime(c *C) {
{"timestamp", `20060102150405`, "year", "2006", 0},
{"timestamp", `060102150405`, "year", "2006", 0},
{"timestamp", `"2006-01-02 23:59:59.506"`, "year", "2006", 0},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_TimestampToTime(c *C) {
tests := []testModifyColumnTimeCase{
// timestamp to time
{"timestamp", `"2006-01-02 15:04:05"`, "time", "15:04:05", 0},
{"timestamp", `"06-01-02 15:04:05"`, "time", "15:04:05", 0},
Expand All @@ -4960,7 +4980,12 @@ func (s *testDBSuite1) TestModifyColumnTime(c *C) {
{"timestamp", `20060102150405`, "time", "15:04:05", 0},
{"timestamp", `060102150405`, "time", "15:04:05", 0},
{"timestamp", `"2006-01-02 23:59:59.506"`, "time", "00:00:00", 0},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_TimestampToDate(c *C) {
tests := []testModifyColumnTimeCase{
// timestamp to date
{"timestamp", `"2006-01-02 15:04:05"`, "date", "2006-01-02", 0},
{"timestamp", `"06-01-02 15:04:05"`, "date", "2006-01-02", 0},
Expand All @@ -4969,7 +4994,12 @@ func (s *testDBSuite1) TestModifyColumnTime(c *C) {
{"timestamp", `20060102150405`, "date", "2006-01-02", 0},
{"timestamp", `060102150405`, "date", "2006-01-02", 0},
{"timestamp", `"2006-01-02 23:59:59.506"`, "date", "2006-01-03", 0},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_TimestampToDatetime(c *C) {
tests := []testModifyColumnTimeCase{
// timestamp to datetime
{"timestamp", `"2006-01-02 15:04:05"`, "datetime", "2006-01-02 15:04:05", 0},
{"timestamp", `"06-01-02 15:04:05"`, "datetime", "2006-01-02 15:04:05", 0},
Expand All @@ -4978,7 +5008,12 @@ func (s *testDBSuite1) TestModifyColumnTime(c *C) {
{"timestamp", `20060102150405`, "datetime", "2006-01-02 15:04:05", 0},
{"timestamp", `060102150405`, "datetime", "2006-01-02 15:04:05", 0},
{"timestamp", `"2006-01-02 23:59:59.506"`, "datetime", "2006-01-03 00:00:00", 0},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_DatetimeToYear(c *C) {
tests := []testModifyColumnTimeCase{
// datetime to year
{"datetime", `"2006-01-02 15:04:05"`, "year", "2006", 0},
{"datetime", `"06-01-02 15:04:05"`, "year", "2006", 0},
Expand All @@ -4990,7 +5025,12 @@ func (s *testDBSuite1) TestModifyColumnTime(c *C) {
// MySQL will get "Data truncation: Out of range value for column 'a' at row 1.
{"datetime", `"1000-01-02 23:59:59"`, "year", "", errno.ErrInvalidYear},
{"datetime", `"9999-01-02 23:59:59"`, "year", "", errno.ErrInvalidYear},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_DatetimeToTime(c *C) {
tests := []testModifyColumnTimeCase{
// datetime to time
{"datetime", `"2006-01-02 15:04:05"`, "time", "15:04:05", 0},
{"datetime", `"06-01-02 15:04:05"`, "time", "15:04:05", 0},
Expand All @@ -5001,7 +5041,12 @@ func (s *testDBSuite1) TestModifyColumnTime(c *C) {
{"datetime", `"2006-01-02 23:59:59.506"`, "time", "00:00:00", 0},
{"datetime", `"1000-01-02 23:59:59"`, "time", "23:59:59", 0},
{"datetime", `"9999-01-02 23:59:59"`, "time", "23:59:59", 0},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_DatetimeToDate(c *C) {
tests := []testModifyColumnTimeCase{
// datetime to date
{"datetime", `"2006-01-02 15:04:05"`, "date", "2006-01-02", 0},
{"datetime", `"06-01-02 15:04:05"`, "date", "2006-01-02", 0},
Expand All @@ -5012,7 +5057,12 @@ func (s *testDBSuite1) TestModifyColumnTime(c *C) {
{"datetime", `"2006-01-02 23:59:59.506"`, "date", "2006-01-03", 0},
{"datetime", `"1000-01-02 23:59:59"`, "date", "1000-01-02", 0},
{"datetime", `"9999-01-02 23:59:59"`, "date", "9999-01-02", 0},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_DatetimeToTimestamp(c *C) {
tests := []testModifyColumnTimeCase{
// datetime to timestamp
{"datetime", `"2006-01-02 15:04:05"`, "timestamp", "2006-01-02 15:04:05", 0},
{"datetime", `"06-01-02 15:04:05"`, "timestamp", "2006-01-02 15:04:05", 0},
Expand All @@ -5023,7 +5073,12 @@ func (s *testDBSuite1) TestModifyColumnTime(c *C) {
{"datetime", `"2006-01-02 23:59:59.506"`, "timestamp", "2006-01-03 00:00:00", 0},
{"datetime", `"1000-01-02 23:59:59"`, "timestamp", "1000-01-02 23:59:59", 0},
{"datetime", `"9999-01-02 23:59:59"`, "timestamp", "9999-01-02 23:59:59", 0},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_YearToTime(c *C) {
tests := []testModifyColumnTimeCase{
// year to time
// failed cases are not handled by TiDB
{"year", `"2019"`, "time", "00:20:19", 0},
Expand All @@ -5036,7 +5091,12 @@ func (s *testDBSuite1) TestModifyColumnTime(c *C) {
{"year", `69`, "time", "", errno.ErrTruncatedWrongValue},
{"year", `70`, "time", "", errno.ErrTruncatedWrongValue},
{"year", `99`, "time", "", errno.ErrTruncatedWrongValue},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_YearToDate(c *C) {
tests := []testModifyColumnTimeCase{
// year to date
{"year", `"2019"`, "date", "", errno.ErrTruncatedWrongValue},
{"year", `2019`, "date", "", errno.ErrTruncatedWrongValue},
Expand All @@ -5049,7 +5109,12 @@ func (s *testDBSuite1) TestModifyColumnTime(c *C) {
{"year", `69`, "date", "", errno.ErrTruncatedWrongValue},
{"year", `70`, "date", "", errno.ErrTruncatedWrongValue},
{"year", `99`, "date", "", errno.ErrTruncatedWrongValue},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_YearToDatetime(c *C) {
tests := []testModifyColumnTimeCase{
// year to datetime
{"year", `"2019"`, "datetime", "", errno.ErrTruncatedWrongValue},
{"year", `2019`, "datetime", "", errno.ErrTruncatedWrongValue},
Expand All @@ -5062,7 +5127,12 @@ func (s *testDBSuite1) TestModifyColumnTime(c *C) {
{"year", `69`, "datetime", "", errno.ErrTruncatedWrongValue},
{"year", `70`, "datetime", "", errno.ErrTruncatedWrongValue},
{"year", `99`, "datetime", "", errno.ErrTruncatedWrongValue},
}
testModifyColumnTime(c, s.store, tests)
}

func (s *testDBSuite1) TestModifyColumnTime_YearToTimestamp(c *C) {
tests := []testModifyColumnTimeCase{
// year to timestamp
{"year", `"2019"`, "timestamp", "", errno.ErrTruncatedWrongValue},
{"year", `2019`, "timestamp", "", errno.ErrTruncatedWrongValue},
Expand All @@ -5076,6 +5146,34 @@ func (s *testDBSuite1) TestModifyColumnTime(c *C) {
{"year", `70`, "timestamp", "", errno.ErrTruncatedWrongValue},
{"year", `99`, "timestamp", "", errno.ErrTruncatedWrongValue},
}
testModifyColumnTime(c, s.store, tests)
}

type testModifyColumnTimeCase struct {
from string
value string
to string
expect string
err uint16
}

func testModifyColumnTime(c *C, store kv.Storage, tests []testModifyColumnTimeCase) {
limit := variable.GetDDLErrorCountLimit()
variable.SetDDLErrorCountLimit(3)

tk := testkit.NewTestKit(c, store)
tk.MustExec("use test_db")
enableChangeColumnType := tk.Se.GetSessionVars().EnableChangeColumnType
tk.Se.GetSessionVars().EnableChangeColumnType = true

// Set time zone to UTC.
originalTz := tk.Se.GetSessionVars().TimeZone
tk.Se.GetSessionVars().TimeZone = time.UTC
defer func() {
variable.SetDDLErrorCountLimit(limit)
tk.Se.GetSessionVars().EnableChangeColumnType = enableChangeColumnType
tk.Se.GetSessionVars().TimeZone = originalTz
}()

for _, t := range tests {
tk.MustExec("drop table if exists t_mc")
Expand Down
4 changes: 3 additions & 1 deletion ddl/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ func LoadGlobalVars(ctx sessionctx.Context, varNames []string) error {
for _, row := range rows {
varName := row.GetString(0)
varValue := row.GetString(1)
variable.SetLocalSystemVar(varName, varValue)
if err = ctx.GetSessionVars().SetSystemVar(varName, varValue); err != nil {
return err
}
}
}
return nil
Expand Down
7 changes: 3 additions & 4 deletions distsql/request_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,9 @@ func (builder *RequestBuilder) SetFromSessionVars(sv *variable.SessionVars) *Req
builder.Request.TaskID = sv.StmtCtx.TaskID
builder.Request.Priority = builder.getKVPriority(sv)
builder.Request.ReplicaRead = sv.GetReplicaRead()
if sv.SnapshotInfoschema != nil {
builder.Request.SchemaVar = infoschema.GetInfoSchemaBySessionVars(sv).SchemaMetaVersion()
} else {
builder.Request.SchemaVar = sv.TxnCtx.SchemaVersion
// in tests, it may be null
if is, ok := sv.GetInfoSchema().(infoschema.InfoSchema); ok {
builder.Request.SchemaVar = is.SchemaMetaVersion()
}
builder.txnScope = sv.TxnCtx.TxnScope
builder.IsStaleness = sv.TxnCtx.IsStaleness
Expand Down
Loading

0 comments on commit 1015efc

Please sign in to comment.