@@ -37,6 +37,7 @@ import (
37
37
"github.com/pingcap/tidb/privilege"
38
38
"github.com/pingcap/tidb/sessionctx"
39
39
"github.com/pingcap/tidb/sessionctx/variable"
40
+ "github.com/pingcap/tidb/sessiontxn"
40
41
"github.com/pingcap/tidb/table"
41
42
"github.com/pingcap/tidb/table/temptable"
42
43
"github.com/pingcap/tidb/types"
@@ -59,6 +60,11 @@ func InTxnRetry(p *preprocessor) {
59
60
p .flag |= inTxnRetry
60
61
}
61
62
63
+ // InitTxnContextProvider is a PreprocessOpt that indicates preprocess should init transaction's context
64
+ func InitTxnContextProvider (p * preprocessor ) {
65
+ p .flag |= initTxnContextProvider
66
+ }
67
+
62
68
// WithPreprocessorReturn returns a PreprocessOpt to initialize the PreprocessorReturn.
63
69
func WithPreprocessorReturn (ret * PreprocessorReturn ) PreprocessOpt {
64
70
return func (p * preprocessor ) {
@@ -117,6 +123,9 @@ func Preprocess(ctx sessionctx.Context, node ast.Node, preprocessOpt ...Preproce
117
123
node .Accept (& v )
118
124
// InfoSchema must be non-nil after preprocessing
119
125
v .ensureInfoSchema ()
126
+
127
+ v .initTxnContextProviderIfNecessary (node )
128
+
120
129
return errors .Trace (v .err )
121
130
}
122
131
@@ -136,6 +145,8 @@ const (
136
145
// inSequenceFunction is set when visiting a sequence function.
137
146
// This flag indicates the tableName in these function should be checked as sequence object.
138
147
inSequenceFunction
148
+ // initTxnContextProvider is set when we should init txn context in preprocess
149
+ initTxnContextProvider
139
150
)
140
151
141
152
// Make linter happy.
@@ -193,6 +204,9 @@ func (p *preprocessor) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
193
204
// handle the insert table name imminently
194
205
// insert into t with t ..., the insert can not see t here. We should hand it before the CTE statement
195
206
p .handleTableName (node .Table .TableRefs .Left .(* ast.TableSource ).Source .(* ast.TableName ))
207
+ case * ast.ExecuteStmt :
208
+ p .stmtTp = TypeExecute
209
+ p .resolveExecuteStmt (node )
196
210
case * ast.CreateTableStmt :
197
211
p .stmtTp = TypeCreate
198
212
p .flag |= inCreateOrDropTable
@@ -361,6 +375,8 @@ const (
361
375
TypeRepair
362
376
// TypeShow for ShowStmt
363
377
TypeShow
378
+ // TypeExecute for ExecuteStmt
379
+ TypeExecute
364
380
)
365
381
366
382
func bindableStmtType (node ast.StmtNode ) byte {
@@ -1489,6 +1505,32 @@ func (p *preprocessor) resolveShowStmt(node *ast.ShowStmt) {
1489
1505
}
1490
1506
}
1491
1507
1508
+ func (p * preprocessor ) resolveExecuteStmt (node * ast.ExecuteStmt ) {
1509
+ prepared , err := GetPreparedStmt (node , p .ctx .GetSessionVars ())
1510
+ if err != nil {
1511
+ p .err = err
1512
+ return
1513
+ }
1514
+
1515
+ if prepared .SnapshotTSEvaluator != nil {
1516
+ snapshotTS , err := prepared .SnapshotTSEvaluator (p .ctx )
1517
+ if err != nil {
1518
+ p .err = err
1519
+ return
1520
+ }
1521
+
1522
+ is , err := domain .GetDomain (p .ctx ).GetSnapshotInfoSchema (snapshotTS )
1523
+ if err != nil {
1524
+ p .err = err
1525
+ return
1526
+ }
1527
+
1528
+ p .LastSnapshotTS = snapshotTS
1529
+ p .initedLastSnapshotTS = true
1530
+ p .InfoSchema = temptable .AttachLocalTemporaryTableInfoSchema (p .ctx , is )
1531
+ }
1532
+ }
1533
+
1492
1534
func (p * preprocessor ) resolveCreateTableStmt (node * ast.CreateTableStmt ) {
1493
1535
for _ , val := range node .Constraints {
1494
1536
if val .Refer != nil && val .Refer .Table .Schema .String () == "" {
@@ -1689,3 +1731,13 @@ func (p *preprocessor) ensureInfoSchema() infoschema.InfoSchema {
1689
1731
p .InfoSchema = p .ctx .GetInfoSchema ().(infoschema.InfoSchema )
1690
1732
return p .InfoSchema
1691
1733
}
1734
+
1735
+ func (p * preprocessor ) initTxnContextProviderIfNecessary (node ast.Node ) {
1736
+ if p .err != nil || p .flag & initTxnContextProvider == 0 {
1737
+ return
1738
+ }
1739
+
1740
+ p .err = sessiontxn .GetTxnManager (p .ctx ).SetContextProvider (& sessiontxn.SimpleTxnContextProvider {
1741
+ InfoSchema : p .ensureInfoSchema (),
1742
+ })
1743
+ }
0 commit comments