Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving random query generation for endtoend testing #13460

Merged
merged 29 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
3d10df5
added flag for if the randomly generated query can have aggregation
arvind-murty Jun 30, 2023
b40e319
added ExprGeneratorConfig to random expression generation
arvind-murty Jul 4, 2023
54368c6
refactor tableT struct
arvind-murty Jul 4, 2023
0b07313
simplified random predicate generation
arvind-murty Jul 6, 2023
f14943d
refactor column struct
arvind-murty Jul 8, 2023
e005e78
added aggregation to random expressions and changed random query gene…
arvind-murty Jul 10, 2023
8e28e60
added queryGen struct to pass along Rand and ExprGeneratorConfig
arvind-murty Jul 11, 2023
f32f5dd
added random column aliases
arvind-murty Jul 13, 2023
e92b3c2
added more complex aggregate expressions
arvind-murty Jul 13, 2023
1953053
added scalar subqueries to fuzzer
arvind-murty Jul 16, 2023
bf53801
added scalar subqueries to all random expression generation for the f…
arvind-murty Jul 17, 2023
98055b7
added support for tuple random expressions
arvind-murty Jul 18, 2023
dccd6ec
added support for random expressions with exists
arvind-murty Jul 19, 2023
35e045b
added union queries in fuzzer
arvind-murty Jul 20, 2023
13c89ca
added tuple comparisons to random expression generator
arvind-murty Jul 21, 2023
60ec73c
exported vschemaWrapper to go/test/utils
arvind-murty Jul 21, 2023
a0acb5a
added query simplification to fuzzer
arvind-murty Jul 24, 2023
a28cd5f
simplified vschema and schema for simplifying fuzzer queries
arvind-murty Jul 24, 2023
ce019af
added simplification for case expressions
arvind-murty Jul 24, 2023
10c2034
moved VSchemaWrapper to its own package
arvind-murty Jul 24, 2023
a066e09
updated caseExpr simplification and reverted reverted simplifier changes
arvind-murty Jul 25, 2023
a2f7920
added sqlparser.NewCaseExpr
arvind-murty Jul 25, 2023
1d2ab6c
removed separate testing runs from simplifier in query fuzzer
arvind-murty Jul 26, 2023
756ad95
simplified existing results mismatched queries
arvind-murty Jul 26, 2023
359189e
minor fixes/improvements
arvind-murty Jul 27, 2023
a60a1c2
fixed goimports and failing endtoend tests
arvind-murty Jul 28, 2023
6736793
skip TestRandom
arvind-murty Jul 28, 2023
2c85a63
Merge remote-tracking branch 'upstream/main' into am-random-2
systay Aug 9, 2023
5c159f9
Merge remote-tracking branch 'upstream/main' into am-random-2
systay Aug 9, 2023
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
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'upstream/main' into am-random-2
Signed-off-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
systay committed Aug 9, 2023
commit 2c85a63c8b3ae4ee2ef09cf21b0da461c216676b
14 changes: 7 additions & 7 deletions go/test/endtoend/vtgate/queries/random/query_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"fmt"
"math/rand"

"golang.org/x/exp/slices"
"vitess.io/vitess/go/slice"

"vitess.io/vitess/go/slices2"
"golang.org/x/exp/slices"

"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/sqlparser"
Expand Down Expand Up @@ -298,7 +298,7 @@ func (sg *selectGenerator) randomSelect() {
// TODO: only_full_group_by related errors in Vitess
var exprGenerators []sqlparser.ExprGenerator
if canAggregate && testFailingQueries {
exprGenerators = slices2.Map(tables, func(t tableT) sqlparser.ExprGenerator { return &t })
exprGenerators = slice.Map(tables, func(t tableT) sqlparser.ExprGenerator { return &t })
// add scalar subqueries
if sg.r.Intn(10) < 1 {
exprGenerators = append(exprGenerators, sg)
Expand Down Expand Up @@ -470,7 +470,7 @@ func (sg *selectGenerator) aliasGroupingColumns(grouping []column) []column {

// returns the aggregation columns as three types: sqlparser.SelectExprs, []column
func (sg *selectGenerator) createAggregations(tables []tableT) (aggregates []column) {
exprGenerators := slices2.Map(tables, func(t tableT) sqlparser.ExprGenerator { return &t })
exprGenerators := slice.Map(tables, func(t tableT) sqlparser.ExprGenerator { return &t })
// add scalar subqueries
// TODO: subqueries fail
if sg.r.Intn(10) < 1 && testFailingQueries {
Expand Down Expand Up @@ -511,7 +511,7 @@ func (sg *selectGenerator) createOrderBy() {

// returns 0-2 random expressions based on tables
func (sg *selectGenerator) createWherePredicates(tables []tableT) {
exprGenerators := slices2.Map(tables, func(t tableT) sqlparser.ExprGenerator { return &t })
exprGenerators := slice.Map(tables, func(t tableT) sqlparser.ExprGenerator { return &t })
// add scalar subqueries
// TODO: subqueries fail
if sg.r.Intn(10) < 1 && testFailingQueries {
Expand All @@ -524,7 +524,7 @@ func (sg *selectGenerator) createWherePredicates(tables []tableT) {

// creates predicates for the having clause comparing a column to a random expression
func (sg *selectGenerator) createHavingPredicates(grouping []column) {
exprGenerators := slices2.Map(grouping, func(c column) sqlparser.ExprGenerator { return &c })
exprGenerators := slice.Map(grouping, func(c column) sqlparser.ExprGenerator { return &c })
// add scalar subqueries
// TODO: subqueries fail
if sg.r.Intn(10) < 1 && testFailingQueries {
Expand Down Expand Up @@ -613,7 +613,7 @@ func (sg *selectGenerator) matchNumCols(tables []tableT, newTable tableT, canAgg
if sg.genConfig.NumCols > len(sg.sel.SelectExprs) {
diff := sg.genConfig.NumCols - len(sg.sel.SelectExprs)
exprs := sg.createRandomExprs(diff, diff,
slices2.Map(tables, func(t tableT) sqlparser.ExprGenerator { return &t })...)
slice.Map(tables, func(t tableT) sqlparser.ExprGenerator { return &t })...)

for i, expr := range exprs {
col := sg.randomlyAlias(expr, fmt.Sprintf("crandom%d", i+1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestRandomExprWithTables(t *testing.T) {
seed := time.Now().UnixNano()
r := rand.New(rand.NewSource(seed))
genConfig := sqlparser.NewExprGeneratorConfig(sqlparser.CanAggregate, "", 0, false)
g := sqlparser.NewGenerator(r, 3, slices2.Map(schemaTables, func(t tableT) sqlparser.ExprGenerator { return &t })...)
g := sqlparser.NewGenerator(r, 3, slice.Map(schemaTables, func(t tableT) sqlparser.ExprGenerator { return &t })...)
g.Expression(genConfig)
}
}
2 changes: 1 addition & 1 deletion go/test/endtoend/vtgate/queries/random/random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func TestRandom(t *testing.T) {
break
}
// EOF
if sqlError, ok := vtErr.(*mysql.SQLError); ok && strings.Contains(sqlError.Message, "EOF") {
if sqlError, ok := vtErr.(*sqlerror.SQLError); ok && strings.Contains(sqlError.Message, "EOF") {
break
}
}
Expand Down
8 changes: 6 additions & 2 deletions go/test/vschemawrapper/vschema_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ func (vw *VSchemaWrapper) ConnCollation() collations.ID {
func (vw *VSchemaWrapper) PlannerWarning(_ string) {
}

func (vw *VSchemaWrapper) ForeignKeyMode() string {
return "allow"
func (vw *VSchemaWrapper) ForeignKeyMode(keyspace string) (vschemapb.Keyspace_ForeignKeyMode, error) {
defaultFkMode := vschemapb.Keyspace_FK_UNMANAGED
if vw.V.Keyspaces[keyspace] != nil && vw.V.Keyspaces[keyspace].ForeignKeyMode != vschemapb.Keyspace_FK_DEFAULT {
return vw.V.Keyspaces[keyspace].ForeignKeyMode, nil
}
return defaultFkMode, nil
}

func (vw *VSchemaWrapper) AllKeyspace() ([]*vindexes.Keyspace, error) {
Expand Down
25 changes: 20 additions & 5 deletions go/vt/vtgate/planbuilder/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ func TestPlan(t *testing.T) {

// TestForeignKeyPlanning tests the planning of foreign keys in a managed mode by Vitess.
func TestForeignKeyPlanning(t *testing.T) {
vschemaWrapper := &vschemaWrapper{
v: loadSchema(t, "vschemas/schema.json", true),
vschemaWrapper := &vschemawrapper.VSchemaWrapper{
V: loadSchema(t, "vschemas/schema.json", true),
// Set the keyspace with foreign keys enabled as the default.
keyspace: &vindexes.Keyspace{
Keyspace: &vindexes.Keyspace{
Name: "user_fk_allow",
Sharded: true,
},
Expand All @@ -116,7 +116,9 @@ func TestSystemTables57(t *testing.T) {
// first we move everything to use 5.7 logic
oldVer := servenv.MySQLServerVersion()
servenv.SetMySQLServerVersionForTest("5.7")
defer servenv.SetMySQLServerVersionForTest("")
defer func() {
servenv.SetMySQLServerVersionForTest(oldVer)
}()
vschemaWrapper := &vschemawrapper.VSchemaWrapper{V: loadSchema(t, "vschemas/schema.json", true)}
testOutputTempDir := makeTestOutput(t)
testFile(t, "info_schema57_cases.json", testOutputTempDir, vschemaWrapper, false)
Expand Down Expand Up @@ -208,7 +210,9 @@ func TestOneWith57Version(t *testing.T) {
// first we move everything to use 5.7 logic
oldVer := servenv.MySQLServerVersion()
servenv.SetMySQLServerVersionForTest("5.7")
defer servenv.SetMySQLServerVersionForTest("")
defer func() {
servenv.SetMySQLServerVersionForTest(oldVer)
}()
vschema := &vschemawrapper.VSchemaWrapper{V: loadSchema(t, "vschemas/schema.json", true)}

testFile(t, "onecase.json", "", vschema, false)
Expand Down Expand Up @@ -446,6 +450,17 @@ func loadSchema(t testing.TB, filename string, setCollation bool) *vindexes.VSch
return vschema
}

// createFkDefinition is a helper function to create a Foreign key definition struct from the columns used in it provided as list of strings.
func createFkDefinition(childCols []string, parentTableName string, parentCols []string) *sqlparser.ForeignKeyDefinition {
return &sqlparser.ForeignKeyDefinition{
Source: sqlparser.MakeColumns(childCols...),
ReferenceDefinition: &sqlparser.ReferenceDefinition{
ReferencedTable: sqlparser.NewTableName(parentTableName),
ReferencedColumns: sqlparser.MakeColumns(parentCols...),
},
}
}

type (
planTest struct {
Comment string `json:"comment,omitempty"`
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.