Skip to content

Commit

Permalink
planner: Keep view always select non-local-temporary table (#26087)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcwangchao authored Jul 13, 2021
1 parent 118671d commit 276a669
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
30 changes: 30 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3922,3 +3922,33 @@ func (s *testIntegrationSerialSuite) TestMergeContinuousSelections(c *C) {
res.Check(testkit.Rows(output[i].Plan...))
}
}

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"))

}
12 changes: 11 additions & 1 deletion planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3803,7 +3803,17 @@ func (b *PlanBuilder) buildDataSource(ctx context.Context, tn *ast.TableName, as
dbName = model.NewCIStr(sessionVars.CurrentDB)
}

tbl, err := b.is.TableByName(dbName, tn.Name)
is := b.is
if len(b.buildingViewStack) > 0 {
if tempIs, ok := is.(*infoschema.TemporaryTableAttachedInfoSchema); ok {
// For tables in view, always ignore local temporary table, considering the below case:
// If a user created a normal table `t1` and a view `v1` referring `t1`, and then a local temporary table with a same name `t1` is created.
// At this time, executing 'select * from v1' should still return all records from normal table `t1` instead of temporary table `t1`.
is = tempIs.InfoSchema
}
}

tbl, err := is.TableByName(dbName, tn.Name)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 276a669

Please sign in to comment.