From 499093cdcb94a23c353a8897a719aaf0ff29b47b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E6=96=B9=E6=B7=9E?= Date: Mon, 30 Mar 2020 21:03:38 +0800 Subject: [PATCH 1/2] cherry pick #15621 to release-3.0 Signed-off-by: sre-bot --- ddl/db_test.go | 77 ++++++++++++++++++++++++++++ planner/core/logical_plan_builder.go | 8 +++ 2 files changed, 85 insertions(+) diff --git a/ddl/db_test.go b/ddl/db_test.go index cbfeaaf226161..79f517b9ead6f 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -66,7 +66,11 @@ var _ = Suite(&testDBSuite4{&testDBSuite{}}) var _ = Suite(&testDBSuite5{&testDBSuite{}}) var _ = Suite(&testDBSuite6{&testDBSuite{}}) var _ = Suite(&testDBSuite7{&testDBSuite{}}) +<<<<<<< HEAD var _ = Suite(&testDBSuite8{&testDBSuite{}}) +======= +var _ = SerialSuites(&testSerialDBSuite{&testDBSuite{}}) +>>>>>>> d0ca931... planner: Fix the 'Unknown column' error when select from view… (#15621) const defaultBatchSize = 1024 @@ -134,7 +138,11 @@ type testDBSuite4 struct{ *testDBSuite } type testDBSuite5 struct{ *testDBSuite } type testDBSuite6 struct{ *testDBSuite } type testDBSuite7 struct{ *testDBSuite } +<<<<<<< HEAD type testDBSuite8 struct{ *testDBSuite } +======= +type testSerialDBSuite struct{ *testDBSuite } +>>>>>>> d0ca931... planner: Fix the 'Unknown column' error when select from view… (#15621) func (s *testDBSuite6) TestAddIndexWithPK(c *C) { s.tk = testkit.NewTestKit(c, s.store) @@ -1850,6 +1858,75 @@ func (s *testDBSuite4) TestChangeColumn(c *C) { s.tk.MustExec("drop table t3") } +<<<<<<< HEAD +======= +func (s *testDBSuite5) TestRenameColumn(c *C) { + s.tk = testkit.NewTestKit(c, s.store) + s.tk.MustExec("use " + s.schemaName) + + assertColNames := func(tableName string, colNames ...string) { + cols := s.testGetTable(c, tableName).Cols() + c.Assert(len(cols), Equals, len(colNames), Commentf("number of columns mismatch")) + for i := range cols { + c.Assert(cols[i].Name.L, Equals, strings.ToLower(colNames[i])) + } + } + + s.mustExec(c, "create table test_rename_column (id int not null primary key auto_increment, col1 int)") + s.mustExec(c, "alter table test_rename_column rename column col1 to col1") + assertColNames("test_rename_column", "id", "col1") + s.mustExec(c, "alter table test_rename_column rename column col1 to col2") + assertColNames("test_rename_column", "id", "col2") + + // Test renaming non-exist columns. + s.tk.MustGetErrCode("alter table test_rename_column rename column non_exist_col to col3", errno.ErrBadField) + + // Test renaming to an exist column. + s.tk.MustGetErrCode("alter table test_rename_column rename column col2 to id", errno.ErrDupFieldName) + + // Test renaming the column with foreign key. + s.tk.MustExec("drop table test_rename_column") + s.tk.MustExec("create table test_rename_column_base (base int)") + s.tk.MustExec("create table test_rename_column (col int, foreign key (col) references test_rename_column_base(base))") + + s.tk.MustGetErrCode("alter table test_rename_column rename column col to col1", errno.ErrFKIncompatibleColumns) + + s.tk.MustExec("drop table test_rename_column_base") + + // Test renaming generated columns. + s.tk.MustExec("drop table test_rename_column") + s.tk.MustExec("create table test_rename_column (id int, col1 int generated always as (id + 1))") + + s.mustExec(c, "alter table test_rename_column rename column col1 to col2") + assertColNames("test_rename_column", "id", "col2") + s.mustExec(c, "alter table test_rename_column rename column col2 to col1") + assertColNames("test_rename_column", "id", "col1") + s.tk.MustGetErrCode("alter table test_rename_column rename column id to id1", errno.ErrBadField) + + // Test renaming view columns. + s.tk.MustExec("drop table test_rename_column") + s.mustExec(c, "create table test_rename_column (id int, col1 int)") + s.mustExec(c, "create view test_rename_column_view as select * from test_rename_column") + + s.mustExec(c, "alter table test_rename_column rename column col1 to col2") + s.tk.MustGetErrCode("select * from test_rename_column_view", errno.ErrViewInvalid) + + s.mustExec(c, "drop view test_rename_column_view") + s.tk.MustExec("drop table test_rename_column") +} + +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") +} + +>>>>>>> d0ca931... planner: Fix the 'Unknown column' error when select from view… (#15621) 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 ef32bb07ac31d..44ae7031d59cf 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -2433,8 +2433,16 @@ func (b *PlanBuilder) buildProjUponView(ctx context.Context, dbName model.CIStr, OrigTblName: col.OrigTblName, ColName: columnInfo[i].Name, OrigColName: origColName, +<<<<<<< HEAD DBName: col.DBName, RetType: col.GetType(), +======= + DBName: dbName, + }) + projSchema.Append(&expression.Column{ + UniqueID: b.ctx.GetSessionVars().AllocPlanColumnID(), + RetType: cols[i].GetType(), +>>>>>>> d0ca931... planner: Fix the 'Unknown column' error when select from view… (#15621) }) projExprs = append(projExprs, col) } From 291582a26e32284cf792dbff484ff94d4a6663a7 Mon Sep 17 00:00:00 2001 From: xuhuaiyu <391585975@qq.com> Date: Thu, 2 Apr 2020 13:53:32 +0800 Subject: [PATCH 2/2] resolve conflicts --- ddl/db_test.go | 66 ---------------------------- planner/core/logical_plan_builder.go | 10 +---- 2 files changed, 1 insertion(+), 75 deletions(-) diff --git a/ddl/db_test.go b/ddl/db_test.go index 79f517b9ead6f..0d7362f2387d9 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -66,11 +66,7 @@ var _ = Suite(&testDBSuite4{&testDBSuite{}}) var _ = Suite(&testDBSuite5{&testDBSuite{}}) var _ = Suite(&testDBSuite6{&testDBSuite{}}) var _ = Suite(&testDBSuite7{&testDBSuite{}}) -<<<<<<< HEAD var _ = Suite(&testDBSuite8{&testDBSuite{}}) -======= -var _ = SerialSuites(&testSerialDBSuite{&testDBSuite{}}) ->>>>>>> d0ca931... planner: Fix the 'Unknown column' error when select from view… (#15621) const defaultBatchSize = 1024 @@ -138,11 +134,7 @@ type testDBSuite4 struct{ *testDBSuite } type testDBSuite5 struct{ *testDBSuite } type testDBSuite6 struct{ *testDBSuite } type testDBSuite7 struct{ *testDBSuite } -<<<<<<< HEAD type testDBSuite8 struct{ *testDBSuite } -======= -type testSerialDBSuite struct{ *testDBSuite } ->>>>>>> d0ca931... planner: Fix the 'Unknown column' error when select from view… (#15621) func (s *testDBSuite6) TestAddIndexWithPK(c *C) { s.tk = testkit.NewTestKit(c, s.store) @@ -1858,63 +1850,6 @@ func (s *testDBSuite4) TestChangeColumn(c *C) { s.tk.MustExec("drop table t3") } -<<<<<<< HEAD -======= -func (s *testDBSuite5) TestRenameColumn(c *C) { - s.tk = testkit.NewTestKit(c, s.store) - s.tk.MustExec("use " + s.schemaName) - - assertColNames := func(tableName string, colNames ...string) { - cols := s.testGetTable(c, tableName).Cols() - c.Assert(len(cols), Equals, len(colNames), Commentf("number of columns mismatch")) - for i := range cols { - c.Assert(cols[i].Name.L, Equals, strings.ToLower(colNames[i])) - } - } - - s.mustExec(c, "create table test_rename_column (id int not null primary key auto_increment, col1 int)") - s.mustExec(c, "alter table test_rename_column rename column col1 to col1") - assertColNames("test_rename_column", "id", "col1") - s.mustExec(c, "alter table test_rename_column rename column col1 to col2") - assertColNames("test_rename_column", "id", "col2") - - // Test renaming non-exist columns. - s.tk.MustGetErrCode("alter table test_rename_column rename column non_exist_col to col3", errno.ErrBadField) - - // Test renaming to an exist column. - s.tk.MustGetErrCode("alter table test_rename_column rename column col2 to id", errno.ErrDupFieldName) - - // Test renaming the column with foreign key. - s.tk.MustExec("drop table test_rename_column") - s.tk.MustExec("create table test_rename_column_base (base int)") - s.tk.MustExec("create table test_rename_column (col int, foreign key (col) references test_rename_column_base(base))") - - s.tk.MustGetErrCode("alter table test_rename_column rename column col to col1", errno.ErrFKIncompatibleColumns) - - s.tk.MustExec("drop table test_rename_column_base") - - // Test renaming generated columns. - s.tk.MustExec("drop table test_rename_column") - s.tk.MustExec("create table test_rename_column (id int, col1 int generated always as (id + 1))") - - s.mustExec(c, "alter table test_rename_column rename column col1 to col2") - assertColNames("test_rename_column", "id", "col2") - s.mustExec(c, "alter table test_rename_column rename column col2 to col1") - assertColNames("test_rename_column", "id", "col1") - s.tk.MustGetErrCode("alter table test_rename_column rename column id to id1", errno.ErrBadField) - - // Test renaming view columns. - s.tk.MustExec("drop table test_rename_column") - s.mustExec(c, "create table test_rename_column (id int, col1 int)") - s.mustExec(c, "create view test_rename_column_view as select * from test_rename_column") - - s.mustExec(c, "alter table test_rename_column rename column col1 to col2") - s.tk.MustGetErrCode("select * from test_rename_column_view", errno.ErrViewInvalid) - - s.mustExec(c, "drop view test_rename_column_view") - s.tk.MustExec("drop table test_rename_column") -} - func (s *testDBSuite7) TestSelectInViewFromAnotherDB(c *C) { _, _ = s.s.Execute(context.Background(), "create database test_db2") s.tk = testkit.NewTestKit(c, s.store) @@ -1926,7 +1861,6 @@ func (s *testDBSuite7) TestSelectInViewFromAnotherDB(c *C) { s.tk.MustExec("select test_db2.v.a from test_db2.v") } ->>>>>>> d0ca931... planner: Fix the 'Unknown column' error when select from view… (#15621) 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 44ae7031d59cf..1afd0eacf3bfc 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -2433,16 +2433,8 @@ func (b *PlanBuilder) buildProjUponView(ctx context.Context, dbName model.CIStr, OrigTblName: col.OrigTblName, ColName: columnInfo[i].Name, OrigColName: origColName, -<<<<<<< HEAD - DBName: col.DBName, - RetType: col.GetType(), -======= DBName: dbName, - }) - projSchema.Append(&expression.Column{ - UniqueID: b.ctx.GetSessionVars().AllocPlanColumnID(), - RetType: cols[i].GetType(), ->>>>>>> d0ca931... planner: Fix the 'Unknown column' error when select from view… (#15621) + RetType: col.GetType(), }) projExprs = append(projExprs, col) }