forked from pingcap/tidb
/
adapter.go
233 lines (208 loc) · 6.18 KB
/
adapter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package executor
import (
"fmt"
"math"
"time"
"github.com/juju/errors"
"github.com/ngaut/log"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/plan"
)
type processinfoSetter interface {
SetProcessInfo(string)
}
// recordSet wraps an executor, implements ast.RecordSet interface
type recordSet struct {
fields []*ast.ResultField
executor Executor
stmt *statement
processinfo processinfoSetter
err error
}
func (a *recordSet) Fields() ([]*ast.ResultField, error) {
if len(a.fields) == 0 {
for _, col := range a.executor.Schema().Columns {
rf := &ast.ResultField{
ColumnAsName: col.ColName,
TableAsName: col.TblName,
DBName: col.DBName,
Column: &model.ColumnInfo{
FieldType: *col.RetType,
Name: col.ColName,
},
}
a.fields = append(a.fields, rf)
}
}
return a.fields, nil
}
func (a *recordSet) Next() (*ast.Row, error) {
row, err := a.executor.Next()
if err != nil || row == nil {
return nil, errors.Trace(err)
}
return &ast.Row{Data: row.Data}, nil
}
func (a *recordSet) Close() error {
err := a.executor.Close()
a.stmt.logSlowQuery()
if a.processinfo != nil {
a.processinfo.SetProcessInfo("")
}
return errors.Trace(err)
}
// statement implements the ast.Statement interface, it builds a plan.Plan to an ast.Statement.
type statement struct {
// The InfoSchema cannot change during execution, so we hold a reference to it.
is infoschema.InfoSchema
ctx context.Context
text string
plan plan.Plan
startTime time.Time
}
func (a *statement) OriginText() string {
return a.text
}
// Exec implements the ast.Statement Exec interface.
// This function builds an Executor from a plan. If the Executor doesn't return result,
// like the INSERT, UPDATE statements, it executes in this function, if the Executor returns
// result, execution is done after this function returns, in the returned ast.RecordSet Next method.
func (a *statement) Exec(ctx context.Context) (ast.RecordSet, error) {
a.startTime = time.Now()
a.ctx = ctx
if _, ok := a.plan.(*plan.Execute); !ok {
// Do not sync transaction for Execute statement, because the real optimization work is done in
// "ExecuteExec.Build".
var err error
if IsPointGetWithPKOrUniqueKeyByAutoCommit(ctx, a.plan) {
log.Debugf("[%d][InitTxnWithStartTS] %s", ctx.GetSessionVars().ConnectionID, a.text)
err = ctx.InitTxnWithStartTS(math.MaxUint64)
} else {
log.Debugf("[%d][ActivePendingTxn] %s", ctx.GetSessionVars().ConnectionID, a.text)
err = ctx.ActivePendingTxn()
}
if err != nil {
return nil, errors.Trace(err)
}
}
b := newExecutorBuilder(ctx, a.is)
e := b.build(a.plan)
if b.err != nil {
return nil, errors.Trace(b.err)
}
// ExecuteExec is not a real Executor, we only use it to build another Executor from a prepared statement.
if executorExec, ok := e.(*ExecuteExec); ok {
err := executorExec.Build()
if err != nil {
return nil, errors.Trace(err)
}
a.text = executorExec.Stmt.Text()
a.plan = executorExec.Plan
e = executorExec.StmtExec
}
var pi processinfoSetter
if raw, ok := ctx.(processinfoSetter); ok {
pi = raw
// Update processinfo, ShowProcess() will use it.
pi.SetProcessInfo(a.OriginText())
}
// Fields or Schema are only used for statements that return result set.
if e.Schema().Len() == 0 {
// Check if "tidb_snapshot" is set for the write executors.
// In history read mode, we can not do write operations.
switch e.(type) {
case *DeleteExec, *InsertExec, *UpdateExec, *ReplaceExec, *LoadData, *DDLExec:
snapshotTS := ctx.GetSessionVars().SnapshotTS
if snapshotTS != 0 {
return nil, errors.New("can not execute write statement when 'tidb_snapshot' is set")
}
}
defer func() {
if pi != nil {
pi.SetProcessInfo("")
}
e.Close()
a.logSlowQuery()
}()
for {
row, err := e.Next()
if err != nil {
return nil, errors.Trace(err)
}
// Even though there isn't any result set, the row is still used to indicate if there is
// more work to do.
// For example, the UPDATE statement updates a single row on a Next call, we keep calling Next until
// There is no more rows to update.
if row == nil {
return nil, nil
}
}
}
return &recordSet{
executor: e,
stmt: a,
processinfo: pi,
}, nil
}
const (
queryLogMaxLen = 2048
slowThreshold = 300 * time.Millisecond
)
func (a *statement) logSlowQuery() {
costTime := time.Since(a.startTime)
sql := a.text
if len(sql) > queryLogMaxLen {
sql = sql[:queryLogMaxLen] + fmt.Sprintf("(len:%d)", len(sql))
}
connID := a.ctx.GetSessionVars().ConnectionID
if costTime < slowThreshold {
log.Debugf("[%d][TIME_QUERY] %v %s", connID, costTime, sql)
} else {
log.Warnf("[%d][TIME_QUERY] %v %s", connID, costTime, sql)
}
}
// IsPointGetWithPKOrUniqueKeyByAutoCommit returns true when meets following conditions:
// 1. ctx is auto commit tagged
// 2. txn is nil
// 2. plan is point get by pk or unique key
func IsPointGetWithPKOrUniqueKeyByAutoCommit(ctx context.Context, p plan.Plan) bool {
// check auto commit
if !ctx.GetSessionVars().IsAutocommit() {
return false
}
// check txn
if ctx.Txn() != nil {
return false
}
// check plan
if proj, ok := p.(*plan.Projection); ok {
if len(proj.Children()) != 1 {
return false
}
p = proj.Children()[0]
}
// get by index key
if indexScan, ok := p.(*plan.PhysicalIndexScan); ok {
return indexScan.IsPointGetByUniqueKey(ctx.GetSessionVars().StmtCtx)
}
// get by primary key
if tableScan, ok := p.(*plan.PhysicalTableScan); ok {
return len(tableScan.Ranges) == 1 && tableScan.Ranges[0].IsPoint()
}
return false
}