From 4f919b07f8f013e48521894c70cb69004f43c622 Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Mon, 26 Jul 2021 18:50:15 +0800 Subject: [PATCH] planner: the UNION's merge type should exclude the pure NULL (#26561) (#26570) --- planner/core/integration_test.go | 8 ++++++++ planner/core/logical_plan_builder.go | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index 0a5add83a625d..4957c29223fbb 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -2087,3 +2087,11 @@ func (s *testIntegrationSuite) TestIssue23846(c *C) { tk.MustExec("insert into t values(0x00A4EEF4FA55D6706ED5)") tk.MustQuery("select * from t where a=0x00A4EEF4FA55D6706ED5").Check(testkit.Rows("\x00\xa4\xee\xf4\xfaU\xd6pn\xd5")) // not empty } + +func (s *testIntegrationSuite) TestIssue26559(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("create table t(a timestamp, b datetime);") + tk.MustExec("insert into t values('2020-07-29 09:07:01', '2020-07-27 16:57:36');") + tk.MustQuery("select greatest(a, b) from t union select null;").Sort().Check(testkit.Rows("2020-07-29 09:07:01", "")) +} diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 8e96587568562..76566d93ac61a 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -1266,6 +1266,12 @@ func (b *PlanBuilder) buildDistinct(child LogicalPlan, length int) (*LogicalAggr // unionJoinFieldType finds the type which can carry the given types in Union. func unionJoinFieldType(a, b *types.FieldType) *types.FieldType { + // We ignore the pure NULL type. + if a.Tp == mysql.TypeNull { + return b + } else if b.Tp == mysql.TypeNull { + return a + } resultTp := types.NewFieldType(types.MergeFieldType(a.Tp, b.Tp)) // This logic will be intelligible when it is associated with the buildProjection4Union logic. if resultTp.Tp == mysql.TypeNewDecimal {