Skip to content

Commit

Permalink
Merge pull request #7776 from planetscale/bp-7730
Browse files Browse the repository at this point in the history
[9.0] make sure to handle subqueries on top of subqueries
  • Loading branch information
shlomi-noach authored Apr 1, 2021
2 parents 064d399 + 61e13d2 commit e92e363
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
14 changes: 14 additions & 0 deletions go/test/endtoend/vtgate/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,20 @@ func TestCreateView(t *testing.T) {
assertMatches(t, conn, "select * from v1", `[[INT64(1) INT64(1)] [INT64(2) INT64(2)] [INT64(3) INT64(3)] [INT64(4) INT64(4)] [INT64(5) INT64(5)]]`)
}

func TestSubQueryOnTopOfSubQuery(t *testing.T) {
defer cluster.PanicHandler(t)
ctx := context.Background()
conn, err := mysql.Connect(ctx, &vtParams)
require.NoError(t, err)
defer conn.Close()
defer exec(t, conn, `delete from t1`)

exec(t, conn, `insert into t1(id1, id2) values (1, 1), (2, 2), (3, 3), (4, 4), (5, 5)`)
exec(t, conn, `insert into t2(id3, id4) values (1, 3), (2, 4)`)

assertMatches(t, conn, "select id1 from t1 where id1 not in (select id3 from t2) and id2 in (select id4 from t2)", `[[INT64(3)] [INT64(4)]]`)
}

func assertMatches(t *testing.T, conn *mysql.Conn, query, expected string) {
t.Helper()
qr := exec(t, conn, query)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,9 @@ select id, 'abc' as test from user where id = 1 union all select id, 'def' as te
1 ks_sharded/-40: select id, 'abc' as test from user where id = 1 union all select id, 'def' as test from user where id = 1 union all select id, 'ghi' as test from user where id = 1 limit 10001 /* union all */

----------------------------------------------------------------------
select id from user where not id in (select col from music where music.user_id = 42) and id in (select col from music where music.user_id = 411)

1 ks_sharded/40-80: select col from music where music.user_id = 42 limit 10001
2 ks_sharded/40-80: select col from music where music.user_id = 411 limit 10001

----------------------------------------------------------------------
2 changes: 2 additions & 0 deletions go/vt/vtexplain/testdata/selectsharded-queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ select id, case when name = 'alice' then 'ALICE' when name = 'bob' then 'BOB' el
select id, case when substr(name, 1, 5) = 'alice' then 'ALICE' when name = 'bob' then 'BOB' else 'OTHER' end as name from user where id = 1 /* select case */;

select id, 'abc' as test from user where id = 1 union all select id, 'def' as test from user where id = 1 union all select id, 'ghi' as test from user where id = 1 /* union all */;

select id from user where not id in (select col from music where music.user_id = 42) and id in (select col from music where music.user_id = 411);
7 changes: 6 additions & 1 deletion go/vt/vtgate/planbuilder/filtering.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ func planFilter(pb *primitiveBuilder, input logicalPlan, filter sqlparser.Expr,
node.UpdatePlan(pb, filter)
return node, nil
case *pulloutSubquery:
return planFilter(pb, node.underlying, filter, whereType, origin)
plan, err := planFilter(pb, node.underlying, filter, whereType, origin)
if err != nil {
return nil, err
}
node.underlying = plan
return node, nil
case *vindexFunc:
return filterVindexFunc(node, filter)
case *subquery:
Expand Down
47 changes: 34 additions & 13 deletions go/vt/vtgate/planbuilder/testdata/filter_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1799,19 +1799,40 @@
"Vindex": "user_index"
},
{
"OperatorType": "Route",
"Variant": "SelectIN",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id from user where 1 != 1",
"Query": "select id from user where :__sq_has_values1 = 1 and id in ::__vals and not (:__sq_has_values2 = 1 and id in ::__sq2)",
"Table": "user",
"Values": [
"::__sq1"
],
"Vindex": "user_index"
"OperatorType": "Subquery",
"Variant": "PulloutIn",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "SelectEqualUnique",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select user_extra.col from user_extra where 1 != 1",
"Query": "select user_extra.col from user_extra where user_extra.user_id = 411",
"Table": "user_extra",
"Values": [
411
],
"Vindex": "user_index"
},
{
"OperatorType": "Route",
"Variant": "SelectIN",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id from user where 1 != 1",
"Query": "select id from user where :__sq_has_values1 = 1 and id in ::__vals and not (:__sq_has_values2 = 1 and id in ::__sq2)",
"Table": "user",
"Values": [
"::__sq1"
],
"Vindex": "user_index"
}
]
}
]
}
Expand Down

0 comments on commit e92e363

Please sign in to comment.