From 2f7241138adfc434f4b199ecae68fb83ad33b36b Mon Sep 17 00:00:00 2001 From: mantuliu Date: Mon, 6 May 2019 00:29:20 +0800 Subject: [PATCH 1/2] modify `desc` is compatible with MySQL --- executor/seqtest/seq_executor_test.go | 2 +- table/column.go | 4 +++- table/column_test.go | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/executor/seqtest/seq_executor_test.go b/executor/seqtest/seq_executor_test.go index e6b3cf3727647..d739b9657d2b7 100644 --- a/executor/seqtest/seq_executor_test.go +++ b/executor/seqtest/seq_executor_test.go @@ -586,7 +586,7 @@ func (s *seqTestSuite) TestShow(c *C) { "c5|varchar(6)|YES||'C6'|", "c6|enum('s','m','l','xl')|YES||xl|", "c7|set('a','b','c','d')|YES||a,c,c|", - "c8|datetime|YES||CURRENT_TIMESTAMP|on update CURRENT_TIMESTAMP", + "c8|datetime|YES||CURRENT_TIMESTAMP|DEFAULT_GENERATED on update CURRENT_TIMESTAMP", "c9|year(4)|YES||2014|", )) } diff --git a/table/column.go b/table/column.go index 0c05e823f0d1f..ce9fb65454673 100644 --- a/table/column.go +++ b/table/column.go @@ -262,7 +262,9 @@ func NewColDesc(col *Column) *ColDesc { if mysql.HasAutoIncrementFlag(col.Flag) { extra = "auto_increment" } else if mysql.HasOnUpdateNowFlag(col.Flag) { - extra = "on update CURRENT_TIMESTAMP" + //in order to match the rules of mysql 8.0.16 version + //see https://github.com/pingcap/tidb/issues/10337 + extra = "DEFAULT_GENERATED on update CURRENT_TIMESTAMP" } else if col.IsGenerated() { if col.GeneratedStored { extra = "STORED GENERATED" diff --git a/table/column_test.go b/table/column_test.go index 363b0efa36cee..f77db4f7925ab 100644 --- a/table/column_test.go +++ b/table/column_test.go @@ -136,7 +136,7 @@ func (t *testTableSuite) TestDesc(c *C) { NewColDesc(col) col.Flag = mysql.UniqueKeyFlag | mysql.OnUpdateNowFlag desc := NewColDesc(col) - c.Assert(desc.Extra, Equals, "on update CURRENT_TIMESTAMP") + c.Assert(desc.Extra, Equals, "DEFAULT_GENERATED on update CURRENT_TIMESTAMP") col.Flag = 0 col.GeneratedExprString = "test" col.GeneratedStored = true From f1338ef36c0850eb68d7122d0433290d5a4baaf0 Mon Sep 17 00:00:00 2001 From: mantuliu Date: Wed, 8 May 2019 01:04:20 +0800 Subject: [PATCH 2/2] fix the issue:Function `period_diff ` is not compatible with MySQL 8.0 --- expression/builtin_time.go | 13 +++++++++++++ expression/builtin_time_test.go | 29 +++++++++++++++++++++++------ expression/integration_test.go | 14 ++++++++++---- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/expression/builtin_time.go b/expression/builtin_time.go index 6a442b8d2d08a..79dfaad32a7f2 100644 --- a/expression/builtin_time.go +++ b/expression/builtin_time.go @@ -4734,6 +4734,11 @@ func (c *periodAddFunctionClass) getFunction(ctx sessionctx.Context, args []Expr return sig, nil } +func validPeriod(p int64) bool { + mod := p % 100 + return !(p < 0 || mod > 12 || mod == 0) +} + // period2Month converts a period to months, in which period is represented in the format of YYMM or YYYYMM. // Note that the period argument is not a date value. func period2Month(period uint64) uint64 { @@ -4835,6 +4840,14 @@ func (b *builtinPeriodDiffSig) evalInt(row chunk.Row) (int64, bool, error) { return 0, isNull, err } + if !validPeriod(p1) { + return 0, false, errIncorrectArgs.GenWithStackByArgs("period_diff") + } + + if !validPeriod(p2) { + return 0, false, errIncorrectArgs.GenWithStackByArgs("period_diff") + } + return int64(period2Month(uint64(p1)) - period2Month(uint64(p2))), false, nil } diff --git a/expression/builtin_time_test.go b/expression/builtin_time_test.go index dda5dbfc9d100..e7ccc2615f7b0 100644 --- a/expression/builtin_time_test.go +++ b/expression/builtin_time_test.go @@ -2375,19 +2375,24 @@ func (s *testEvaluatorSuite) TestPeriodDiff(c *C) { }{ {201611, 201611, true, 0}, {200802, 200703, true, 11}, - {0, 999999999, true, -120000086}, - {9999999, 0, true, 1200086}, - {411, 200413, true, -2}, - {197000, 207700, true, -1284}, {201701, 201611, true, 2}, {201702, 201611, true, 3}, {201510, 201611, true, -13}, {201702, 1611, true, 3}, {197102, 7011, true, 3}, - {12509, 12323, true, 10}, - {12509, 12323, true, 10}, } + tests2 := []struct { + Period1 int64 + Period2 int64 + }{ + {0, 999999999}, + {9999999, 0}, + {411, 200413}, + {197000, 207700}, + {12509, 12323}, + {12509, 12323}, + } fc := funcs[ast.PeriodDiff] for _, test := range tests { period1 := types.NewIntDatum(test.Period1) @@ -2405,6 +2410,18 @@ func (s *testEvaluatorSuite) TestPeriodDiff(c *C) { value := result.GetInt64() c.Assert(value, Equals, test.Expect) } + + for _, test := range tests2 { + period1 := types.NewIntDatum(test.Period1) + period2 := types.NewIntDatum(test.Period2) + f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Datum{period1, period2})) + c.Assert(err, IsNil) + c.Assert(f, NotNil) + _, err = evalBuiltinFunc(f, chunk.Row{}) + c.Assert(err, NotNil) + c.Assert(err.Error(), Equals, "[expression:1210]Incorrect arguments to period_diff") + } + // nil args := []types.Datum{types.NewDatum(nil), types.NewIntDatum(0)} f, err := fc.getFunction(s.ctx, s.datumsToConstants(args)) diff --git a/expression/integration_test.go b/expression/integration_test.go index ed9c94ae73d23..420d025f7fec7 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -1517,10 +1517,16 @@ func (s *testIntegrationSuite) TestTimeBuiltin(c *C) { result.Check(testkit.Rows(" 200010 200208 0")) // for period_diff - result = tk.MustQuery(`SELECT period_diff(191, 2), period_diff(191, -2), period_diff(0, 0), period_diff(191, 191);`) - result.Check(testkit.Rows("101 -2213609288845122103 0 0")) - result = tk.MustQuery(`SELECT period_diff(NULL, 2), period_diff(-191, NULL), period_diff(NULL, NULL), period_diff(12.09, 2), period_diff("21aa", "11aa"), period_diff("", "");`) - result.Check(testkit.Rows(" 10 10 0")) + result = tk.MustQuery(`SELECT period_diff(200807, 200705), period_diff(200807, 200908);`) + result.Check(testkit.Rows("14 -13")) + result = tk.MustQuery(`SELECT period_diff(NULL, 2), period_diff(-191, NULL), period_diff(NULL, NULL), period_diff(12.09, 2), period_diff("12aa", "11aa");`) + result.Check(testkit.Rows(" 10 1")) + for _, errPeriod := range []string{ + "period_diff(-00013,1)", "period_diff(00013,1)", "period_diff(0, 0)", "period_diff(200013, 1)", "period_diff(5612, 4513)", "period_diff('', '')", + } { + err := tk.QueryToErr(fmt.Sprintf("SELECT %v;", errPeriod)) + c.Assert(err.Error(), Equals, "[expression:1210]Incorrect arguments to period_diff") + } // TODO: fix `CAST(xx as duration)` and release the test below: // result = tk.MustQuery(`SELECT hour("aaa"), hour(123456), hour(1234567);`)