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

Fix for query and sub query with limits #8097

Merged
merged 2 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions go/test/endtoend/vtgate/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,16 @@ func TestShowGtid(t *testing.T) {
}
}

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

exec(t, conn, "insert into t1(id1, id2) values(0,0),(1,1),(2,2),(3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8),(9,9)")
result := exec(t, conn, `select id1, id2 from t1 where id1 >= ( select id1 from t1 order by id1 asc limit 1) limit 100`)
assert.Equal(t, 10, len(result.Rows))
}

func assertMatches(t *testing.T, conn *mysql.Conn, query, expected string) {
t.Helper()
qr := exec(t, conn, query)
Expand Down
6 changes: 5 additions & 1 deletion go/vt/vtgate/engine/pullout_subquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ var (
)

func (ps *PulloutSubquery) execSubquery(vcursor VCursor, bindVars map[string]*querypb.BindVariable) (map[string]*querypb.BindVariable, error) {
result, err := ps.Subquery.Execute(vcursor, bindVars, false)
subqueryBindVars := make(map[string]*querypb.BindVariable, len(bindVars))
for k, v := range bindVars {
subqueryBindVars[k] = v
}
result, err := ps.Subquery.Execute(vcursor, subqueryBindVars, false)
if err != nil {
return nil, err
}
Expand Down
42 changes: 42 additions & 0 deletions go/vt/vtgate/executor_select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2127,6 +2127,48 @@ func TestCrossShardSubquery(t *testing.T) {
}
}

func TestSubQueryAndQueryWithLimit(t *testing.T) {
executor, sbc1, sbc2, _ := createExecutorEnv()
result1 := []*sqltypes.Result{{
Fields: []*querypb.Field{
{Name: "id", Type: sqltypes.Int32},
{Name: "col", Type: sqltypes.Int32},
},
InsertID: 0,
Rows: [][]sqltypes.Value{{
sqltypes.NewInt32(1),
sqltypes.NewInt32(3),
}},
}}
result2 := []*sqltypes.Result{{
Fields: []*querypb.Field{
{Name: "id", Type: sqltypes.Int32},
{Name: "col", Type: sqltypes.Int32},
},
InsertID: 0,
Rows: [][]sqltypes.Value{{
sqltypes.NewInt32(111),
sqltypes.NewInt32(333),
}},
}}
sbc1.SetResults(result1)
sbc2.SetResults(result2)

exec(executor, NewSafeSession(&vtgatepb.Session{
TargetString: "@master",
}), "select id1, id2 from t1 where id1 >= ( select id1 from t1 order by id1 asc limit 1) limit 100")
require.Equal(t, 2, len(sbc1.Queries))
require.Equal(t, 2, len(sbc2.Queries))

// sub query is evaluated first, and sees a limit of 1
assert.Equal(t, `type:INT64 value:"1" `, sbc1.Queries[0].BindVariables["__upper_limit"].String())
assert.Equal(t, `type:INT64 value:"1" `, sbc2.Queries[0].BindVariables["__upper_limit"].String())

// outer limit is only applied to the outer query
assert.Equal(t, `type:INT64 value:"100" `, sbc1.Queries[1].BindVariables["__upper_limit"].String())
assert.Equal(t, `type:INT64 value:"100" `, sbc2.Queries[1].BindVariables["__upper_limit"].String())
}

func TestCrossShardSubqueryStream(t *testing.T) {
executor, sbc1, sbc2, _ := createLegacyExecutorEnv()
result1 := []*sqltypes.Result{{
Expand Down