diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index ac2d3617dfb63..0c7685aae48d8 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -3901,3 +3901,70 @@ func (s *testIntegrationSerialSuite) TestMergeContinuousSelections(c *C) { res.Check(testkit.Rows(output[i].Plan...)) } } +<<<<<<< HEAD +======= + +func (s *testIntegrationSerialSuite) TestSelectIgnoreTemporaryTableInView(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + + tk.Se.Auth(&auth.UserIdentity{Username: "root", Hostname: "localhost", CurrentUser: true, AuthUsername: "root", AuthHostname: "%"}, nil, []byte("012345678901234567890")) + tk.MustExec("set @@tidb_enable_noop_functions=1") + tk.MustExec("create table t1 (a int, b int)") + tk.MustExec("create table t2 (c int, d int)") + tk.MustExec("create view v1 as select * from t1 order by a") + tk.MustExec("create view v2 as select * from ((select * from t1) union (select * from t2)) as tt order by a, b") + tk.MustExec("create view v3 as select * from v1 order by a") + tk.MustExec("create view v4 as select * from t1, t2 where t1.a = t2.c order by a, b") + tk.MustExec("create view v5 as select * from (select * from t1) as t1 order by a") + + tk.MustExec("insert into t1 values (1, 2), (3, 4)") + tk.MustExec("insert into t2 values (3, 5), (6, 7)") + + tk.MustExec("create temporary table t1 (a int, b int)") + tk.MustExec("create temporary table t2 (c int, d int)") + tk.MustQuery("select * from t1").Check(testkit.Rows()) + tk.MustQuery("select * from t2").Check(testkit.Rows()) + + tk.MustQuery("select * from v1").Check(testkit.Rows("1 2", "3 4")) + tk.MustQuery("select * from v2").Check(testkit.Rows("1 2", "3 4", "3 5", "6 7")) + tk.MustQuery("select * from v3").Check(testkit.Rows("1 2", "3 4")) + tk.MustQuery("select * from v4").Check(testkit.Rows("3 4 3 5")) + tk.MustQuery("select * from v5").Check(testkit.Rows("1 2", "3 4")) + +} + +func (s *testIntegrationSerialSuite) TestIssue26250(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("create table tp (id int primary key) partition by range (id) (partition p0 values less than (100));") + tk.MustExec("create table tn (id int primary key);") + tk.MustExec("insert into tp values(1),(2);") + tk.MustExec("insert into tn values(1),(2);") + tk.MustQuery("select * from tp,tn where tp.id=tn.id and tn.id=1 for update;").Check(testkit.Rows("1 1")) +} + +// https://github.com/pingcap/tidb/issues/26214 +func (s *testIntegrationSerialSuite) TestIssue26214(c *C) { + originalVal := config.GetGlobalConfig().Experimental.AllowsExpressionIndex + config.GetGlobalConfig().Experimental.AllowsExpressionIndex = true + defer func() { + config.GetGlobalConfig().Experimental.AllowsExpressionIndex = originalVal + }() + + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table `t` (`a` int(11) default null, `b` int(11) default null, `c` int(11) default null, key `expression_index` ((case when `a` < 0 then 1 else 2 end)))") + _, 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", "")) +} +>>>>>>> 71638ee46... planner: the UNION's merge type should exclude the pure NULL (#26561) diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 6f58d7aca1e2b..59c8fb5089e11 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -1322,6 +1322,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 {