diff --git a/executor/executor_test.go b/executor/executor_test.go index 012d7ccbfd593..0ed65eeea26c3 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -7107,3 +7107,12 @@ func (s *testSerialSuite1) TestIssue24210(c *C) { err = failpoint.Disable("github.com/pingcap/tidb/executor/mockSelectionExecBaseExecutorOpenReturnedError") c.Assert(err, IsNil) } + +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", "")) +} diff --git a/expression/builtin_compare.go b/expression/builtin_compare.go index 8223cdfa0e12c..b50089dc1fc40 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 }