Skip to content

Commit

Permalink
test wip: run plan tests end2end
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Oct 29, 2024
1 parent c21d1de commit 31229d9
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 36 deletions.
162 changes: 162 additions & 0 deletions go/test/endtoend/vtgate/queries/plan_tests/plan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package plan_tests

import (
_ "embed"
"encoding/json"
"fmt"
"os"
"testing"

"github.com/stretchr/testify/require"

"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/test/endtoend/cluster"
"vitess.io/vitess/go/test/endtoend/utils"
"vitess.io/vitess/go/vt/vtgate/planbuilder"
)

func readJSONTests(filename string) []planbuilder.PlanTest {
var output []planbuilder.PlanTest
file, err := os.Open(locateFile(filename))
if err != nil {
panic(err)
}
dec := json.NewDecoder(file)
err = dec.Decode(&output)
if err != nil {
panic(err)
}
return output
}

func locateFile(name string) string {
return "../../../../../vt/vtgate/planbuilder/testdata/" + name
}

func TestPlan(t *testing.T) {
mcmp, closer := start(t)
defer closer()
tests := readJSONTests("select_cases.json")
for _, test := range tests {
mcmp.Run(test.Query, func(mcmp *utils.MySQLCompare) {
mcmp.Exec(test.Query)
})
}
}

var (
clusterInstance *cluster.LocalProcessCluster
vtParams mysql.ConnParams
mysqlParams mysql.ConnParams
keyspaceName = "user"
cell = "test_aggr"
)

func readFile(filename string) string {
schema, err := os.ReadFile(locateFile(filename))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return string(schema)
}

func start(t *testing.T) (utils.MySQLCompare, func()) {
mcmp, err := utils.NewMySQLCompare(t, vtParams, mysqlParams)
require.NoError(t, err)
return mcmp, func() {
mcmp.Close()
}
}

func extractUserKS(jsonString string) string {
var result map[string]any
if err := json.Unmarshal([]byte(jsonString), &result); err != nil {
panic(err.Error())
}

keyspaces, ok := result["keyspaces"].(map[string]any)
if !ok {
panic("Keyspaces not found")
}

user, ok := keyspaces["user"].(map[string]any)
if !ok {
panic("User keyspaces not found")
}

tables, ok := user["tables"].(map[string]any)
if !ok {
panic("Tables not found")
}

userTbl, ok := tables["user"].(map[string]any)
if !ok {
panic("User table not found")
}

delete(userTbl, "auto_increment") // TODO: we should have an unsharded keyspace where this could live

// Marshal the inner part back to JSON string
userJson, err := json.Marshal(user)
if err != nil {
panic(err.Error())
}

return string(userJson)
}

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

schemaSQL := readFile("vschemas/schema.sql")
vschema := extractUserKS(readFile("vschemas/schema.json"))

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

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

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

// TODO: (@GuptaManan100/@systay): Also run the tests with normalizer on.
clusterInstance.VtGateExtraArgs = append(clusterInstance.VtGateExtraArgs,
"--normalize_queries=false",
"--schema_change_signal=false",
)

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

vtParams = clusterInstance.GetVTParams(keyspaceName)

// create mysql instance and connection parameters
conn, closer, err := utils.NewMySQL(clusterInstance, keyspaceName, schemaSQL)
if err != nil {
fmt.Println(err)
return 1
}
defer closer()
mysqlParams = conn

return m.Run()
}()
os.Exit(exitCode)
}
21 changes: 6 additions & 15 deletions go/vt/vtgate/planbuilder/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,21 +659,12 @@ func createFkDefinition(childCols []string, parentTableName string, parentCols [
}
}

type (
planTest struct {
Comment string `json:"comment,omitempty"`
Query string `json:"query,omitempty"`
Plan json.RawMessage `json:"plan,omitempty"`
Skip bool `json:"skip,omitempty"`
}
)

func (s *planTestSuite) testFile(filename string, vschema *vschemawrapper.VSchemaWrapper, render bool) {
opts := jsondiff.DefaultConsoleOptions()

s.T().Run(filename, func(t *testing.T) {
failed := false
var expected []planTest
var expected []PlanTest
for _, tcase := range readJSONTests(filename) {
testName := tcase.Comment
if testName == "" {
Expand All @@ -682,7 +673,7 @@ func (s *planTestSuite) testFile(filename string, vschema *vschemawrapper.VSchem
if tcase.Query == "" {
continue
}
current := planTest{
current := PlanTest{
Comment: testName,
Query: tcase.Query,
}
Expand Down Expand Up @@ -730,8 +721,8 @@ func (s *planTestSuite) testFile(filename string, vschema *vschemawrapper.VSchem
})
}

func readJSONTests(filename string) []planTest {
var output []planTest
func readJSONTests(filename string) []PlanTest {
var output []PlanTest
file, err := os.Open(locateFile(filename))
if err != nil {
panic(err)
Expand All @@ -745,7 +736,7 @@ func readJSONTests(filename string) []planTest {
return output
}

func getPlanOutput(tcase planTest, vschema *vschemawrapper.VSchemaWrapper, render bool) (out string) {
func getPlanOutput(tcase PlanTest, vschema *vschemawrapper.VSchemaWrapper, render bool) (out string) {
defer func() {
if r := recover(); r != nil {
out = fmt.Sprintf("panicked: %v\n%s", r, string(debug.Stack()))
Expand Down Expand Up @@ -887,7 +878,7 @@ func BenchmarkBaselineVsMirrored(b *testing.B) {
})
}

func benchmarkPlanner(b *testing.B, version plancontext.PlannerVersion, testCases []planTest, vschema *vschemawrapper.VSchemaWrapper) {
func benchmarkPlanner(b *testing.B, version plancontext.PlannerVersion, testCases []PlanTest, vschema *vschemawrapper.VSchemaWrapper) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
for _, tcase := range testCases {
Expand Down
27 changes: 27 additions & 0 deletions go/vt/vtgate/planbuilder/test_helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2024 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 planbuilder

import "encoding/json"

type PlanTest struct {
Comment string `json:"comment,omitempty"`
Query string `json:"query,omitempty"`
Plan json.RawMessage `json:"plan,omitempty"`
Skip bool `json:"skip,omitempty"`
SkipE2E bool `json:"skip_e2e,omitempty"`
}
48 changes: 27 additions & 21 deletions go/vt/vtgate/planbuilder/testdata/vschemas/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,34 +58,34 @@
"sharded": true,
"vindexes": {
"user_index": {
"type": "hash_test",
"type": "hash",
"owner": "user"
},
"kid_index": {
"type": "hash_test",
"type": "hash",
"owner": "multicolvin"
},
"user_md5_index": {
"type": "unicode_loose_md5"
},
"music_user_map": {
"type": "lookup_test",
"type": "lookup_unique",
"owner": "music"
},
"cola_map": {
"type": "lookup_test",
"type": "lookup_unique",
"owner": "multicolvin"
},
"colb_colc_map": {
"type": "lookup_test",
"type": "lookup_unique",
"owner": "multicolvin"
},
"cola_kid_map": {
"type": "lookup_test",
"type": "lookup_unique",
"owner": "overlap_vindex"
},
"name_user_map": {
"type": "name_lkp_test",
"type": "lookup_hash",
"owner": "user",
"params": {
"table": "name_user_vdx",
Expand All @@ -94,41 +94,44 @@
}
},
"email_user_map": {
"type": "lookup_test",
"type": "lookup_unique",
"owner": "user_metadata"
},
"address_user_map": {
"type": "lookup_test",
"type": "lookup_unique",
"owner": "user_metadata"
},
"costly_map": {
"type": "costly",
"type": "lookup_unique",
"owner": "user"
},
"hash_dup": {
"type": "hash_test",
"type": "hash",
"owner": "user"
},
"vindex1": {
"type": "hash_test",
"type": "hash",
"owner": "samecolvin"
},
"vindex2": {
"type": "lookup_test",
"type": "lookup_unique",
"owner": "samecolvin"
},
"cfc": {
"type": "cfc"
},
"multicolIdx": {
"type": "multiCol_test"
"type": "multicol",
"params": {
"column_count": "2"
}
},
"colc_map": {
"type": "lookup_test",
"type": "lookup_unique",
"owner": "multicol_tbl"
},
"name_muticoltbl_map": {
"type": "name_lkp_test",
"type": "lookup_hash",
"owner": "multicol_tbl"
},
"non_planable_user_map": {
Expand All @@ -141,7 +144,7 @@
"owner": "user_metadata"
},
"lkp_shard_map": {
"type": "name_lkp_test",
"type": "lookup_hash",
"owner": "mixed_tbl",
"params": {
"table": "lkp_shard_vdx",
Expand All @@ -153,7 +156,7 @@
"type": "xxhash"
},
"unq_lkp_bf_vdx": {
"type": "unq_lkp_test",
"type": "lookup_unique",
"owner": "customer",
"params": {
"table": "unq_lkp_idx",
Expand All @@ -164,7 +167,7 @@
}
},
"unq_lkp_vdx": {
"type": "unq_lkp_test",
"type": "lookup_unique",
"owner": "customer",
"params": {
"table": "unq_lkp_idx",
Expand All @@ -174,7 +177,7 @@
}
},
"lkp_bf_vdx": {
"type": "name_lkp_test",
"type": "lookup_hash",
"owner": "customer",
"params": {
"table": "lkp_shard_vdx",
Expand Down Expand Up @@ -641,7 +644,10 @@
"type": "hash_test"
},
"multicolIdx": {
"type": "multiCol_test"
"type": "multicol",
"params": {
"column_count": "3"
}
}
},
"tables": {
Expand Down
Loading

0 comments on commit 31229d9

Please sign in to comment.