Skip to content

Commit

Permalink
planner: forbid prepared stale select in txn (pingcap#25375)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhebox authored Jun 15, 2021
1 parent bd11917 commit 07d0b2d
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions errno/errcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,7 @@ const (
ErrNotSupportedWithSem = 8132
ErrDataInConsistentExtraIndex = 8133
ErrDataInConsistentMisMatchIndex = 8134
ErrAsOf = 8135

// Error codes used by TiDB ddl package
ErrUnsupportedDDLOperation = 8200
Expand Down
1 change: 1 addition & 0 deletions errno/errname.go
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@ var MySQLErrName = map[uint16]*mysql.ErrMessage{
ErrInvalidPlacementSpec: mysql.Message("Invalid placement policy '%s': %s", nil),
ErrPlacementPolicyCheck: mysql.Message("Placement policy didn't meet the constraint, reason: %s", nil),
ErrMultiStatementDisabled: mysql.Message("client has multi-statement capability disabled. Run SET GLOBAL tidb_multi_statement_mode='ON' after you understand the security risk", nil),
ErrAsOf: mysql.Message("invalid as of timestamp: %s", nil),

// TiKV/PD errors.
ErrPDServerTimeout: mysql.Message("PD server timeout", nil),
Expand Down
5 changes: 5 additions & 0 deletions errors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,11 @@ error = '''
Feature '%s' is not supported when security enhanced mode is enabled
'''

["planner:8135"]
error = '''
invalid as of timestamp: %s
'''

["privilege:1141"]
error = '''
There is no such grant defined for user '%-.48s' on host '%-.64s'
Expand Down
4 changes: 2 additions & 2 deletions executor/stale_txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ func (s *testStaleTxnSuite) TestStaleSelect(c *C) {

// test prepared stale select in txn
tk.MustExec("begin")
tk.MustQuery("execute s").Check(staleRows)
c.Assert(tk.ExecToErr(staleSQL), NotNil)
tk.MustExec("commit")

// test stale select in stale txn
Expand All @@ -825,7 +825,7 @@ func (s *testStaleTxnSuite) TestStaleSelect(c *C) {

// test prepared stale select in stale txn
tk.MustExec(fmt.Sprintf(`start transaction read only as of timestamp '%s'`, time2.Format("2006-1-2 15:04:05.000")))
tk.MustQuery("execute s").Check(staleRows)
c.Assert(tk.ExecToErr(staleSQL), NotNil)
tk.MustExec("commit")

// test prepared stale select with schema change
Expand Down
3 changes: 3 additions & 0 deletions planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ func (e *Execute) OptimizePreparedPlan(ctx context.Context, sctx sessionctx.Cont

var snapshotTS uint64
if preparedObj.SnapshotTSEvaluator != nil {
if vars.InTxn() {
return ErrAsOf.FastGenWithCause("as of timestamp can't be set in transaction.")
}
// if preparedObj.SnapshotTSEvaluator != nil, it is a stale read SQL:
// which means its infoschema is specified by the SQL, not the current/latest infoschema
var err error
Expand Down
3 changes: 1 addition & 2 deletions planner/core/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ var (
ErrAccessDenied = dbterror.ClassOptimizer.NewStdErr(mysql.ErrAccessDenied, mysql.MySQLErrName[mysql.ErrAccessDeniedNoPassword])
ErrBadNull = dbterror.ClassOptimizer.NewStd(mysql.ErrBadNull)
ErrNotSupportedWithSem = dbterror.ClassOptimizer.NewStd(mysql.ErrNotSupportedWithSem)
ErrDifferentAsOf = dbterror.ClassOptimizer.NewStd(mysql.ErrUnknown)
ErrAsOf = dbterror.ClassOptimizer.NewStd(mysql.ErrUnknown)
ErrAsOf = dbterror.ClassOptimizer.NewStd(mysql.ErrAsOf)
ErrOptOnTemporaryTable = dbterror.ClassOptimizer.NewStd(mysql.ErrOptOnTemporaryTable)
// ErrPartitionNoTemporary returns when partition at temporary mode
ErrPartitionNoTemporary = dbterror.ClassOptimizer.NewStd(mysql.ErrPartitionNoTemporary)
Expand Down
2 changes: 1 addition & 1 deletion planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,7 @@ func (p *preprocessor) handleAsOfAndReadTS(node *ast.AsOfClause) {
}
}
if p.LastSnapshotTS != ts {
p.err = ErrDifferentAsOf.GenWithStack("can not set different time in the as of")
p.err = ErrAsOf.GenWithStack("can not set different time in the as of")
return
}
if p.LastSnapshotTS != 0 {
Expand Down

0 comments on commit 07d0b2d

Please sign in to comment.