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

Commit

Permalink
Merge pull request #383 from kuba--/fix-374/upgrade-vitess
Browse files Browse the repository at this point in the history
Support ON/OFF for SET autocommit
  • Loading branch information
ajnavarro authored Sep 28, 2018
2 parents 7f8e182 + 84d5bd9 commit a086daa
Show file tree
Hide file tree
Showing 16 changed files with 214 additions and 83 deletions.
2 changes: 1 addition & 1 deletion _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"gopkg.in/src-d/go-mysql-server.v0/mem"
"gopkg.in/src-d/go-mysql-server.v0/server"
"gopkg.in/src-d/go-mysql-server.v0/sql"
"gopkg.in/src-d/go-vitess.v0/mysql"
"gopkg.in/src-d/go-vitess.v1/mysql"
)

// Example of how to implement a MySQL server based on a Engine:
Expand Down
22 changes: 22 additions & 0 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,28 @@ func TestSessionVariables(t *testing.T) {
require.Equal([]sql.Row{{int64(1), ",STRICT_TRANS_TABLES"}}, rows)
}

func TestSessionVariablesONOFF(t *testing.T) {
require := require.New(t)

e := newEngine(t)

session := sql.NewBaseSession()
ctx := sql.NewContext(context.Background(), sql.WithSession(session), sql.WithPid(1))

_, _, err := e.Query(ctx, `set autocommit=ON, sql_mode = OFF, autoformat="true"`)
require.NoError(err)

ctx = sql.NewContext(context.Background(), sql.WithSession(session), sql.WithPid(2))

_, iter, err := e.Query(ctx, `SELECT @@autocommit, @@session.sql_mode, @@autoformat`)
require.NoError(err)

rows, err := sql.RowIterToRows(iter)
require.NoError(err)

require.Equal([]sql.Row{{int64(1), int64(0), true}}, rows)
}

func insertRows(t *testing.T, table sql.Inserter, rows ...sql.Row) {
t.Helper()

Expand Down
60 changes: 7 additions & 53 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,65 +1,19 @@
module gopkg.in/src-d/go-mysql-server.v0

require (
github.com/BurntSushi/toml v0.3.0 // indirect
github.com/CAFxX/gcnotifier v0.0.0-20170518020117-39b0596a2da3 // indirect
github.com/DataDog/datadog-go v0.0.0-20180822151419-281ae9f2d895 // indirect
github.com/OneOfOne/xxhash v1.2.2 // indirect
github.com/StackExchange/wmi v0.0.0-20180725035823-b12b22c5341f // indirect
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 // indirect
github.com/boltdb/bolt v1.3.1
github.com/cespare/xxhash v1.0.0 // indirect
github.com/circonus-labs/circonus-gometrics v2.2.1+incompatible // indirect
github.com/circonus-labs/circonusllhist v0.0.0-20180430145027-5eb751da55c6 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-ole/go-ole v1.2.1 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/gogo/protobuf v1.1.1 // indirect
github.com/google/go-cmp v0.2.0 // indirect
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/handlers v1.4.0 // indirect
github.com/gorilla/mux v1.6.2 // indirect
github.com/hashicorp/consul v1.2.3 // indirect
github.com/hashicorp/go-cleanhttp v0.5.0 // indirect
github.com/hashicorp/go-immutable-radix v1.0.0 // indirect
github.com/hashicorp/go-msgpack v0.0.0-20150518234257-fa3f63826f7c // indirect
github.com/hashicorp/go-multierror v1.0.0 // indirect
github.com/hashicorp/go-retryablehttp v0.0.0-20180718195005-e651d75abec6 // indirect
github.com/hashicorp/go-sockaddr v0.0.0-20180320115054-6d291a969b86 // indirect
github.com/hashicorp/memberlist v0.1.0 // indirect
github.com/hashicorp/serf v0.8.1 // indirect
github.com/hashicorp/yamux v0.0.0-20180917205041-7221087c3d28 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/miekg/dns v1.0.9 // indirect
github.com/mitchellh/hashstructure v0.0.0-20170609045927-2bca23e0e452
github.com/onsi/gomega v1.4.2 // indirect
github.com/mitchellh/hashstructure v1.0.0
github.com/opentracing/opentracing-go v1.0.2
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/pilosa/pilosa v1.1.0
github.com/pkg/errors v0.8.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v0.8.0 // indirect
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 // indirect
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e // indirect
github.com/prometheus/procfs v0.0.0-20180920065004-418d78d0b9a7 // indirect
github.com/satori/go.uuid v1.2.0
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 // indirect
github.com/shirou/gopsutil v2.17.12+incompatible // indirect
github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4 // indirect
github.com/sirupsen/logrus v1.0.6
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/sirupsen/logrus v1.1.0
github.com/spf13/cast v1.2.0
github.com/stretchr/testify v1.2.2
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926 // indirect
golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac // indirect
google.golang.org/appengine v1.2.0 // indirect
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 // indirect
google.golang.org/grpc v1.14.0 // indirect
gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect
golang.org/x/net v0.0.0-20180926154720-4dfa2610cdf3 // indirect
google.golang.org/grpc v1.15.0 // indirect
gopkg.in/src-d/go-errors.v1 v1.0.0
gopkg.in/src-d/go-vitess.v0 v0.0.0-20180222154500-2cb632cdef3c
gopkg.in/vmihailenco/msgpack.v2 v2.9.1 // indirect
gopkg.in/src-d/go-vitess.v1 v1.1.0
gopkg.in/yaml.v2 v2.2.1
)
2 changes: 1 addition & 1 deletion server/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

opentracing "github.com/opentracing/opentracing-go"
"gopkg.in/src-d/go-mysql-server.v0/sql"
"gopkg.in/src-d/go-vitess.v0/mysql"
"gopkg.in/src-d/go-vitess.v1/mysql"
)

// SessionBuilder creates sessions given a MySQL connection and a server address.
Expand Down
6 changes: 3 additions & 3 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"gopkg.in/src-d/go-mysql-server.v0/sql"

"github.com/sirupsen/logrus"
"gopkg.in/src-d/go-vitess.v0/mysql"
"gopkg.in/src-d/go-vitess.v0/sqltypes"
"gopkg.in/src-d/go-vitess.v0/vt/proto/query"
"gopkg.in/src-d/go-vitess.v1/mysql"
"gopkg.in/src-d/go-vitess.v1/sqltypes"
"gopkg.in/src-d/go-vitess.v1/vt/proto/query"
)

var regKillCmd = regexp.MustCompile(`^kill (?:(query|connection) )?(\d+)$`)
Expand Down
4 changes: 2 additions & 2 deletions server/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"gopkg.in/src-d/go-mysql-server.v0"
"gopkg.in/src-d/go-mysql-server.v0/mem"
"gopkg.in/src-d/go-mysql-server.v0/sql"
"gopkg.in/src-d/go-vitess.v0/mysql"
"gopkg.in/src-d/go-vitess.v0/sqltypes"
"gopkg.in/src-d/go-vitess.v1/mysql"
"gopkg.in/src-d/go-vitess.v1/sqltypes"

opentracing "github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/require"
Expand Down
22 changes: 20 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package server // import "gopkg.in/src-d/go-mysql-server.v0/server"

import (
"time"

opentracing "github.com/opentracing/opentracing-go"
"gopkg.in/src-d/go-mysql-server.v0"

"gopkg.in/src-d/go-vitess.v0/mysql"
"gopkg.in/src-d/go-vitess.v1/mysql"
)

const (
DefaultConnReadTimeout = 30 * time.Second
DefaultConnWriteTimeout = 60 * time.Second
)

// Server is a MySQL server for SQLe engines.
Expand All @@ -23,6 +30,9 @@ type Config struct {
// Tracer to use in the server. By default, a noop tracer will be used if
// no tracer is provided.
Tracer opentracing.Tracer

ConnReadTimeout time.Duration
ConnWriteTimeout time.Duration
}

// NewDefaultServer creates a Server with the default session builder.
Expand All @@ -40,8 +50,16 @@ func NewServer(cfg Config, e *sqle.Engine, sb SessionBuilder) (*Server, error) {
tracer = opentracing.NoopTracer{}
}

if cfg.ConnReadTimeout == 0 {
cfg.ConnReadTimeout = DefaultConnReadTimeout
}

if cfg.ConnWriteTimeout == 0 {
cfg.ConnWriteTimeout = DefaultConnWriteTimeout
}

handler := NewHandler(e, NewSessionManager(sb, tracer, cfg.Address))
l, err := mysql.NewListener(cfg.Protocol, cfg.Address, cfg.Auth, handler)
l, err := mysql.NewListener(cfg.Protocol, cfg.Address, cfg.Auth, handler, cfg.ConnReadTimeout, cfg.ConnWriteTimeout)
if err != nil {
return nil, err
}
Expand Down
15 changes: 12 additions & 3 deletions sql/analyzer/resolve_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"gopkg.in/src-d/go-mysql-server.v0/sql"
"gopkg.in/src-d/go-mysql-server.v0/sql/expression"
"gopkg.in/src-d/go-mysql-server.v0/sql/plan"
"gopkg.in/src-d/go-vitess.v1/vt/sqlparser"
)

// deferredColumn is a wrapper on UnresolvedColumn used only to defer the
Expand Down Expand Up @@ -190,17 +191,25 @@ func resolveColumns(ctx *sql.Context, a *Analyzer, n sql.Node) (sql.Node, error)
return e, nil
}

const (
sessionTable = "@@" + sqlparser.SessionStr
sessionPrefix = sqlparser.SessionStr + "."
)
columns, ok := colMap[uc.Name()]
if !ok {
switch uc := uc.(type) {
case *expression.UnresolvedColumn:
if isGlobalOrSessionColumn(uc) {
if uc.Table() != "" && strings.ToLower(uc.Table()) != "@@session" {
if uc.Table() != "" && strings.ToLower(uc.Table()) != sessionTable {
return nil, errGlobalVariablesNotSupported.New(uc)
}

typ, value := ctx.Get(strings.TrimLeft(uc.Name(), "@"))
return expression.NewGetSessionField(uc.Name(), typ, value), nil
name := strings.TrimLeft(uc.Name(), "@")
if strings.HasPrefix(name, sessionPrefix) {
name = name[len(sessionPrefix):]
}
typ, value := ctx.Get(name)
return expression.NewGetSessionField(name, typ, value), nil
}

a.Log("evaluation of column %q was deferred", uc.Name())
Expand Down
33 changes: 29 additions & 4 deletions sql/analyzer/resolve_columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,44 @@ func TestQualifyColumns(t *testing.T) {

table := mem.NewTable("mytable", sql.Schema{{Name: "i", Type: sql.Int32}})
table2 := mem.NewTable("mytable2", sql.Schema{{Name: "i", Type: sql.Int32}})
sessionTable := mem.NewTable("@@session", sql.Schema{{Name: "autocommit", Type: sql.Int64}})

node := plan.NewProject(
[]sql.Expression{
expression.NewUnresolvedColumn("@@autocommit"),
},
plan.NewResolvedTable(sessionTable),
)
col, ok := node.Projections[0].(*expression.UnresolvedColumn)
require.True(ok)
require.Truef(isGlobalOrSessionColumn(col), "@@autocommit is not session column")

expected := plan.NewProject(
[]sql.Expression{
expression.NewUnresolvedQualifiedColumn("", "@@autocommit"),
},
plan.NewResolvedTable(sessionTable),
)

result, err := f.Apply(sql.NewEmptyContext(), nil, node)
require.NoError(err)
require.Equal(expected, result)

node = plan.NewProject(
[]sql.Expression{
expression.NewUnresolvedColumn("i"),
},
plan.NewResolvedTable(table),
)

expected := plan.NewProject(
expected = plan.NewProject(
[]sql.Expression{
expression.NewUnresolvedQualifiedColumn("mytable", "i"),
},
plan.NewResolvedTable(table),
)

result, err := f.Apply(sql.NewEmptyContext(), nil, node)
result, err = f.Apply(sql.NewEmptyContext(), nil, node)
require.NoError(err)
require.Equal(expected, result)

Expand Down Expand Up @@ -204,11 +226,13 @@ func TestResolveColumnsSession(t *testing.T) {

ctx := sql.NewContext(context.Background(), sql.WithSession(sql.NewBaseSession()))
ctx.Set("foo_bar", sql.Int64, int64(42))
ctx.Set("autocommit", sql.Boolean, true)

node := plan.NewProject(
[]sql.Expression{
expression.NewUnresolvedColumn("@@foo_bar"),
expression.NewUnresolvedColumn("@@bar_baz"),
expression.NewUnresolvedColumn("@@autocommit"),
},
plan.NewResolvedTable(dualTable),
)
Expand All @@ -218,8 +242,9 @@ func TestResolveColumnsSession(t *testing.T) {

expected := plan.NewProject(
[]sql.Expression{
expression.NewGetSessionField("@@foo_bar", sql.Int64, int64(42)),
expression.NewGetSessionField("@@bar_baz", sql.Null, nil),
expression.NewGetSessionField("foo_bar", sql.Int64, int64(42)),
expression.NewGetSessionField("bar_baz", sql.Null, nil),
expression.NewGetSessionField("autocommit", sql.Boolean, true),
},
plan.NewResolvedTable(dualTable),
)
Expand Down
2 changes: 1 addition & 1 deletion sql/expression/arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

errors "gopkg.in/src-d/go-errors.v1"
"gopkg.in/src-d/go-vitess.v0/vt/sqlparser"
"gopkg.in/src-d/go-vitess.v1/vt/sqlparser"

"gopkg.in/src-d/go-mysql-server.v0/sql"
)
Expand Down
2 changes: 1 addition & 1 deletion sql/parse/indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"gopkg.in/src-d/go-mysql-server.v0/sql/plan"

"gopkg.in/src-d/go-mysql-server.v0/sql"
"gopkg.in/src-d/go-vitess.v0/vt/sqlparser"
"gopkg.in/src-d/go-vitess.v1/vt/sqlparser"
)

func parseShowIndex(s string) (sql.Node, error) {
Expand Down
Loading

0 comments on commit a086daa

Please sign in to comment.