Skip to content

Commit

Permalink
record plan warnings, added e2e test and unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <[email protected]>
  • Loading branch information
harshit-gangal committed Oct 15, 2021
1 parent 91917fa commit db61f7d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
22 changes: 22 additions & 0 deletions go/test/endtoend/vtgate/gen4/gen4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,28 @@ func TestSubQueries(t *testing.T) {
assertMatches(t, conn, `select (select id from t2 order by id limit 1) from t2 order by id limit 2`, `[[INT64(1)] [INT64(1)]]`)
}

func TestPlannerWarning(t *testing.T) {
ctx := context.Background()
conn, err := mysql.Connect(ctx, &vtParams)
require.NoError(t, err)
defer conn.Close()

// straight_join query
_ = checkedExec(t, conn, `select 1 from t1 straight_join t2 on t1.id = t2.id`)
assertMatches(t, conn, `show warnings`, `[[VARCHAR("Warning") UINT16(1235) VARCHAR("straight join is converted to normal join")]]`)

// execute same query again.
_ = checkedExec(t, conn, `select 1 from t1 straight_join t2 on t1.id = t2.id`)
assertMatches(t, conn, `show warnings`, `[[VARCHAR("Warning") UINT16(1235) VARCHAR("straight join is converted to normal join")]]`)

// random query to reset the warning.
_ = checkedExec(t, conn, `select 1 from t1`)

// execute same query again.
_ = checkedExec(t, conn, `select 1 from t1 straight_join t2 on t1.id = t2.id`)
assertMatches(t, conn, `show warnings`, `[[VARCHAR("Warning") UINT16(1235) VARCHAR("straight join is converted to normal join")]]`)
}

func assertMatches(t *testing.T, conn *mysql.Conn, query, expected string) {
t.Helper()
qr := checkedExec(t, conn, query)
Expand Down
33 changes: 33 additions & 0 deletions go/vt/vtgate/executor_select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2675,3 +2675,36 @@ func TestSelectScatterFails(t *testing.T) {
_, err = executorExecSession(executor, "select /*vt+ ALLOW_SCATTER */ id from user", nil, sess)
require.NoError(t, err)
}

func TestGen4SelectStraightJoin(t *testing.T) {
executor, sbc1, _, _ := createExecutorEnv()
executor.normalize = true
*plannerVersion = "gen4"
defer func() {
// change it back to v3
*plannerVersion = "v3"
}()

session := NewSafeSession(&vtgatepb.Session{TargetString: "TestExecutor"})
query := "select u.id from user u straight_join user2 u2 on u.id = u2.id"
_, err := executor.Execute(context.Background(),
"TestGen4SelectStraightJoin",
session,
query, map[string]*querypb.BindVariable{},
)
require.NoError(t, err)
wantQueries := []*querypb.BoundQuery{
{
Sql: "select u.id from `user` as u, user2 as u2 where u.id = u2.id",
BindVariables: map[string]*querypb.BindVariable{},
},
}
wantWarnings := []*querypb.QueryWarning{
{
Code: 1235,
Message: "straight join is converted to normal join",
},
}
utils.MustMatch(t, wantQueries, sbc1.Queries)
utils.MustMatch(t, wantWarnings, session.Warnings)
}
9 changes: 5 additions & 4 deletions go/vt/vtgate/planbuilder/gen4_planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ func gen4planSQLCalcFoundRows(vschema ContextVSchema, sel *sqlparser.Select, que
if err != nil {
return nil, err
}
if semTable.Warning != "" {
// log it as planning warning.
vschema.PlannerWarning(semTable.Warning)
}
// record any warning as planner warning.
vschema.PlannerWarning(semTable.Warning)

plan, err := buildSQLCalcFoundRowsPlan(query, sel, reservedVars, vschema, planSelectGen4)
if err != nil {
return nil, err
Expand Down Expand Up @@ -124,6 +123,8 @@ func newBuildSelectPlan(selStmt sqlparser.SelectStatement, reservedVars *sqlpars
if err != nil {
return nil, err
}
// record any warning as planner warning.
vschema.PlannerWarning(semTable.Warning)

err = queryRewrite(semTable, reservedVars, selStmt)
if err != nil {
Expand Down

0 comments on commit db61f7d

Please sign in to comment.