diff --git a/ddl/db_test.go b/ddl/db_test.go index cbfeaaf226161..0d7362f2387d9 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -1850,6 +1850,17 @@ func (s *testDBSuite4) TestChangeColumn(c *C) { s.tk.MustExec("drop table t3") } +func (s *testDBSuite7) TestSelectInViewFromAnotherDB(c *C) { + _, _ = s.s.Execute(context.Background(), "create database test_db2") + s.tk = testkit.NewTestKit(c, s.store) + s.tk.MustExec("use " + s.schemaName) + s.tk.MustExec("create table t(a int)") + s.tk.MustExec("use test_db2") + s.tk.MustExec("create sql security invoker view v as select * from " + s.schemaName + ".t") + s.tk.MustExec("use " + s.schemaName) + s.tk.MustExec("select test_db2.v.a from test_db2.v") +} + func (s *testDBSuite) mustExec(c *C, query string, args ...interface{}) { s.tk.MustExec(query, args...) } diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 6c4a334965b20..8015f6e2f8782 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -2433,7 +2433,7 @@ func (b *PlanBuilder) buildProjUponView(ctx context.Context, dbName model.CIStr, OrigTblName: col.OrigTblName, ColName: columnInfo[i].Name, OrigColName: origColName, - DBName: col.DBName, + DBName: dbName, RetType: col.GetType(), }) projExprs = append(projExprs, col)