diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index 0bfd2719fe1ec..47589d104244d 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -3551,3 +3551,11 @@ func (s *testIntegrationSuite) TestIssue23839(c *C) { ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=2000001") tk.Exec("explain SELECT OUTR . col2 AS X FROM (SELECT INNR . col1 as col1, SUM( INNR . col2 ) as col2 FROM (SELECT INNR . `col_int_not_null` + 1 as col1, INNR . `pk` as col2 FROM BB AS INNR) AS INNR GROUP BY col1) AS OUTR2 INNER JOIN (SELECT INNR . col1 as col1, MAX( INNR . col2 ) as col2 FROM (SELECT INNR . `col_int_not_null` + 1 as col1, INNR . `pk` as col2 FROM BB AS INNR) AS INNR GROUP BY col1) AS OUTR ON OUTR2.col1 = OUTR.col1 GROUP BY OUTR . col1, OUTR2 . col1 HAVING X <> 'b'") } + +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 794f54d06a830..f3e8d7393a081 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -1292,6 +1292,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 {