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: Forbide create view on tmp table #26091

Merged
merged 18 commits into from
Jul 13, 2021
Merged
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
refactor
  • Loading branch information
lcwangchao committed Jul 13, 2021
commit 03a1ec453130cdcc9fd1bdf644eed00f3317409e
22 changes: 19 additions & 3 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
@@ -21,12 +21,10 @@ import (
"sync/atomic"
"time"

"github.com/pingcap/parser/auth"
"github.com/pingcap/tidb/planner/core"

. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/auth"
"github.com/pingcap/parser/charset"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
@@ -38,6 +36,7 @@ import (
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/stmtctx"
@@ -2893,6 +2892,23 @@ func (s *testIntegrationSuite3) TestAvoidCreateViewOnLocalTemporaryTable(c *C) {
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[schema:1146]Table 'test.v3' doesn't exist")

_, err = tk.Exec("create view v4 as select a, (select count(1) from tt1 where tt1.a = tt0.a) as tt1a from tt0")
c.Assert(core.ErrViewSelectTemporaryTable.Equal(err), IsTrue)
_, err = tk.Exec("select * from v4")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[schema:1146]Table 'test.v4' doesn't exist")

_, err = tk.Exec("create view v5 as select a, (select count(1) from tt1 where tt1.a = 1) as tt1a from tt0")
c.Assert(core.ErrViewSelectTemporaryTable.Equal(err), IsTrue)
_, err = tk.Exec("select * from v5")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[schema:1146]Table 'test.v5' doesn't exist")

_, err = tk.Exec("create view v6 as select * from tt0 where tt0.a=(select max(tt1.b) from tt1)")
c.Assert(core.ErrViewSelectTemporaryTable.Equal(err), IsTrue)
_, err = tk.Exec("select * from v6")
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[schema:1146]Table 'test.v6' doesn't exist")
}

checkCreateView()
4 changes: 4 additions & 0 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
@@ -3819,6 +3819,10 @@ func (b *PlanBuilder) buildDataSource(ctx context.Context, tn *ast.TableName, as
}

tableInfo := tbl.Meta()
if b.isCreateView && tableInfo.TempTableType == model.TempTableLocal {
return nil, ErrViewSelectTemporaryTable.GenWithStackByArgs(tn.Name)
}

var authErr error
if sessionVars.User != nil {
authErr = ErrTableaccessDenied.FastGenByArgs("SELECT", sessionVars.User.AuthUsername, sessionVars.User.AuthHostname, tableInfo.Name.L)
4 changes: 4 additions & 0 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
@@ -487,6 +487,8 @@ type PlanBuilder struct {
buildingViewStack set.StringSet
// renamingViewName is the name of the view which is being renamed.
renamingViewName string
// isCreateView indicates whether the query is create view.
isCreateView bool

// evalDefaultExpr needs this information to find the corresponding column.
// It stores the OutputNames before buildProjection.
@@ -3608,11 +3610,13 @@ func (b *PlanBuilder) buildDDL(ctx context.Context, node ast.DDLNode) (Plan, err
v.ReferTable.Name.L, "", authErr)
}
case *ast.CreateViewStmt:
b.isCreateView = true
b.capFlag |= canExpandAST | renameView
b.renamingViewName = v.ViewName.Schema.L + "." + v.ViewName.Name.L
defer func() {
b.capFlag &= ^canExpandAST
b.capFlag &= ^renameView
b.isCreateView = false
}()
plan, err := b.Build(ctx, v.Select)
if err != nil {
16 changes: 0 additions & 16 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
@@ -815,22 +815,6 @@ func (p *preprocessor) checkCreateViewWithSelect(stmt ast.Node) {
s.LockInfo.LockType = ast.SelectLockNone
return
}

if s.From != nil {
tblNames := extractTableList(s.From.TableRefs, nil, false)
for _, tn := range tblNames {
tbl, err := p.tableByName(tn)
if err != nil {
p.err = err
return
}

if tbl.Meta().TempTableType == model.TempTableLocal {
p.err = ErrViewSelectTemporaryTable.GenWithStackByArgs(tn.Name)
return
}
}
}
case *ast.SetOprSelectList:
for _, sel := range s.Selects {
p.checkCreateViewWithSelect(sel)