Skip to content

Commit

Permalink
Merge branch 'master' into stmt-cpu-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored Jun 2, 2021
2 parents 1f4d10b + c8c0dd0 commit 027ef61
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
52 changes: 52 additions & 0 deletions executor/stale_txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ func (s *testStaleTxnSerialSuite) TestSetTransactionReadOnlyAsOf(c *C) {
c.Assert(tk.Se.GetSessionVars().TxnReadTS.PeakTxnReadTS(), Equals, uint64(0))
tk.MustExec("begin")
c.Assert(tk.Se.GetSessionVars().TxnCtx.StartTS, Not(Equals), testcase.expectedTS)
tk.MustExec("commit")

failpoint.Disable("github.com/pingcap/tidb/expression/injectSafeTS")
}
Expand Down Expand Up @@ -682,6 +683,57 @@ func (s *testStaleTxnSuite) TestSpecialSQLInStalenessTxn(c *C) {
}
}

func (s *testStaleTxnSuite) TestAsOfTimestampCompatibility(c *C) {
tk := testkit.NewTestKit(c, s.store)
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
safePointName := "tikv_gc_safe_point"
safePointValue := "20160102-15:04:05 -0700"
safePointComment := "All versions after safe point can be accessed. (DO NOT EDIT)"
updateSafePoint := fmt.Sprintf(`INSERT INTO mysql.tidb VALUES ('%[1]s', '%[2]s', '%[3]s')
ON DUPLICATE KEY
UPDATE variable_value = '%[2]s', comment = '%[3]s'`, safePointName, safePointValue, safePointComment)
tk.MustExec(updateSafePoint)
tk.MustExec("use test")
tk.MustExec("create table t5(id int);")
time1 := time.Now()
testcases := []struct {
beginSQL string
sql string
}{
{
beginSQL: fmt.Sprintf("START TRANSACTION READ ONLY AS OF TIMESTAMP '%s'", time1.Format("2006-1-2 15:04:05.000")),
sql: fmt.Sprintf(`SET TRANSACTION READ ONLY AS OF TIMESTAMP '%s'`, time1.Format("2006-1-2 15:04:05.000")),
},
{
beginSQL: "begin",
sql: fmt.Sprintf(`SET TRANSACTION READ ONLY AS OF TIMESTAMP '%s'`, time1.Format("2006-1-2 15:04:05.000")),
},
{
beginSQL: "start transaction",
sql: fmt.Sprintf(`SET TRANSACTION READ ONLY AS OF TIMESTAMP '%s'`, time1.Format("2006-1-2 15:04:05.000")),
},
{
beginSQL: fmt.Sprintf("START TRANSACTION READ ONLY AS OF TIMESTAMP '%s'", time1.Format("2006-1-2 15:04:05.000")),
sql: fmt.Sprintf("select * from t5 as of timestamp '%s'", time1.Format("2006-1-2 15:04:05.000")),
},
{
beginSQL: "begin",
sql: fmt.Sprintf("select * from t5 as of timestamp '%s'", time1.Format("2006-1-2 15:04:05.000")),
},
{
beginSQL: "start transaction",
sql: fmt.Sprintf("select * from t5 as of timestamp '%s'", time1.Format("2006-1-2 15:04:05.000")),
},
}
for _, testcase := range testcases {
tk.MustExec(testcase.beginSQL)
err := tk.ExecToErr(testcase.sql)
c.Assert(err, NotNil)
c.Assert(err.Error(), Matches, ".*as of timestamp can't be set in transaction.*")
tk.MustExec("commit")
}
}

func (s *testStaleTxnSuite) TestSetTransactionInfoSchema(c *C) {
tk := testkit.NewTestKit(c, s.store)
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
Expand Down
4 changes: 4 additions & 0 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,10 @@ func (p *preprocessor) handleAsOf(node *ast.AsOfClause) {
dom := domain.GetDomain(p.ctx)
ts := uint64(0)
if node != nil {
if p.ctx.GetSessionVars().InTxn() {
p.err = ErrAsOf.FastGenWithCause("as of timestamp can't be set in transaction.")
return
}
ts, p.err = calculateTsExpr(p.ctx, node)
if p.err != nil {
return
Expand Down
5 changes: 5 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,11 @@ var defaultSysVars = []*SysVar{
}},
{Scope: ScopeSession, Name: TiDBTxnReadTS, Value: "", Hidden: true, SetSession: func(s *SessionVars, val string) error {
return setTxnReadTS(s, val)
}, Validation: func(vars *SessionVars, normalizedValue string, originalValue string, scope ScopeFlag) (string, error) {
if vars.InTxn() {
return "", errors.New("as of timestamp can't be set in transaction")
}
return normalizedValue, nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBAllowMPPExecution, Value: On, Type: TypeEnum, PossibleValues: []string{"OFF", "ON", "ENFORCE"}, SetSession: func(s *SessionVars, val string) error {
s.allowMPPExecution = val
Expand Down

0 comments on commit 027ef61

Please sign in to comment.