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

For "set @@session.autocommit" replace on/1, off/0. #381

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ Makefile.main
.ci/
_example/main
_example/*.exe

go.sum
.vscode/
go.sum
14 changes: 14 additions & 0 deletions sql/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
showIndexRegex = regexp.MustCompile(`^show\s+(index|indexes|keys)\s+(from|in)\s+\S+\s*`)
describeRegex = regexp.MustCompile(`^(describe|desc|explain)\s+(.*)\s+`)
fullProcessListRegex = regexp.MustCompile(`^show\s+(full\s+)?processlist$`)
setAutocommitRegexp = regexp.MustCompile(`^set\s+(@+)session.autocommit\s*=`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is already support for set in go-mysql-server, there is no need to add a regexp for this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but because ON is a keyword, yacc Parse fails. That's why I first converts on/off to 1/0 and pass through regular workflow.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then maybe we can pre-process the query and replace the on/off with 1/0 before actually parsing it using sqlparser. There might be other queries that use on/off, this is very ad-hoc.

)

// Parse parses the given SQL sentence and returns the corresponding node.
Expand All @@ -61,6 +62,18 @@ func Parse(ctx *sql.Context, s string) (sql.Node, error) {
return parseDescribeQuery(ctx, s)
case fullProcessListRegex.MatchString(lowerQuery):
return plan.NewShowProcessList(), nil
case setAutocommitRegexp.MatchString(lowerQuery):
idx := setAutocommitRegexp.FindStringIndex(lowerQuery)
if idx == nil || len(idx) < 2 {
break
}
tail := strings.TrimSpace(lowerQuery[idx[1]:])
if strings.HasPrefix(tail, "on") {
tail = strings.Replace(tail, "on", "1", 1)
} else if strings.HasPrefix(tail, "off") {
tail = strings.Replace(tail, "off", "0", 1)
}
s = lowerQuery[0:idx[1]] + tail
}

stmt, err := sqlparser.Parse(s)
Expand Down Expand Up @@ -112,6 +125,7 @@ func convertSet(ctx *sql.Context, n *sqlparser.Set) (sql.Node, error) {
fmt.Println(strings.TrimSpace(strings.ToLower(e.Name.Qualifier.Name.String())))
switch strings.TrimSpace(strings.ToLower(e.Name.Qualifier.Name.String())) {
case "@@session", "": // do nothing

default:
return nil, ErrUnsupportedFeature.New("qualifiers in set variable names other than @@session")
}
Expand Down
43 changes: 43 additions & 0 deletions sql/parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,49 @@ var fixtures = map[string]sql.Node{
Value: expression.NewLiteral("bar", sql.Text),
},
),
`SET @@session.autocommit=ON`: plan.NewSet(
plan.SetVariable{
Name: "autocommit",
Value: expression.NewLiteral(int64(1), sql.Int64),
},
),

`SET @@session.autocommit=OFF`: plan.NewSet(
plan.SetVariable{
Name: "autocommit",
Value: expression.NewLiteral(int64(0), sql.Int64),
},
),

`SET @@session.autocommit = on, one="on", zero="off"`: plan.NewSet(
plan.SetVariable{
Name: "autocommit",
Value: expression.NewLiteral(int64(1), sql.Int64),
},
plan.SetVariable{
Name: "one",
Value: expression.NewLiteral("on", sql.Text),
},
plan.SetVariable{
Name: "zero",
Value: expression.NewLiteral("off", sql.Text),
},
),

`SET @@session.autocommit = off, one="on", zero="off"`: plan.NewSet(
plan.SetVariable{
Name: "autocommit",
Value: expression.NewLiteral(int64(0), sql.Int64),
},
plan.SetVariable{
Name: "one",
Value: expression.NewLiteral("on", sql.Text),
},
plan.SetVariable{
Name: "zero",
Value: expression.NewLiteral("off", sql.Text),
},
),
}

func TestParse(t *testing.T) {
Expand Down