Skip to content

Commit

Permalink
Merge pull request vitessio#6069 from planetscale/set-plan-sysvar
Browse files Browse the repository at this point in the history
Planning Set Statement for System Variable with Ignore and CheckAndIgnore
  • Loading branch information
harshit-gangal authored Apr 16, 2020
2 parents 97f4aff + 470a63c commit 48020c8
Show file tree
Hide file tree
Showing 17 changed files with 737 additions and 173 deletions.
101 changes: 101 additions & 0 deletions go/test/endtoend/vtgate/setstatement/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
Copyright 2020 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package setstatement

import (
"flag"
"os"
"testing"

"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/test/endtoend/cluster"
)

var (
clusterInstance *cluster.LocalProcessCluster
keyspaceName = "ks"
cell = "zone1"
hostname = "localhost"
sqlSchema = `
create table test(
id bigint,
val1 varchar(16),
val2 int,
val3 float,
primary key(id)
)Engine=InnoDB;`

vSchema = `
{
"sharded":true,
"vindexes": {
"hash_index": {
"type": "hash"
}
},
"tables": {
"test":{
"column_vindexes": [
{
"column": "id",
"name": "hash_index"
}
]
}
}
}
`
)

func TestMain(m *testing.M) {
defer cluster.PanicHandler(nil)
flag.Parse()

exitCode := func() int {
clusterInstance = cluster.NewCluster(cell, hostname)
defer clusterInstance.Teardown()

// Start topo server
if err := clusterInstance.StartTopo(); err != nil {
return 1
}

// Start keyspace
keyspace := &cluster.Keyspace{
Name: keyspaceName,
SchemaSQL: sqlSchema,
VSchema: vSchema,
}
if err := clusterInstance.StartKeyspace(*keyspace, []string{"-80", "80-"}, 1, false); err != nil {
return 1
}

// Start vtgate
if err := clusterInstance.StartVtgate(); err != nil {
return 1
}

return m.Run()
}()
os.Exit(exitCode)
}

func exec(t *testing.T, conn *mysql.Conn, query string) (*sqltypes.Result, error) {
t.Helper()
return conn.ExecuteFetch(query, 1000, true)
}
89 changes: 89 additions & 0 deletions go/test/endtoend/vtgate/setstatement/sysvar_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright 2020 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package setstatement

import (
"context"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"

"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/test/endtoend/cluster"
)

func TestSetSysVar(t *testing.T) {
defer cluster.PanicHandler(t)
ctx := context.Background()
vtParams := mysql.ConnParams{
Host: "localhost",
Port: clusterInstance.VtgateMySQLPort,
}
type queriesWithExpectations struct {
query string
expectedRows string
rowsAffected int
errMsg string
expectedWarning string
}

queries := []queriesWithExpectations{{
query: `set @@default_storage_engine = INNODB`,
expectedRows: ``, rowsAffected: 0,
expectedWarning: "[[VARCHAR(\"Warning\") UINT16(1235) VARCHAR(\"Ignored inapplicable SET default_storage_engine = INNODB\")]]",
}, {
query: `set @@sql_mode = @@sql_mode`,
expectedRows: ``, rowsAffected: 0,
}, {
query: `set @@sql_mode = concat(@@sql_mode,"")`,
expectedRows: ``, rowsAffected: 0,
}, {
query: `set @@sql_mode = concat(@@sql_mode,"ALLOW_INVALID_DATES")`,
errMsg: "Modification not allowed using set construct for: sql_mode",
}}

conn, err := mysql.Connect(ctx, &vtParams)
require.NoError(t, err)
defer conn.Close()

for i, q := range queries {
t.Run(fmt.Sprintf("%d-%s", i, q.query), func(t *testing.T) {
qr, err := exec(t, conn, q.query)
if q.errMsg != "" {
require.Contains(t, err.Error(), q.errMsg)
} else {
require.NoError(t, err)
require.Equal(t, uint64(q.rowsAffected), qr.RowsAffected, "rows affected wrong for query: %s", q.query)
if q.expectedRows != "" {
result := fmt.Sprintf("%v", qr.Rows)
if diff := cmp.Diff(q.expectedRows, result); diff != "" {
t.Errorf("%s\nfor query: %s", diff, q.query)
}
}
if q.expectedWarning != "" {
qr, err := exec(t, conn, "show warnings")
require.NoError(t, err)
if got, want := fmt.Sprintf("%v", qr.Rows), q.expectedWarning; got != want {
t.Errorf("select:\n%v want\n%v", got, want)
}
}
}
})
}
}
104 changes: 20 additions & 84 deletions go/test/endtoend/vtgate/setstatement/udv_test.go
Original file line number Diff line number Diff line change
@@ -1,99 +1,34 @@
/*
Copyright 2020 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package setstatement

import (
"context"
"flag"
"fmt"
"os"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"

"github.com/stretchr/testify/require"

"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/test/endtoend/cluster"
)

var (
clusterInstance *cluster.LocalProcessCluster
keyspaceName = "ks"
cell = "zone1"
hostname = "localhost"
sqlSchema = `
create table test(
id bigint,
val1 varchar(16),
val2 int,
val3 float,
primary key(id)
)Engine=InnoDB;`

vSchema = `
{
"sharded":true,
"vindexes": {
"hash_index": {
"type": "hash"
}
},
"tables": {
"test":{
"column_vindexes": [
{
"column": "id",
"name": "hash_index"
}
]
}
}
}
`
)

func TestMain(m *testing.M) {
defer cluster.PanicHandler(nil)
flag.Parse()

exitCode := func() int {
clusterInstance = cluster.NewCluster(cell, hostname)
defer clusterInstance.Teardown()

// Start topo server
if err := clusterInstance.StartTopo(); err != nil {
return 1
}

// Start keyspace
keyspace := &cluster.Keyspace{
Name: keyspaceName,
SchemaSQL: sqlSchema,
VSchema: vSchema,
}
if err := clusterInstance.StartKeyspace(*keyspace, []string{"-80", "80-"}, 1, false); err != nil {
return 1
}

// Start vtgate
if err := clusterInstance.StartVtgate(); err != nil {
return 1
}

return m.Run()
}()
os.Exit(exitCode)
}

func exec(t *testing.T, conn *mysql.Conn, query string) *sqltypes.Result {
t.Helper()
qr, err := conn.ExecuteFetch(query, 1000, true)
require.Nil(t, err)
return qr
}

func TestSet(t *testing.T) {
func TestSetUDV(t *testing.T) {
defer cluster.PanicHandler(t)
ctx := context.Background()
vtParams := mysql.ConnParams{
Expand Down Expand Up @@ -150,8 +85,9 @@ func TestSet(t *testing.T) {

for i, q := range queries {
t.Run(fmt.Sprintf("%d-%s", i, q.query), func(t *testing.T) {
qr := exec(t, conn, q.query)
assert.Equal(t, uint64(q.rowsAffected), qr.RowsAffected, "rows affected wrong for query: %s", q.query)
qr, err := exec(t, conn, q.query)
require.NoError(t, err)
require.Equal(t, uint64(q.rowsAffected), qr.RowsAffected, "rows affected wrong for query: %s", q.query)
if q.expectedRows != "" {
result := fmt.Sprintf("%v", qr.Rows)
if diff := cmp.Diff(q.expectedRows, result); diff != "" {
Expand Down
8 changes: 7 additions & 1 deletion go/vt/vtgate/engine/fake_vcursor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ func (t noopVCursor) SetUDV(key string, value interface{}) error {
func (t noopVCursor) ExecuteVSchema(keyspace string, vschemaDDL *sqlparser.DDL) error {
panic("implement me")
}

func (t noopVCursor) Session() SessionActions {
return t
}
func (t noopVCursor) SetTarget(target string) error {
panic("implement me")
}
Expand Down Expand Up @@ -136,6 +138,10 @@ func (f *loggingVCursor) ExecuteVSchema(keyspace string, vschemaDDL *sqlparser.D
panic("implement me")
}

func (f *loggingVCursor) Session() SessionActions {
return f
}

func (f *loggingVCursor) SetTarget(target string) error {
f.log = append(f.log, fmt.Sprintf("Target set to %s", target))
return nil
Expand Down
Loading

0 comments on commit 48020c8

Please sign in to comment.