Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: Fix the 'Unknown column' error when select from view in another database (#15621) #15866

Merged
merged 4 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
resolve conflicts
  • Loading branch information
XuHuaiyu committed Apr 2, 2020
commit 291582a26e32284cf792dbff484ff94d4a6663a7
66 changes: 0 additions & 66 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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...)
}
Expand Down
10 changes: 1 addition & 9 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down