From f3791518d1660a988df83d86ed882ce4c4550463 Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Thu, 18 Mar 2021 19:01:36 +0800 Subject: [PATCH] expression: do not adjust int when it is null and compared year (#22821) (#22844) --- executor/executor_test.go | 9 +++++++++ expression/builtin_compare.go | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/executor/executor_test.go b/executor/executor_test.go index e310abc017c96..0e77b813485c1 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -6454,6 +6454,15 @@ func (s *testSuite) TestIssue20305(c *C) { tk.MustQuery("SELECT * FROM `t3` where y <= a").Check(testkit.Rows("2155 2156")) } +func (s *testSuite) TestIssue22817(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t3") + tk.MustExec("create table t3 (a year)") + tk.MustExec("insert into t3 values (1991), (\"1992\"), (\"93\"), (94)") + tk.MustQuery("select * from t3 where a >= NULL").Check(testkit.Rows()) +} + func (s *testSuite) TestIssue13953(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") diff --git a/expression/builtin_compare.go b/expression/builtin_compare.go index 671158a5e0138..72b71de7ac5ca 100644 --- a/expression/builtin_compare.go +++ b/expression/builtin_compare.go @@ -1373,7 +1373,7 @@ func (c *compareFunctionClass) refineArgs(ctx sessionctx.Context, args []Express } } // int constant [cmp] year type - if arg0IsCon && arg0IsInt && arg1Type.Tp == mysql.TypeYear { + if arg0IsCon && arg0IsInt && arg1Type.Tp == mysql.TypeYear && !arg0.Value.IsNull() { adjusted, failed := types.AdjustYear(arg0.Value.GetInt64(), false) if failed == nil { arg0.Value.SetInt64(adjusted) @@ -1381,7 +1381,7 @@ func (c *compareFunctionClass) refineArgs(ctx sessionctx.Context, args []Express } } // year type [cmp] int constant - if arg1IsCon && arg1IsInt && arg0Type.Tp == mysql.TypeYear { + if arg1IsCon && arg1IsInt && arg0Type.Tp == mysql.TypeYear && !arg1.Value.IsNull() { adjusted, failed := types.AdjustYear(arg1.Value.GetInt64(), false) if failed == nil { arg1.Value.SetInt64(adjusted)