Skip to content

Commit 7deedf8

Browse files
sourcelliuzz-jason
authored andcommitted
expression: fix issue that period_diff is not compatible with MySQL 8.0 (#10383)
1 parent 1880483 commit 7deedf8

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
@@ -5209,6 +5209,14 @@ func (b *builtinPeriodDiffSig) evalInt(row chunk.Row) (int64, bool, error) {
52095209
return 0, isNull, err
52105210
}
52115211

5212+
if !validPeriod(p1) {
5213+
return 0, false, errIncorrectArgs.GenWithStackByArgs("period_diff")
5214+
}
5215+
5216+
if !validPeriod(p2) {
5217+
return 0, false, errIncorrectArgs.GenWithStackByArgs("period_diff")
5218+
}
5219+
52125220
return int64(period2Month(uint64(p1)) - period2Month(uint64(p2))), false, nil
52135221
}
52145222

expression/builtin_time_test.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2403,19 +2403,24 @@ func (s *testEvaluatorSuite) TestPeriodDiff(c *C) {
24032403
}{
24042404
{201611, 201611, true, 0},
24052405
{200802, 200703, true, 11},
2406-
{0, 999999999, true, -120000086},
2407-
{9999999, 0, true, 1200086},
2408-
{411, 200413, true, -2},
2409-
{197000, 207700, true, -1284},
24102406
{201701, 201611, true, 2},
24112407
{201702, 201611, true, 3},
24122408
{201510, 201611, true, -13},
24132409
{201702, 1611, true, 3},
24142410
{197102, 7011, true, 3},
2415-
{12509, 12323, true, 10},
2416-
{12509, 12323, true, 10},
24172411
}
24182412

2413+
tests2 := []struct {
2414+
Period1 int64
2415+
Period2 int64
2416+
}{
2417+
{0, 999999999},
2418+
{9999999, 0},
2419+
{411, 200413},
2420+
{197000, 207700},
2421+
{12509, 12323},
2422+
{12509, 12323},
2423+
}
24192424
fc := funcs[ast.PeriodDiff]
24202425
for _, test := range tests {
24212426
period1 := types.NewIntDatum(test.Period1)
@@ -2433,6 +2438,18 @@ func (s *testEvaluatorSuite) TestPeriodDiff(c *C) {
24332438
value := result.GetInt64()
24342439
c.Assert(value, Equals, test.Expect)
24352440
}
2441+
2442+
for _, test := range tests2 {
2443+
period1 := types.NewIntDatum(test.Period1)
2444+
period2 := types.NewIntDatum(test.Period2)
2445+
f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Datum{period1, period2}))
2446+
c.Assert(err, IsNil)
2447+
c.Assert(f, NotNil)
2448+
_, err = evalBuiltinFunc(f, chunk.Row{})
2449+
c.Assert(err, NotNil)
2450+
c.Assert(err.Error(), Equals, "[expression:1210]Incorrect arguments to period_diff")
2451+
}
2452+
24362453
// nil
24372454
args := []types.Datum{types.NewDatum(nil), types.NewIntDatum(0)}
24382455
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
@@ -1523,10 +1523,16 @@ func (s *testIntegrationSuite) TestTimeBuiltin(c *C) {
15231523
}
15241524

15251525
// for period_diff
1526-
result = tk.MustQuery(`SELECT period_diff(191, 2), period_diff(191, -2), period_diff(0, 0), period_diff(191, 191);`)
1527-
result.Check(testkit.Rows("101 -2213609288845122103 0 0"))
1528-
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("", "");`)
1529-
result.Check(testkit.Rows("<nil> <nil> <nil> 10 10 0"))
1526+
result = tk.MustQuery(`SELECT period_diff(200807, 200705), period_diff(200807, 200908);`)
1527+
result.Check(testkit.Rows("14 -13"))
1528+
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");`)
1529+
result.Check(testkit.Rows("<nil> <nil> <nil> 10 1"))
1530+
for _, errPeriod := range []string{
1531+
"period_diff(-00013,1)", "period_diff(00013,1)", "period_diff(0, 0)", "period_diff(200013, 1)", "period_diff(5612, 4513)", "period_diff('', '')",
1532+
} {
1533+
err := tk.QueryToErr(fmt.Sprintf("SELECT %v;", errPeriod))
1534+
c.Assert(err.Error(), Equals, "[expression:1210]Incorrect arguments to period_diff")
1535+
}
15301536

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

0 commit comments

Comments
 (0)