Skip to content

Commit

Permalink
*: basic support show warnings. (pingcap#2724)
Browse files Browse the repository at this point in the history
  • Loading branch information
coocood authored and zimulala committed Feb 24, 2017
1 parent a1a24e8 commit 07952cb
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 7 deletions.
24 changes: 23 additions & 1 deletion executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/sessionctx/varsutil"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/util/charset"
"github.com/pingcap/tidb/util/types"
)
Expand Down Expand Up @@ -109,7 +110,9 @@ func (e *ShowExec) fetchAll() error {
return e.fetchShowTriggers()
case ast.ShowVariables:
return e.fetchShowVariables()
case ast.ShowWarnings, ast.ShowProcessList, ast.ShowEvents:
case ast.ShowWarnings:
return e.fetchShowWarnings()
case ast.ShowProcessList, ast.ShowEvents:
// empty result
}
return nil
Expand Down Expand Up @@ -539,6 +542,25 @@ func (e *ShowExec) fetchShowProcedureStatus() error {
return nil
}

func (e *ShowExec) fetchShowWarnings() error {
warns := e.ctx.GetSessionVars().StmtCtx.GetWarnings()
for _, warn := range warns {
datums := make([]types.Datum, 3)
datums[0] = types.NewStringDatum("Warning")
switch x := warn.(type) {
case *terror.Error:
sqlErr := x.ToSQLError()
datums[1] = types.NewIntDatum(int64(sqlErr.Code))
datums[2] = types.NewStringDatum(sqlErr.Message)
default:
datums[1] = types.NewIntDatum(int64(mysql.ErrUnknown))
datums[2] = types.NewStringDatum(warn.Error())
}
e.rows = append(e.rows, &Row{Data: datums})
}
return nil
}

func (e *ShowExec) getTable() (table.Table, error) {
if e.Table == nil {
return nil, errors.New("table not found")
Expand Down
14 changes: 14 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testleak"
"github.com/pingcap/tidb/util/testutil"
)

func (s *testSuite) TestShow(c *C) {
Expand Down Expand Up @@ -215,5 +216,18 @@ func (s *testSuite) TestForeignKeyInShowCreateTable(c *C) {
for i, r := range row {
c.Check(r, Equals, expectedRow[i])
}
}

func (s *testSuite) TestShowWarnings(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
testSQL := `create table if not exists show_warnings (a int)`
tk.MustExec(testSQL)
tk.MustExec("set @@sql_mode=''")
tk.MustExec("insert show_warnings values ('a')")
c.Assert(tk.Se.GetSessionVars().StmtCtx.WarningCount(), Equals, uint16(1))
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1265|Data Truncated"))
c.Assert(tk.Se.GetSessionVars().StmtCtx.WarningCount(), Equals, uint16(0))
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1265|Data Truncated"))
c.Assert(tk.Se.GetSessionVars().StmtCtx.WarningCount(), Equals, uint16(0))
}
11 changes: 11 additions & 0 deletions plan/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,8 @@ func (b *planBuilder) buildShow(show *ast.ShowStmt) Plan {
p.SetSchema(buildShowTriggerSchema())
case ast.ShowEvents:
p.SetSchema(buildShowEventsSchema())
case ast.ShowWarnings:
p.SetSchema(buildShowWarningsSchema())
default:
p.SetSchema(buildShowSchema(show))
}
Expand Down Expand Up @@ -831,6 +833,15 @@ func buildShowEventsSchema() *expression.Schema {
return schema
}

func buildShowWarningsSchema() *expression.Schema {
tblName := "WARNINGS"
schema := expression.NewSchema(make([]*expression.Column, 0, 3)...)
schema.Append(buildColumn(tblName, "Level", mysql.TypeVarchar, 64))
schema.Append(buildColumn(tblName, "Code", mysql.TypeLong, 19))
schema.Append(buildColumn(tblName, "Message", mysql.TypeVarchar, 64))
return schema
}

func composeShowSchema(names []string, ftypes []byte) *expression.Schema {
schema := expression.NewSchema(make([]*expression.Column, 0, len(names))...)
for i, name := range names {
Expand Down
9 changes: 4 additions & 5 deletions server/driver_tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ func NewTiDBDriver(store kv.Storage) *TiDBDriver {

// TiDBContext implements QueryCtx.
type TiDBContext struct {
session tidb.Session
currentDB string
warningCount uint16
stmts map[int]*TiDBStatement
session tidb.Session
currentDB string
stmts map[int]*TiDBStatement
}

// TiDBStatement implements PreparedStatement.
Expand Down Expand Up @@ -185,7 +184,7 @@ func (tc *TiDBContext) CurrentDB() string {

// WarningCount implements QueryCtx WarningCount method.
func (tc *TiDBContext) WarningCount() uint16 {
return tc.warningCount
return tc.session.GetSessionVars().StmtCtx.WarningCount()
}

// Execute implements QueryCtx Execute method.
Expand Down
17 changes: 16 additions & 1 deletion sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package variable

import (
"math"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -261,6 +262,7 @@ type StatementContext struct {
InUpdateOrDeleteStmt bool
IgnoreTruncate bool
TruncateAsWarning bool
InShowWarning bool

/* Variables that changes during execution. */
mu struct {
Expand Down Expand Up @@ -310,6 +312,17 @@ func (sc *StatementContext) GetWarnings() []error {
return warns
}

// WarningCount gets warning count.
func (sc *StatementContext) WarningCount() uint16 {
if sc.InShowWarning {
return 0
}
sc.mu.Lock()
wc := uint16(len(sc.mu.warnings))
sc.mu.Unlock()
return wc
}

// SetWarnings sets warnings.
func (sc *StatementContext) SetWarnings(warns []error) {
sc.mu.Lock()
Expand All @@ -320,6 +333,8 @@ func (sc *StatementContext) SetWarnings(warns []error) {
// AppendWarning appends a warning.
func (sc *StatementContext) AppendWarning(warn error) {
sc.mu.Lock()
sc.mu.warnings = append(sc.mu.warnings, warn)
if len(sc.mu.warnings) < math.MaxUint16 {
sc.mu.warnings = append(sc.mu.warnings, warn)
}
sc.mu.Unlock()
}
1 change: 1 addition & 0 deletions tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func resetStmtCtx(ctx context.Context, s ast.StmtNode) {
sc.IgnoreTruncate = true
if show, ok := s.(*ast.ShowStmt); ok {
if show.Tp == ast.ShowWarnings {
sc.InShowWarning = true
sc.SetWarnings(sessVars.StmtCtx.GetWarnings())
}
}
Expand Down

0 comments on commit 07952cb

Please sign in to comment.