Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

sql/*: implement show collation #587

Merged
merged 1 commit into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,26 @@ var queries = []struct {
{"mytable"},
},
},
{
`SHOW COLLATION`,
[]sql.Row{{"utf8_bin", "utf8mb4", int64(1), "Yes", "Yes", int64(1)}},
},
{
`SHOW COLLATION LIKE 'foo'`,
[]sql.Row{},
},
{
`SHOW COLLATION LIKE 'utf8%'`,
[]sql.Row{{"utf8_bin", "utf8mb4", int64(1), "Yes", "Yes", int64(1)}},
},
{
`SHOW COLLATION WHERE charset = 'foo'`,
[]sql.Row{},
},
{
"SHOW COLLATION WHERE `Default` = 'Yes'",
[]sql.Row{{"utf8_bin", "utf8mb4", int64(1), "Yes", "Yes", int64(1)}},
},
}

func TestQueries(t *testing.T) {
Expand Down
60 changes: 60 additions & 0 deletions sql/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
showCreateRegex = regexp.MustCompile(`^show create\s+\S+\s*`)
showVariablesRegex = regexp.MustCompile(`^show\s+(.*)?variables\s*`)
showWarningsRegex = regexp.MustCompile(`^show\s+warnings\s*`)
showCollationRegex = regexp.MustCompile(`^show\s+collation\s*`)
describeRegex = regexp.MustCompile(`^(describe|desc|explain)\s+(.*)\s+`)
fullProcessListRegex = regexp.MustCompile(`^show\s+(full\s+)?processlist$`)
unlockTablesRegex = regexp.MustCompile(`^unlock\s+tables$`)
Expand Down Expand Up @@ -82,6 +83,8 @@ func Parse(ctx *sql.Context, query string) (sql.Node, error) {
return parseShowVariables(ctx, s)
case showWarningsRegex.MatchString(lowerQuery):
return parseShowWarnings(ctx, s)
case showCollationRegex.MatchString(lowerQuery):
return parseShowCollation(s)
case describeRegex.MatchString(lowerQuery):
return parseDescribeQuery(ctx, s)
case fullProcessListRegex.MatchString(lowerQuery):
Expand Down Expand Up @@ -1218,6 +1221,63 @@ func parseShowTableStatus(query string) (sql.Node, error) {
}
}

func parseShowCollation(query string) (sql.Node, error) {
buf := bufio.NewReader(strings.NewReader(query))
err := parseFuncs{
expect("show"),
skipSpaces,
expect("collation"),
skipSpaces,
}.exec(buf)

if err != nil {
return nil, err
}

if _, err = buf.Peek(1); err == io.EOF {
return plan.NewShowCollation(), nil
}

var clause string
if err := readIdent(&clause)(buf); err != nil {
return nil, err
}

if err := skipSpaces(buf); err != nil {
return nil, err
}

switch strings.ToUpper(clause) {
case "WHERE", "LIKE":
bs, err := ioutil.ReadAll(buf)
if err != nil {
return nil, err
}

expr, err := parseExpr(string(bs))
if err != nil {
return nil, err
}

var filter sql.Expression
if strings.ToUpper(clause) == "LIKE" {
filter = expression.NewLike(
expression.NewUnresolvedColumn("collation"),
expr,
)
} else {
filter = expr
}

return plan.NewFilter(
filter,
plan.NewShowCollation(),
), nil
default:
return nil, errUnexpectedSyntax.New("one of: LIKE or WHERE", clause)
}
}

var fixSessionRegex = regexp.MustCompile(`(,\s*|(set|SET)\s+)(SESSION|session)\s+([a-zA-Z0-9_]+)\s*=`)
var fixGlobalRegex = regexp.MustCompile(`(,\s*|(set|SET)\s+)(GLOBAL|global)\s+([a-zA-Z0-9_]+)\s*=`)

Expand Down
15 changes: 15 additions & 0 deletions sql/parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,21 @@ var fixtures = map[string]sql.Node{
)},
plan.NewUnresolvedTable("dual", ""),
),
"SHOW COLLATION": plan.NewShowCollation(),
"SHOW COLLATION LIKE 'foo'": plan.NewFilter(
expression.NewLike(
expression.NewUnresolvedColumn("collation"),
expression.NewLiteral("foo", sql.Text),
),
plan.NewShowCollation(),
),
"SHOW COLLATION WHERE Charset = 'foo'": plan.NewFilter(
expression.NewEquals(
expression.NewUnresolvedColumn("charset"),
expression.NewLiteral("foo", sql.Text),
),
plan.NewShowCollation(),
),
}

func TestParse(t *testing.T) {
Expand Down
53 changes: 53 additions & 0 deletions sql/plan/show_collation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package plan

import "gopkg.in/src-d/go-mysql-server.v0/sql"

// ShowCollation shows all available collations.
type ShowCollation struct{}

var collationSchema = sql.Schema{
{Name: "Collation", Type: sql.Text},
{Name: "Charset", Type: sql.Text},
{Name: "Id", Type: sql.Int64},
{Name: "Default", Type: sql.Text},
{Name: "Compiled", Type: sql.Text},
{Name: "Sortlen", Type: sql.Int64},
}

// NewShowCollation creates a new ShowCollation node.
func NewShowCollation() ShowCollation {
return ShowCollation{}
}

// Children implements the sql.Node interface.
func (ShowCollation) Children() []sql.Node { return nil }

func (ShowCollation) String() string { return "SHOW COLLATION" }

// Resolved implements the sql.Node interface.
func (ShowCollation) Resolved() bool { return true }

// RowIter implements the sql.Node interface.
func (ShowCollation) RowIter(ctx *sql.Context) (sql.RowIter, error) {
return sql.RowsToRowIter(sql.Row{
defaultCollation,
defaultCharacterSet,
int64(1),
"Yes",
"Yes",
int64(1),
}), nil
}

// Schema implements the sql.Node interface.
func (ShowCollation) Schema() sql.Schema { return collationSchema }

// TransformUp implements the sql.Node interface.
func (ShowCollation) TransformUp(f sql.TransformNodeFunc) (sql.Node, error) {
return f(ShowCollation{})
}

// TransformExpressionsUp implements the sql.Node interface.
func (ShowCollation) TransformExpressionsUp(f sql.TransformExprFunc) (sql.Node, error) {
return ShowCollation{}, nil
}
6 changes: 3 additions & 3 deletions sql/plan/showwarnings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ func TestShowWarnings(t *testing.T) {
require := require.New(t)

ctx := sql.NewEmptyContext()
ctx.Session.Warn(&sql.Warning{"l1", "w1", 1})
ctx.Session.Warn(&sql.Warning{"l2", "w2", 2})
ctx.Session.Warn(&sql.Warning{"l4", "w3", 3})
ctx.Session.Warn(&sql.Warning{Level: "l1", Message: "w1", Code: 1})
ctx.Session.Warn(&sql.Warning{Level: "l2", Message: "w2", Code: 2})
ctx.Session.Warn(&sql.Warning{Level: "l4", Message: "w3", Code: 3})

sw := ShowWarnings(ctx.Session.Warnings())
require.True(sw.Resolved())
Expand Down