Skip to content

Commit

Permalink
planner fix: only expose columns being returned from the RHS
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
systay committed Feb 6, 2024
1 parent 5d30fd0 commit 4c8e0e3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
17 changes: 13 additions & 4 deletions go/vt/vtgate/planbuilder/operators/projection_pushing.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,18 @@ func exposeColumnsThroughDerivedTable(ctx *plancontext.PlanningContext, p *Proje

lhsIDs := TableID(src.LHS)
rhsIDs := TableID(src.RHS)
rewriteColumnsForJoin(ctx, src.JoinPredicates.columns, lhsIDs, rhsIDs, lhs, rhs)
rewriteColumnsForJoin(ctx, src.JoinColumns.columns, lhsIDs, rhsIDs, lhs, rhs)
rewriteColumnsForJoin(ctx, src.JoinPredicates.columns, lhsIDs, rhsIDs, lhs, rhs, false)
rewriteColumnsForJoin(ctx, src.JoinColumns.columns, lhsIDs, rhsIDs, lhs, rhs, true)
}

func rewriteColumnsForJoin(ctx *plancontext.PlanningContext, columns []applyJoinColumn, lhsIDs, rhsIDs semantics.TableSet, lhs, rhs *projector) {
func rewriteColumnsForJoin(
ctx *plancontext.PlanningContext,
columns []applyJoinColumn,
lhsIDs, rhsIDs semantics.TableSet,
lhs, rhs *projector,
exposeRHS bool, // we only want to expose the returned columns from the RHS.
// For predicates, we don't need to expose the RHS columns
) {
for colIdx, predicate := range columns {
for lhsIdx, bve := range predicate.LHSExprs {
// since this is on the LHSExprs, we know that dependencies are from that side of the join
Expand All @@ -353,7 +360,9 @@ func rewriteColumnsForJoin(ctx *plancontext.PlanningContext, columns []applyJoin
rewriteTo = lhs.get(ctx, expr)
return false
case deps.IsSolvedBy(rhsIDs):
rewriteTo = rhs.get(ctx, expr)
if exposeRHS {
rewriteTo = rhs.get(ctx, expr)
}
return false
default:
return true
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/testdata/cte_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -1133,8 +1133,8 @@
"Name": "user",
"Sharded": true
},
"FieldQuery": "select 1 from (select user_extra.col as `user_extra.col` from user_extra where 1 != 1) as t where 1 != 1",
"Query": "select 1 from (select user_extra.col as `user_extra.col` from user_extra where user_extra.col = :user_col) as t",
"FieldQuery": "select 1 from user_extra where 1 != 1",
"Query": "select 1 from user_extra where user_extra.col = :user_col",
"Table": "user_extra"
}
]
Expand Down
10 changes: 5 additions & 5 deletions go/vt/vtgate/planbuilder/testdata/from_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -1901,8 +1901,8 @@
"Name": "user",
"Sharded": true
},
"FieldQuery": "select 1 from (select user_extra.col as `user_extra.col` from user_extra where 1 != 1) as t where 1 != 1",
"Query": "select 1 from (select user_extra.col as `user_extra.col` from user_extra where user_extra.col = :user_col) as t",
"FieldQuery": "select 1 from user_extra where 1 != 1",
"Query": "select 1 from user_extra where user_extra.col = :user_col",
"Table": "user_extra"
}
]
Expand Down Expand Up @@ -4296,7 +4296,7 @@
{
"OperatorType": "Join",
"Variant": "Join",
"JoinColumnIndexes": "R:0,L:1,R:2",
"JoinColumnIndexes": "R:0,L:1,R:1",
"JoinVars": {
"m_id": 0
},
Expand All @@ -4320,8 +4320,8 @@
"Name": "user",
"Sharded": true
},
"FieldQuery": "select subquery_for_count.user_id, subquery_for_count.`u.id`, weight_string(subquery_for_count.user_id) from (select u.user_id, u.id as `u.id` from `user` as u, user_extra as ue where 1 != 1) as subquery_for_count where 1 != 1",
"Query": "select distinct subquery_for_count.user_id, subquery_for_count.`u.id`, weight_string(subquery_for_count.user_id) from (select u.user_id, u.id as `u.id` from `user` as u, user_extra as ue where u.id = :m_id and u.id = ue.user_id) as subquery_for_count",
"FieldQuery": "select subquery_for_count.user_id, weight_string(subquery_for_count.user_id) from (select u.user_id from `user` as u, user_extra as ue where 1 != 1) as subquery_for_count where 1 != 1",
"Query": "select distinct subquery_for_count.user_id, weight_string(subquery_for_count.user_id) from (select u.user_id from `user` as u, user_extra as ue where u.id = :m_id and u.id = ue.user_id) as subquery_for_count",
"Table": "`user`, user_extra",
"Values": [
":m_id"
Expand Down
20 changes: 10 additions & 10 deletions go/vt/vtgate/planbuilder/testdata/tpch_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,8 @@
"Name": "main",
"Sharded": true
},
"FieldQuery": "select count(*), shipping.o_custkey from (select o_custkey as o_custkey, o_orderkey as o_orderkey from orders where 1 != 1) as shipping where 1 != 1 group by shipping.o_custkey",
"Query": "select count(*), shipping.o_custkey from (select o_custkey as o_custkey, o_orderkey as o_orderkey from orders where o_orderkey = :l_orderkey) as shipping group by shipping.o_custkey",
"FieldQuery": "select count(*), shipping.o_custkey from (select o_custkey as o_custkey from orders where 1 != 1) as shipping where 1 != 1 group by shipping.o_custkey",
"Query": "select count(*), shipping.o_custkey from (select o_custkey as o_custkey from orders where o_orderkey = :l_orderkey) as shipping group by shipping.o_custkey",
"Table": "orders",
"Values": [
":l_orderkey"
Expand Down Expand Up @@ -679,8 +679,8 @@
"Name": "main",
"Sharded": true
},
"FieldQuery": "select count(*), shipping.s_nationkey from (select s_suppkey as s_suppkey, s_nationkey as s_nationkey from supplier where 1 != 1) as shipping where 1 != 1 group by shipping.s_nationkey",
"Query": "select count(*), shipping.s_nationkey from (select s_suppkey as s_suppkey, s_nationkey as s_nationkey from supplier where s_suppkey = :l_suppkey) as shipping group by shipping.s_nationkey",
"FieldQuery": "select count(*), shipping.s_nationkey from (select s_nationkey as s_nationkey from supplier where 1 != 1) as shipping where 1 != 1 group by shipping.s_nationkey",
"Query": "select count(*), shipping.s_nationkey from (select s_nationkey as s_nationkey from supplier where s_suppkey = :l_suppkey) as shipping group by shipping.s_nationkey",
"Table": "supplier",
"Values": [
":l_suppkey"
Expand All @@ -694,8 +694,8 @@
"Name": "main",
"Sharded": true
},
"FieldQuery": "select count(*), supp_nation, weight_string(supp_nation), n1.n_nationkey as `n1.n_nationkey` from (select n1.n_name as supp_nation, n1.n_name = 'FRANCE' as `n1.n_name = 'FRANCE'`, n1.n_name = 'GERMANY' as `n1.n_name = 'GERMANY'`, n1.n_nationkey as `n1.n_nationkey` from nation as n1 where 1 != 1) as shipping where 1 != 1 group by supp_nation, weight_string(supp_nation)",
"Query": "select count(*), supp_nation, weight_string(supp_nation), n1.n_nationkey as `n1.n_nationkey` from (select n1.n_name as supp_nation, n1.n_name = 'FRANCE' as `n1.n_name = 'FRANCE'`, n1.n_name = 'GERMANY' as `n1.n_name = 'GERMANY'`, n1.n_nationkey as `n1.n_nationkey` from nation as n1 where n1.n_nationkey = :s_nationkey) as shipping group by supp_nation, weight_string(supp_nation)",
"FieldQuery": "select count(*), supp_nation, weight_string(supp_nation) from (select n1.n_name as supp_nation, n1.n_name = 'FRANCE' as `n1.n_name = 'FRANCE'`, n1.n_name = 'GERMANY' as `n1.n_name = 'GERMANY'` from nation as n1 where 1 != 1) as shipping where 1 != 1 group by supp_nation, weight_string(supp_nation)",
"Query": "select count(*), supp_nation, weight_string(supp_nation) from (select n1.n_name as supp_nation, n1.n_name = 'FRANCE' as `n1.n_name = 'FRANCE'`, n1.n_name = 'GERMANY' as `n1.n_name = 'GERMANY'` from nation as n1 where n1.n_nationkey = :s_nationkey) as shipping group by supp_nation, weight_string(supp_nation)",
"Table": "nation",
"Values": [
":s_nationkey"
Expand Down Expand Up @@ -734,8 +734,8 @@
"Name": "main",
"Sharded": true
},
"FieldQuery": "select count(*), shipping.c_nationkey from (select c_custkey as c_custkey, c_nationkey as c_nationkey from customer where 1 != 1) as shipping where 1 != 1 group by shipping.c_nationkey",
"Query": "select count(*), shipping.c_nationkey from (select c_custkey as c_custkey, c_nationkey as c_nationkey from customer where c_custkey = :o_custkey) as shipping group by shipping.c_nationkey",
"FieldQuery": "select count(*), shipping.c_nationkey from (select c_nationkey as c_nationkey from customer where 1 != 1) as shipping where 1 != 1 group by shipping.c_nationkey",
"Query": "select count(*), shipping.c_nationkey from (select c_nationkey as c_nationkey from customer where c_custkey = :o_custkey) as shipping group by shipping.c_nationkey",
"Table": "customer",
"Values": [
":o_custkey"
Expand All @@ -749,8 +749,8 @@
"Name": "main",
"Sharded": true
},
"FieldQuery": "select count(*), cust_nation, weight_string(cust_nation), n2.n_nationkey as `n2.n_nationkey` from (select n2.n_name as cust_nation, n2.n_name = 'GERMANY' as `n2.n_name = 'GERMANY'`, n2.n_name = 'FRANCE' as `n2.n_name = 'FRANCE'`, n2.n_nationkey as `n2.n_nationkey` from nation as n2 where 1 != 1) as shipping where 1 != 1 group by cust_nation, weight_string(cust_nation)",
"Query": "select count(*), cust_nation, weight_string(cust_nation), n2.n_nationkey as `n2.n_nationkey` from (select n2.n_name as cust_nation, n2.n_name = 'GERMANY' as `n2.n_name = 'GERMANY'`, n2.n_name = 'FRANCE' as `n2.n_name = 'FRANCE'`, n2.n_nationkey as `n2.n_nationkey` from nation as n2 where (:n1_n_name = 'FRANCE' and n2.n_name = 'GERMANY' or :n1_n_name = 'GERMANY' and n2.n_name = 'FRANCE') and n2.n_nationkey = :c_nationkey) as shipping group by cust_nation, weight_string(cust_nation)",
"FieldQuery": "select count(*), cust_nation, weight_string(cust_nation) from (select n2.n_name as cust_nation from nation as n2 where 1 != 1) as shipping where 1 != 1 group by cust_nation, weight_string(cust_nation)",
"Query": "select count(*), cust_nation, weight_string(cust_nation) from (select n2.n_name as cust_nation from nation as n2 where (:n1_n_name = 'FRANCE' and n2.n_name = 'GERMANY' or :n1_n_name = 'GERMANY' and n2.n_name = 'FRANCE') and n2.n_nationkey = :c_nationkey) as shipping group by cust_nation, weight_string(cust_nation)",
"Table": "nation",
"Values": [
":c_nationkey"
Expand Down

0 comments on commit 4c8e0e3

Please sign in to comment.