diff --git a/ddl/db_test.go b/ddl/db_test.go index 87b339b3ec82f..657bb8ddb4315 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -1854,6 +1854,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 2f418e3636a16..2847dbe0fe1ef 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -2566,7 +2566,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)