Skip to content

Commit a84a1ee

Browse files
sourcelliuzz-jason
authored andcommitted
expression: fix issue that period_diff is not compatible with MySQL 8.0 (#10383) (#10501)
1 parent 329afda commit a84a1ee

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

expression/builtin_time.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4994,6 +4994,14 @@ func (b *builtinPeriodDiffSig) evalInt(row chunk.Row) (int64, bool, error) {
49944994
return 0, isNull, errors.Trace(err)
49954995
}
49964996

4997+
if !validPeriod(p1) {
4998+
return 0, false, errIncorrectArgs.GenWithStackByArgs("period_diff")
4999+
}
5000+
5001+
if !validPeriod(p2) {
5002+
return 0, false, errIncorrectArgs.GenWithStackByArgs("period_diff")
5003+
}
5004+
49975005
return int64(period2Month(uint64(p1)) - period2Month(uint64(p2))), false, nil
49985006
}
49995007

expression/builtin_time_test.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,19 +2351,24 @@ func (s *testEvaluatorSuite) TestPeriodDiff(c *C) {
23512351
}{
23522352
{201611, 201611, true, 0},
23532353
{200802, 200703, true, 11},
2354-
{0, 999999999, true, -120000086},
2355-
{9999999, 0, true, 1200086},
2356-
{411, 200413, true, -2},
2357-
{197000, 207700, true, -1284},
23582354
{201701, 201611, true, 2},
23592355
{201702, 201611, true, 3},
23602356
{201510, 201611, true, -13},
23612357
{201702, 1611, true, 3},
23622358
{197102, 7011, true, 3},
2363-
{12509, 12323, true, 10},
2364-
{12509, 12323, true, 10},
23652359
}
23662360

2361+
tests2 := []struct {
2362+
Period1 int64
2363+
Period2 int64
2364+
}{
2365+
{0, 999999999},
2366+
{9999999, 0},
2367+
{411, 200413},
2368+
{197000, 207700},
2369+
{12509, 12323},
2370+
{12509, 12323},
2371+
}
23672372
fc := funcs[ast.PeriodDiff]
23682373
for _, test := range tests {
23692374
period1 := types.NewIntDatum(test.Period1)
@@ -2381,6 +2386,18 @@ func (s *testEvaluatorSuite) TestPeriodDiff(c *C) {
23812386
value := result.GetInt64()
23822387
c.Assert(value, Equals, test.Expect)
23832388
}
2389+
2390+
for _, test := range tests2 {
2391+
period1 := types.NewIntDatum(test.Period1)
2392+
period2 := types.NewIntDatum(test.Period2)
2393+
f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Datum{period1, period2}))
2394+
c.Assert(err, IsNil)
2395+
c.Assert(f, NotNil)
2396+
_, err = evalBuiltinFunc(f, chunk.Row{})
2397+
c.Assert(err, NotNil)
2398+
c.Assert(err.Error(), Equals, "[expression:1210]Incorrect arguments to period_diff")
2399+
}
2400+
23842401
// nil
23852402
args := []types.Datum{types.NewDatum(nil), types.NewIntDatum(0)}
23862403
f, err := fc.getFunction(s.ctx, s.datumsToConstants(args))

expression/integration_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,10 +1462,16 @@ func (s *testIntegrationSuite) TestTimeBuiltin(c *C) {
14621462
}
14631463

14641464
// for period_diff
1465-
result = tk.MustQuery(`SELECT period_diff(191, 2), period_diff(191, -2), period_diff(0, 0), period_diff(191, 191);`)
1466-
result.Check(testkit.Rows("101 -2213609288845122103 0 0"))
1467-
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("", "");`)
1468-
result.Check(testkit.Rows("<nil> <nil> <nil> 10 10 0"))
1465+
result = tk.MustQuery(`SELECT period_diff(200807, 200705), period_diff(200807, 200908);`)
1466+
result.Check(testkit.Rows("14 -13"))
1467+
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");`)
1468+
result.Check(testkit.Rows("<nil> <nil> <nil> 10 1"))
1469+
for _, errPeriod := range []string{
1470+
"period_diff(-00013,1)", "period_diff(00013,1)", "period_diff(0, 0)", "period_diff(200013, 1)", "period_diff(5612, 4513)", "period_diff('', '')",
1471+
} {
1472+
err := tk.QueryToErr(fmt.Sprintf("SELECT %v;", errPeriod))
1473+
c.Assert(err.Error(), Equals, "[expression:1210]Incorrect arguments to period_diff")
1474+
}
14691475

14701476
// TODO: fix `CAST(xx as duration)` and release the test below:
14711477
// result = tk.MustQuery(`SELECT hour("aaa"), hour(123456), hour(1234567);`)

0 commit comments

Comments
 (0)