diff --git a/executor/ddl_test.go b/executor/ddl_test.go index 66c9c607bca9d..7a5860dcebb09 100644 --- a/executor/ddl_test.go +++ b/executor/ddl_test.go @@ -29,6 +29,7 @@ import ( "github.com/pingcap/tidb/ddl" ddlutil "github.com/pingcap/tidb/ddl/util" "github.com/pingcap/tidb/domain" + "github.com/pingcap/tidb/infoschema" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/meta" "github.com/pingcap/tidb/meta/autoid" @@ -224,6 +225,33 @@ func (s *testSuite3) TestCreateView(c *C) { // create view using prepare tk.MustExec(`prepare stmt from "create view v10 (x) as select 1";`) tk.MustExec("execute stmt") + + // create view on union + tk.MustExec("drop table if exists t1, t2") + tk.MustExec("drop view if exists v") + _, err = tk.Exec("create view v as select * from t1 union select * from t2") + c.Assert(terror.ErrorEqual(err, infoschema.ErrTableNotExists), IsTrue) + tk.MustExec("create table t1(a int, b int)") + tk.MustExec("create table t2(a int, b int)") + tk.MustExec("insert into t1 values(1,2), (1,1), (1,2)") + tk.MustExec("insert into t2 values(1,1),(1,3)") + tk.MustExec("create definer='root'@'localhost' view v as select * from t1 union select * from t2") + tk.MustQuery("select * from v").Sort().Check(testkit.Rows("1 1", "1 2", "1 3")) + tk.MustExec("alter table t1 drop column a") + _, err = tk.Exec("select * from v") + c.Assert(terror.ErrorEqual(err, plannercore.ErrViewInvalid), IsTrue) + tk.MustExec("alter table t1 add column a int") + tk.MustQuery("select * from v").Sort().Check(testkit.Rows("1 1", "1 3", " 1", " 2")) + tk.MustExec("alter table t1 drop column a") + tk.MustExec("alter table t2 drop column b") + _, err = tk.Exec("select * from v") + c.Assert(terror.ErrorEqual(err, plannercore.ErrViewInvalid), IsTrue) + tk.MustExec("drop view v") + + tk.MustExec("create view v as (select * from t1)") + tk.MustExec("drop view v") + tk.MustExec("create view v as (select * from t1 union select * from t2)") + tk.MustExec("drop view v") } func (s *testSuite3) TestCreateDropDatabase(c *C) { diff --git a/executor/executor_test.go b/executor/executor_test.go index 01bad6b60decb..246e58cf714d4 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -3609,11 +3609,11 @@ func (s *testSuite) TestSelectView(c *C) { tk.MustExec("drop table view_t;") tk.MustExec("create table view_t(c int,d int)") err := tk.ExecToErr("select * from view1") - c.Assert(err.Error(), Equals, "[planner:1054]Unknown column 'test.view_t.a' in 'field list'") + c.Assert(err.Error(), Equals, "[planner:1356]View 'test.view1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them") err = tk.ExecToErr("select * from view2") - c.Assert(err.Error(), Equals, "[planner:1054]Unknown column 'test.view_t.a' in 'field list'") + c.Assert(err.Error(), Equals, "[planner:1356]View 'test.view2' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them") err = tk.ExecToErr("select * from view3") - c.Assert(err.Error(), Equals, "[planner:1054]Unknown column 'a' in 'field list'") + c.Assert(err.Error(), Equals, plannercore.ErrViewInvalid.GenWithStackByArgs("test", "view3").Error()) tk.MustExec("drop table view_t;") tk.MustExec("create table view_t(a int,b int,c int)") tk.MustExec("insert into view_t values(1,2,3)") diff --git a/go.sum b/go.sum index 9b40b36d696e7..b7d5184fcc063 100644 --- a/go.sum +++ b/go.sum @@ -189,7 +189,6 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d h1:GoAlyOgbOEIFd github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/remyoudompheng/bigfft v0.0.0-20190512091148-babf20351dd7 h1:FUL3b97ZY2EPqg2NbXKuMHs5pXJB9hjj1fDHnF2vl28= github.com/remyoudompheng/bigfft v0.0.0-20190512091148-babf20351dd7/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= -github.com/sergi/go-diff v1.0.1-0.20180205163309-da645544ed44 h1:tB9NOR21++IjLyVx3/PCPhWMwqGNCMQEH96A6dMZ/gc= github.com/sergi/go-diff v1.0.1-0.20180205163309-da645544ed44/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/shirou/gopsutil v2.18.10+incompatible h1:cy84jW6EVRPa5g9HAHrlbxMSIjBhDSX0OFYyMYminYs= github.com/shirou/gopsutil v2.18.10+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 3f2ba830175cb..e2434021df74b 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -2356,6 +2356,7 @@ func (b *PlanBuilder) BuildDataSourceFromView(ctx context.Context, dbName model. b.visitInfo = make([]visitInfo, 0) selectLogicalPlan, err := b.Build(ctx, selectNode) if err != nil { + err = ErrViewInvalid.GenWithStackByArgs(dbName.O, tableInfo.Name.O) return nil, err }