Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
GuptaManan100 committed Oct 29, 2024
1 parent 804ad78 commit e2921ce
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 29 deletions.
47 changes: 39 additions & 8 deletions go/test/endtoend/vtgate/queries/plan_tests/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import (
_ "embed"
"encoding/json"
"fmt"
"github.com/stretchr/testify/require"
"os"
"path/filepath"
"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 {
func readJSONTests(filename string) []planbuilder.PlanTest {
var output []planbuilder.PlanTest
file, err := os.Open(locateFile(filename))
if err != nil {
Expand All @@ -35,9 +36,11 @@ func locateFile(name string) string {
func TestPlan(t *testing.T) {
mcmp, closer := start(t)
defer closer()
tests := ReadJSONTests("select_cases.json")
tests := readJSONTests("select_cases.json")
for _, test := range tests {
mcmp.Exec(test.Query)
mcmp.Run(test.Query, func(mcmp *utils.MySQLCompare) {
mcmp.Exec(test.Query)
})
}
}

Expand All @@ -50,8 +53,6 @@ var (
)

func readFile(filename string) string {
clean := filepath.Clean(locateFile(filename))
fmt.Println(clean)
schema, err := os.ReadFile(locateFile(filename))
if err != nil {
fmt.Println(err)
Expand All @@ -68,11 +69,36 @@ func start(t *testing.T) (utils.MySQLCompare, func()) {
}
}

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")
}

// 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 := readFile("vschemas/schema.json")
vschema := extractUserKS(readFile("vschemas/schema.json"))

exitCode := func() int {
clusterInstance = cluster.NewCluster(cell, "localhost")
Expand All @@ -95,6 +121,11 @@ func TestMain(m *testing.M) {
return 1
}

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

// Start vtgate
err = clusterInstance.StartVtgate()
if err != nil {
Expand Down
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
92 changes: 92 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/vschemas/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
CREATE TABLE user
(
id INT PRIMARY KEY,
col BIGINT,
predef1 VARCHAR(255),
predef2 VARCHAR(255),
textcol1 VARCHAR(255),
intcol BIGINT,
textcol2 VARCHAR(255)
);

CREATE TABLE user_metadata
(
user_id INT,
email VARCHAR(255),
address VARCHAR(255),
md5 VARCHAR(255),
non_planable VARCHAR(255),
PRIMARY KEY (user_id)
);

CREATE TABLE music
(
user_id INT,
id INT,
PRIMARY KEY (user_id)
);

CREATE TABLE samecolvin
(
col VARCHAR(255),
PRIMARY KEY (col)
);

CREATE TABLE multicolvin
(
kid INT,
column_a VARCHAR(255),
column_b VARCHAR(255),
column_c VARCHAR(255),
PRIMARY KEY (kid)
);

CREATE TABLE customer
(
id INT,
email VARCHAR(255),
phone VARCHAR(255),
PRIMARY KEY (id)
);

CREATE TABLE multicol_tbl
(
cola VARCHAR(255),
colb VARCHAR(255),
colc VARCHAR(255),
name VARCHAR(255),
PRIMARY KEY (cola, colb)
);

CREATE TABLE mixed_tbl
(
shard_key VARCHAR(255),
lkp_key VARCHAR(255),
PRIMARY KEY (shard_key)
);

CREATE TABLE pin_test
(
id INT PRIMARY KEY
);

CREATE TABLE cfc_vindex_col
(
c1 VARCHAR(255),
c2 VARCHAR(255),
PRIMARY KEY (c1)
);

CREATE TABLE unq_lkp_idx
(
unq_key INT PRIMARY KEY,
keyspace_id VARCHAR(255)
);

CREATE TABLE t1
(
c1 INT,
c2 INT,
c3 INT,
PRIMARY KEY (c1)
);

0 comments on commit e2921ce

Please sign in to comment.