From 71638ee4690bbc153347251bd2aa39759ff6002b Mon Sep 17 00:00:00 2001 From: Yiding Cui Date: Mon, 26 Jul 2021 17:50:16 +0800 Subject: [PATCH] planner: the UNION's merge type should exclude the pure NULL (#26561) --- 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 1c6c7fb8785a6..bc6a0d43120af 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -3996,3 +3996,11 @@ func (s *testIntegrationSerialSuite) TestIssue26214(c *C) { _, err := tk.Exec("select * from t where case when a < 0 then 1 else 2 end <= 1 order by 4;") c.Assert(core.ErrUnknownColumn.Equal(err), IsTrue) } + +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 414a095be0211..49d3b9193ac7b 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -1324,6 +1324,12 @@ func (b *PlanBuilder) buildDistinct(child LogicalPlan, length int) (*LogicalAggr // unionJoinFieldType finds the type which can carry the given types in Union. // Note that unionJoinFieldType doesn't handle charset and collation, caller need to handle it by itself. 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 {