forked from talent-plan/tinysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow.go
483 lines (444 loc) · 13.7 KB
/
show.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright 2016 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 (
"bytes"
"context"
"fmt"
"sort"
"strconv"
"strings"
"github.com/cznic/mathutil"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/charset"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/format"
)
// ShowExec represents a show executor.
type ShowExec struct {
baseExecutor
Tp ast.ShowStmtType // Databases/Tables/Columns/....
DBName model.CIStr
Table *ast.TableName // Used for showing columns.
Column *ast.ColumnName // Used for `desc table column`.
IndexName model.CIStr // Used for show table regions.
Flag int // Some flag parsed from sql, such as FULL.
is infoschema.InfoSchema
result *chunk.Chunk
cursor int
Full bool
IfNotExists bool // Used for `show create database if not exists`
GlobalScope bool // GlobalScope is used by show variables
}
// Next implements the Executor Next interface.
func (e *ShowExec) Next(ctx context.Context, req *chunk.Chunk) error {
req.GrowAndReset(e.maxChunkSize)
if e.result == nil {
e.result = newFirstChunk(e)
err := e.fetchAll(ctx)
if err != nil {
return errors.Trace(err)
}
iter := chunk.NewIterator4Chunk(e.result)
for colIdx := 0; colIdx < e.Schema().Len(); colIdx++ {
retType := e.Schema().Columns[colIdx].RetType
if !types.IsTypeVarchar(retType.Tp) {
continue
}
for row := iter.Begin(); row != iter.End(); row = iter.Next() {
if valLen := len(row.GetString(colIdx)); retType.Flen < valLen {
retType.Flen = valLen
}
}
}
}
if e.cursor >= e.result.NumRows() {
return nil
}
numCurBatch := mathutil.Min(req.Capacity(), e.result.NumRows()-e.cursor)
req.Append(e.result, e.cursor, e.cursor+numCurBatch)
e.cursor += numCurBatch
return nil
}
func (e *ShowExec) fetchAll(ctx context.Context) error {
switch e.Tp {
case ast.ShowCreateTable:
return e.fetchShowCreateTable()
case ast.ShowCreateDatabase:
return e.fetchShowCreateDatabase()
case ast.ShowDatabases:
return e.fetchShowDatabases()
case ast.ShowTables:
return e.fetchShowTables()
case ast.ShowVariables:
return e.fetchShowVariables()
case ast.ShowWarnings:
return e.fetchShowWarnings(false)
case ast.ShowErrors:
return e.fetchShowWarnings(true)
}
return nil
}
// moveInfoSchemaToFront moves information_schema to the first, and the others are sorted in the origin ascending order.
func moveInfoSchemaToFront(dbs []string) {
if len(dbs) > 0 && strings.EqualFold(dbs[0], "INFORMATION_SCHEMA") {
return
}
i := sort.SearchStrings(dbs, "INFORMATION_SCHEMA")
if i < len(dbs) && strings.EqualFold(dbs[i], "INFORMATION_SCHEMA") {
copy(dbs[1:i+1], dbs[0:i])
dbs[0] = "INFORMATION_SCHEMA"
}
}
func (e *ShowExec) fetchShowDatabases() error {
dbs := e.is.AllSchemaNames()
sort.Strings(dbs)
// let information_schema be the first database
moveInfoSchemaToFront(dbs)
for _, d := range dbs {
e.appendRow([]interface{}{
d,
})
}
return nil
}
func (e *ShowExec) fetchShowTables() error {
if !e.is.SchemaExists(e.DBName) {
return ErrBadDB.GenWithStackByArgs(e.DBName)
}
// sort for tables
tableNames := make([]string, 0, len(e.is.SchemaTables(e.DBName)))
var tableTypes = make(map[string]string)
for _, v := range e.is.SchemaTables(e.DBName) {
tableNames = append(tableNames, v.Meta().Name.O)
tableTypes[v.Meta().Name.O] = "BASE TABLE"
}
sort.Strings(tableNames)
for _, v := range tableNames {
if e.Full {
e.appendRow([]interface{}{v, tableTypes[v]})
} else {
e.appendRow([]interface{}{v})
}
}
return nil
}
func (e *ShowExec) fetchShowVariables() (err error) {
var (
value string
ok bool
sessionVars = e.ctx.GetSessionVars()
unreachedVars = make([]string, 0, len(variable.SysVars))
)
for _, v := range variable.SysVars {
if !e.GlobalScope {
// For a session scope variable,
// 1. try to fetch value from SessionVars.Systems;
// 2. if this variable is session-only, fetch value from SysVars
// otherwise, fetch the value from table `mysql.Global_Variables`.
value, ok, err = variable.GetSessionOnlySysVars(sessionVars, v.Name)
} else {
// If the scope of a system variable is ScopeNone,
// it's a read-only variable, so we return the default value of it.
// Otherwise, we have to fetch the values from table `mysql.Global_Variables` for global variable names.
value, ok, err = variable.GetScopeNoneSystemVar(v.Name)
}
if err != nil {
return errors.Trace(err)
}
if !ok {
unreachedVars = append(unreachedVars, v.Name)
continue
}
e.appendRow([]interface{}{v.Name, value})
}
if len(unreachedVars) != 0 {
systemVars, err := sessionVars.GlobalVarsAccessor.GetAllSysVars()
if err != nil {
return errors.Trace(err)
}
for _, varName := range unreachedVars {
varValue, ok := systemVars[varName]
if !ok {
varValue = variable.SysVars[varName].Value
}
e.appendRow([]interface{}{varName, varValue})
}
}
return nil
}
func getDefaultCollate(charsetName string) string {
for _, c := range charset.GetSupportedCharsets() {
if strings.EqualFold(c.Name, charsetName) {
return c.DefaultCollation
}
}
return ""
}
// escape the identifier for pretty-printing.
// For instance, the identifier "foo `bar`" will become "`foo ``bar```".
// The sqlMode controls whether to escape with backquotes (`) or double quotes
// (`"`) depending on whether mysql.ModeANSIQuotes is enabled.
func escape(cis model.CIStr, sqlMode mysql.SQLMode) string {
var quote string
if sqlMode&mysql.ModeANSIQuotes != 0 {
quote = `"`
} else {
quote = "`"
}
return quote + strings.Replace(cis.O, quote, quote+quote, -1) + quote
}
// ConstructResultOfShowCreateTable constructs the result for show create table.
func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.TableInfo, allocator autoid.Allocator, buf *bytes.Buffer) (err error) {
tblCharset := tableInfo.Charset
if len(tblCharset) == 0 {
tblCharset = mysql.DefaultCharset
}
tblCollate := tableInfo.Collate
// Set default collate if collate is not specified.
if len(tblCollate) == 0 {
tblCollate = getDefaultCollate(tblCharset)
}
sqlMode := ctx.GetSessionVars().SQLMode
fmt.Fprintf(buf, "CREATE TABLE %s (\n", escape(tableInfo.Name, sqlMode))
var pkCol *model.ColumnInfo
var hasAutoIncID bool
for i, col := range tableInfo.Cols() {
fmt.Fprintf(buf, " %s %s", escape(col.Name, sqlMode), col.GetTypeDesc())
if col.Charset != "binary" {
if col.Charset != tblCharset {
fmt.Fprintf(buf, " CHARACTER SET %s", col.Charset)
}
if col.Collate != tblCollate {
fmt.Fprintf(buf, " COLLATE %s", col.Collate)
} else {
defcol, err := charset.GetDefaultCollation(col.Charset)
if err == nil && defcol != col.Collate {
fmt.Fprintf(buf, " COLLATE %s", col.Collate)
}
}
}
if mysql.HasAutoIncrementFlag(col.Flag) {
hasAutoIncID = true
buf.WriteString(" NOT NULL AUTO_INCREMENT")
} else {
if mysql.HasNotNullFlag(col.Flag) {
buf.WriteString(" NOT NULL")
}
// default values are not shown for generated columns in MySQL
if !mysql.HasNoDefaultValueFlag(col.Flag) {
defaultValue := col.GetDefaultValue()
switch defaultValue {
case nil:
if !mysql.HasNotNullFlag(col.Flag) {
buf.WriteString(" DEFAULT NULL")
}
default:
defaultValStr := fmt.Sprintf("%v", defaultValue)
fmt.Fprintf(buf, " DEFAULT '%s'", format.OutputFormat(defaultValStr))
}
}
if mysql.HasOnUpdateNowFlag(col.Flag) {
buf.WriteString(" ON UPDATE CURRENT_TIMESTAMP")
buf.WriteString(table.OptionalFsp(&col.FieldType))
}
}
if len(col.Comment) > 0 {
fmt.Fprintf(buf, " COMMENT '%s'", format.OutputFormat(col.Comment))
}
if i != len(tableInfo.Cols())-1 {
buf.WriteString(",\n")
}
if tableInfo.PKIsHandle && mysql.HasPriKeyFlag(col.Flag) {
pkCol = col
}
}
if pkCol != nil {
// If PKIsHanle, pk info is not in tb.Indices(). We should handle it here.
buf.WriteString(",\n")
fmt.Fprintf(buf, " PRIMARY KEY (%s)", escape(pkCol.Name, sqlMode))
}
publicIndices := make([]*model.IndexInfo, 0, len(tableInfo.Indices))
for _, idx := range tableInfo.Indices {
if idx.State == model.StatePublic {
publicIndices = append(publicIndices, idx)
}
}
if len(publicIndices) > 0 {
buf.WriteString(",\n")
}
for i, idxInfo := range publicIndices {
if idxInfo.Primary {
buf.WriteString(" PRIMARY KEY ")
} else if idxInfo.Unique {
fmt.Fprintf(buf, " UNIQUE KEY %s ", escape(idxInfo.Name, sqlMode))
} else {
fmt.Fprintf(buf, " KEY %s ", escape(idxInfo.Name, sqlMode))
}
cols := make([]string, 0, len(idxInfo.Columns))
for _, c := range idxInfo.Columns {
colInfo := escape(c.Name, sqlMode)
if c.Length != types.UnspecifiedLength {
colInfo = fmt.Sprintf("%s(%s)", colInfo, strconv.Itoa(c.Length))
}
cols = append(cols, colInfo)
}
fmt.Fprintf(buf, "(%s)", strings.Join(cols, ","))
if i != len(publicIndices)-1 {
buf.WriteString(",\n")
}
}
buf.WriteString("\n")
buf.WriteString(") ENGINE=InnoDB")
// Because we only support case sensitive utf8_bin collate, we need to explicitly set the default charset and collation
// to make it work on MySQL server which has default collate utf8_general_ci.
if len(tblCollate) == 0 {
// If we can not find default collate for the given charset,
// do not show the collate part.
fmt.Fprintf(buf, " DEFAULT CHARSET=%s", tblCharset)
} else {
fmt.Fprintf(buf, " DEFAULT CHARSET=%s COLLATE=%s", tblCharset, tblCollate)
}
// Displayed if the compression typed is set.
if len(tableInfo.Compression) != 0 {
fmt.Fprintf(buf, " COMPRESSION='%s'", tableInfo.Compression)
}
if hasAutoIncID {
autoIncID, err := allocator.NextGlobalAutoID(tableInfo.ID)
if err != nil {
return errors.Trace(err)
}
// It's compatible with MySQL.
if autoIncID > 1 {
fmt.Fprintf(buf, " AUTO_INCREMENT=%d", autoIncID)
}
}
if tableInfo.ShardRowIDBits > 0 {
fmt.Fprintf(buf, "/*!90000 SHARD_ROW_ID_BITS=%d ", tableInfo.ShardRowIDBits)
if tableInfo.PreSplitRegions > 0 {
fmt.Fprintf(buf, "PRE_SPLIT_REGIONS=%d ", tableInfo.PreSplitRegions)
}
buf.WriteString("*/")
}
if len(tableInfo.Comment) > 0 {
fmt.Fprintf(buf, " COMMENT='%s'", format.OutputFormat(tableInfo.Comment))
}
return nil
}
func (e *ShowExec) fetchShowCreateTable() error {
tb, err := e.getTable()
if err != nil {
return errors.Trace(err)
}
allocator := tb.Allocator(e.ctx)
var buf bytes.Buffer
// TODO: let the result more like MySQL.
if err = ConstructResultOfShowCreateTable(e.ctx, tb.Meta(), allocator, &buf); err != nil {
return err
}
e.appendRow([]interface{}{tb.Meta().Name.O, buf.String()})
return nil
}
// ConstructResultOfShowCreateDatabase constructs the result for show create database.
func ConstructResultOfShowCreateDatabase(ctx sessionctx.Context, dbInfo *model.DBInfo, ifNotExists bool, buf *bytes.Buffer) (err error) {
sqlMode := ctx.GetSessionVars().SQLMode
var ifNotExistsStr string
if ifNotExists {
ifNotExistsStr = "/*!32312 IF NOT EXISTS*/ "
}
fmt.Fprintf(buf, "CREATE DATABASE %s%s", ifNotExistsStr, escape(dbInfo.Name, sqlMode))
if s := dbInfo.Charset; len(s) > 0 {
fmt.Fprintf(buf, " /*!40100 DEFAULT CHARACTER SET %s */", s)
}
return nil
}
// fetchShowCreateDatabase composes show create database result.
func (e *ShowExec) fetchShowCreateDatabase() error {
dbInfo, ok := e.is.SchemaByName(e.DBName)
if !ok {
return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(e.DBName.O)
}
var buf bytes.Buffer
err := ConstructResultOfShowCreateDatabase(e.ctx, dbInfo, e.IfNotExists, &buf)
if err != nil {
return err
}
e.appendRow([]interface{}{dbInfo.Name.O, buf.String()})
return nil
}
func (e *ShowExec) fetchShowWarnings(errOnly bool) error {
warns := e.ctx.GetSessionVars().StmtCtx.GetWarnings()
for _, w := range warns {
if errOnly && w.Level != stmtctx.WarnLevelError {
continue
}
warn := errors.Cause(w.Err)
switch x := warn.(type) {
case *terror.Error:
sqlErr := x.ToSQLError()
e.appendRow([]interface{}{w.Level, int64(sqlErr.Code), sqlErr.Message})
default:
e.appendRow([]interface{}{w.Level, int64(mysql.ErrUnknown), warn.Error()})
}
}
return nil
}
func (e *ShowExec) getTable() (table.Table, error) {
if e.Table == nil {
return nil, errors.New("table not found")
}
tb, ok := e.is.TableByID(e.Table.TableInfo.ID)
if !ok {
return nil, errors.Errorf("table %s not found", e.Table.Name)
}
return tb, nil
}
func (e *ShowExec) appendRow(row []interface{}) {
for i, col := range row {
if col == nil {
e.result.AppendNull(i)
continue
}
switch x := col.(type) {
case nil:
e.result.AppendNull(i)
case int:
e.result.AppendInt64(i, int64(x))
case int64:
e.result.AppendInt64(i, x)
case uint64:
e.result.AppendUint64(i, x)
case float64:
e.result.AppendFloat64(i, x)
case float32:
e.result.AppendFloat32(i, x)
case string:
e.result.AppendString(i, x)
case []byte:
e.result.AppendBytes(i, x)
default:
e.result.AppendNull(i)
}
}
}