Skip to content

Commit

Permalink
*: Implement show variables stmt/rset/plan
Browse files Browse the repository at this point in the history
TODO: add test cases
  • Loading branch information
shenli committed Sep 10, 2015
1 parent 288fc4b commit 20cf24f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 30 deletions.
20 changes: 11 additions & 9 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ import (
Function "function expr"
FunctionCall "function call post part"
FunctionCallArgList "function call optional argument list"
GlobalScope "The scope of variable"
GroupByClause "GROUP BY clause"
GroupByList "GROUP BY list"
HavingClause "HAVING clause"
Expand Down Expand Up @@ -422,7 +423,6 @@ import (
VariableAssignment "set variable value"
VariableAssignmentList "set variable value list"
Variable "User or system variable"
VariableScope "The scope of variable"
WhereClause "WHERE clause"
WhereClauseOptional "Optinal WHERE clause"

Expand Down Expand Up @@ -2650,34 +2650,36 @@ ShowStmt:
{
$$ = &stmts.ShowStmt{Target: stmt.ShowWarnings}
}
| "SHOW" VariableScope "VARIABLES"
// See: https://dev.mysql.com/doc/refman/5.7/en/show-variables.html
// TODO: Support show variables with where clause.
| "SHOW" GlobalScope "VARIABLES"
{
$$ = &stmts.ShowStmt{
Target: stmt.ShowVariables,
VarScope: $2.(int),
GlobalScope: $2.(bool),
}

}
| "SHOW" VariableScope "VARIABLES" "LIKE" PrimaryExpression
| "SHOW" GlobalScope "VARIABLES" "LIKE" PrimaryExpression
{
$$ = &stmts.ShowStmt{
Target: stmt.ShowVariables,
VarScope: $2.(int),
GlobalScope: $2.(bool),
Pattern: &expressions.PatternLike{Pattern: $5.(expression.Expression)},
}
}

VariableScope:
GlobalScope:
{
$$ = stmt.SessionScope
$$ = false
}
| "GLOBAL"
{
$$ = stmt.GlobalScope
$$ = true
}
| "SESSION"
{
$$ = stmt.SessionScope
$$ = false
}

OptFull:
Expand Down
34 changes: 33 additions & 1 deletion plan/plans/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import (
"github.com/pingcap/tidb/column"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/expression/expressions"
"github.com/pingcap/tidb/field"
"github.com/pingcap/tidb/model"
"github.com/pingcap/tidb/plan"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/stmt"
"github.com/pingcap/tidb/util/charset"
"github.com/pingcap/tidb/util/format"
Expand All @@ -43,6 +45,9 @@ type ShowPlan struct {
ColumnName string
Flag int
Full bool
// Used by SHOW VARIABLES
GlobalScope bool
Pattern expression.Expression
}

func (s *ShowPlan) isColOK(c *column.Col) bool {
Expand Down Expand Up @@ -144,6 +149,32 @@ func (s *ShowPlan) Do(ctx context.Context, f plan.RowIterFunc) (err error) {
row := []interface{}{desc.Name, desc.Desc, desc.DefaultCollation, desc.Maxlen}
f(0, row)
}
case stmt.ShowVariables:
for _, v := range variable.SysVars {
if s.GlobalScope != (v.Scope == variable.ScopeGlobal) {
continue
}
if s.Pattern != nil {
p, ok := s.Pattern.(*expressions.PatternLike)
if !ok {
return errors.Errorf("Like should be a PatternLike expression")
}
p.Expr = expressions.Value{Val: v.Name}
r, err := p.Eval(ctx, nil)
if err != nil {
return errors.Trace(err)
}
match, ok := r.(bool)
if !ok {
return errors.Errorf("Eval like pattern error")
}
if !match {
continue
}
}
row := []interface{}{v.Name, v.Value}
f(0, row)
}
}
return nil
}
Expand All @@ -170,8 +201,9 @@ func (s *ShowPlan) GetFields() []*field.ResultField {
names = []string{"Level", "Code", "Message"}
case stmt.ShowCharset:
names = []string{"Charset", "Description", "Default collation", "Maxlen"}
case stmt.ShowVariables:
names = []string{"Variable_name", "Value"}
}

fields := make([]*field.ResultField, 0, len(names))
for _, name := range names {
fields = append(fields, &field.ResultField{Name: name})
Expand Down
29 changes: 17 additions & 12 deletions rset/rsets/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package rsets

import (
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/plan"
"github.com/pingcap/tidb/plan/plans"
"github.com/pingcap/tidb/sessionctx/db"
Expand All @@ -26,23 +27,27 @@ var (

// ShowRset is record set to show.
type ShowRset struct {
Target int
DBName string
TableName string
ColumnName string
Flag int
Full bool
Target int
DBName string
TableName string
ColumnName string
Flag int
Full bool
GlobalScope bool
Pattern expression.Expression
}

// Plan gets ShowPlan.
func (r *ShowRset) Plan(ctx context.Context) (plan.Plan, error) {
return &plans.ShowPlan{
Target: r.Target,
DBName: r.getDBName(ctx),
TableName: r.TableName,
ColumnName: r.ColumnName,
Flag: r.Flag,
Full: r.Full,
Target: r.Target,
DBName: r.getDBName(ctx),
TableName: r.TableName,
ColumnName: r.ColumnName,
Flag: r.Flag,
Full: r.Full,
GlobalScope: r.GlobalScope,
Pattern: r.Pattern,
}, nil
}

Expand Down
18 changes: 10 additions & 8 deletions stmt/stmts/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ type ShowStmt struct {
Full bool

// Used by show variables
VarScope int
Pattern expression.Expression
GlobalScope bool
Pattern expression.Expression

Text string
}
Expand Down Expand Up @@ -68,12 +68,14 @@ func (s *ShowStmt) Exec(ctx context.Context) (_ rset.Recordset, err error) {
// TODO: finish this
log.Debug("Exec Show Stmt")
sr := &rsets.ShowRset{
Target: s.Target,
DBName: s.DBName,
TableName: s.TableIdent.Name.O,
ColumnName: s.ColumnName,
Flag: s.Flag,
Full: s.Full,
Target: s.Target,
DBName: s.DBName,
TableName: s.TableIdent.Name.O,
ColumnName: s.ColumnName,
Flag: s.Flag,
Full: s.Full,
GlobalScope: s.GlobalScope,
Pattern: s.Pattern,
}

r, err := sr.Plan(ctx)
Expand Down

0 comments on commit 20cf24f

Please sign in to comment.