From 12ca87955b80d8cb931f367deeb69e16810435d6 Mon Sep 17 00:00:00 2001 From: HuaiyuXu <391585975@qq.com> Date: Mon, 26 Jul 2021 17:04:16 +0800 Subject: [PATCH 1/2] cherry pick #26533 to release-5.1 Signed-off-by: ti-srebot --- executor/executor_test.go | 24 ++++++++++++++++++++++++ expression/builtin_compare.go | 15 +++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/executor/executor_test.go b/executor/executor_test.go index b5c75f917f40a..d84b2511056a6 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -8710,3 +8710,27 @@ func (s *testSuite) TestEmptyTableSampleTemporaryTable(c *C) { rs.Check(testkit.Rows()) tk.MustExec("commit") } +<<<<<<< HEAD +======= + +func (s *testSuite) TestIssue25506(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists tbl_3, tbl_23") + tk.MustExec("create table tbl_3 (col_15 bit(20))") + tk.MustExec("insert into tbl_3 values (0xFFFF)") + tk.MustExec("insert into tbl_3 values (0xFF)") + tk.MustExec("create table tbl_23 (col_15 bit(15))") + tk.MustExec("insert into tbl_23 values (0xF)") + tk.MustQuery("(select col_15 from tbl_23) union all (select col_15 from tbl_3 for update) order by col_15").Check(testkit.Rows("\x00\x00\x0F", "\x00\x00\xFF", "\x00\xFF\xFF")) +} + +func (s *testSuite) TestIssue26532(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustQuery("select greatest(cast(\"2020-01-01 01:01:01\" as datetime), cast(\"2019-01-01 01:01:01\" as datetime) )union select null;").Sort().Check(testkit.Rows("2020-01-01 01:01:01", "")) + tk.MustQuery("select least(cast(\"2020-01-01 01:01:01\" as datetime), cast(\"2019-01-01 01:01:01\" as datetime) )union select null;").Sort().Check(testkit.Rows("2019-01-01 01:01:01", "")) + tk.MustQuery("select greatest(\"2020-01-01 01:01:01\" ,\"2019-01-01 01:01:01\" )union select null;").Sort().Check(testkit.Rows("2020-01-01 01:01:01", "")) + tk.MustQuery("select least(\"2020-01-01 01:01:01\" , \"2019-01-01 01:01:01\" )union select null;").Sort().Check(testkit.Rows("2019-01-01 01:01:01", "")) +} +>>>>>>> dd2fadb19... expression, executor: fix type infer for greatest/leastest(datetime) (#26533) diff --git a/expression/builtin_compare.go b/expression/builtin_compare.go index 30de86394d7f9..5c9367004ec89 100644 --- a/expression/builtin_compare.go +++ b/expression/builtin_compare.go @@ -485,9 +485,23 @@ func (c *greatestFunctionClass) getFunction(ctx sessionctx.Context, args []Expre sig = &builtinGreatestTimeSig{bf} sig.setPbCode(tipb.ScalarFuncSig_GreatestTime) } + sig.getRetTp().Flen, sig.getRetTp().Decimal = fixFlenAndDecimalForGreatestAndLeast(args) return sig, nil } +func fixFlenAndDecimalForGreatestAndLeast(args []Expression) (flen, decimal int) { + for _, arg := range args { + argFlen, argDecimal := arg.GetType().Flen, arg.GetType().Decimal + if argFlen > flen { + flen = argFlen + } + if argDecimal > decimal { + decimal = argDecimal + } + } + return flen, decimal +} + type builtinGreatestIntSig struct { baseBuiltinFunc } @@ -702,6 +716,7 @@ func (c *leastFunctionClass) getFunction(ctx sessionctx.Context, args []Expressi sig = &builtinLeastTimeSig{bf} sig.setPbCode(tipb.ScalarFuncSig_LeastTime) } + sig.getRetTp().Flen, sig.getRetTp().Decimal = fixFlenAndDecimalForGreatestAndLeast(args) return sig, nil } From 481f2da104b7061060f0e906ad4e69eb12ec78dd Mon Sep 17 00:00:00 2001 From: xuhuaiyu <391585975@qq.com> Date: Mon, 26 Jul 2021 17:15:26 +0800 Subject: [PATCH 2/2] resolve conflicts --- executor/executor_test.go | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/executor/executor_test.go b/executor/executor_test.go index d84b2511056a6..5e3acc586b567 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -8710,20 +8710,6 @@ func (s *testSuite) TestEmptyTableSampleTemporaryTable(c *C) { rs.Check(testkit.Rows()) tk.MustExec("commit") } -<<<<<<< HEAD -======= - -func (s *testSuite) TestIssue25506(c *C) { - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") - tk.MustExec("drop table if exists tbl_3, tbl_23") - tk.MustExec("create table tbl_3 (col_15 bit(20))") - tk.MustExec("insert into tbl_3 values (0xFFFF)") - tk.MustExec("insert into tbl_3 values (0xFF)") - tk.MustExec("create table tbl_23 (col_15 bit(15))") - tk.MustExec("insert into tbl_23 values (0xF)") - tk.MustQuery("(select col_15 from tbl_23) union all (select col_15 from tbl_3 for update) order by col_15").Check(testkit.Rows("\x00\x00\x0F", "\x00\x00\xFF", "\x00\xFF\xFF")) -} func (s *testSuite) TestIssue26532(c *C) { tk := testkit.NewTestKit(c, s.store) @@ -8733,4 +8719,3 @@ func (s *testSuite) TestIssue26532(c *C) { tk.MustQuery("select greatest(\"2020-01-01 01:01:01\" ,\"2019-01-01 01:01:01\" )union select null;").Sort().Check(testkit.Rows("2020-01-01 01:01:01", "")) tk.MustQuery("select least(\"2020-01-01 01:01:01\" , \"2019-01-01 01:01:01\" )union select null;").Sort().Check(testkit.Rows("2019-01-01 01:01:01", "")) } ->>>>>>> dd2fadb19... expression, executor: fix type infer for greatest/leastest(datetime) (#26533)