Skip to content

Commit 4c8e0e3

Browse files
committed
planner fix: only expose columns being returned from the RHS
Signed-off-by: Andres Taylor <andres@planetscale.com>
1 parent 5d30fd0 commit 4c8e0e3

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

go/vt/vtgate/planbuilder/operators/projection_pushing.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,18 @@ func exposeColumnsThroughDerivedTable(ctx *plancontext.PlanningContext, p *Proje
324324

325325
lhsIDs := TableID(src.LHS)
326326
rhsIDs := TableID(src.RHS)
327-
rewriteColumnsForJoin(ctx, src.JoinPredicates.columns, lhsIDs, rhsIDs, lhs, rhs)
328-
rewriteColumnsForJoin(ctx, src.JoinColumns.columns, lhsIDs, rhsIDs, lhs, rhs)
327+
rewriteColumnsForJoin(ctx, src.JoinPredicates.columns, lhsIDs, rhsIDs, lhs, rhs, false)
328+
rewriteColumnsForJoin(ctx, src.JoinColumns.columns, lhsIDs, rhsIDs, lhs, rhs, true)
329329
}
330330

331-
func rewriteColumnsForJoin(ctx *plancontext.PlanningContext, columns []applyJoinColumn, lhsIDs, rhsIDs semantics.TableSet, lhs, rhs *projector) {
331+
func rewriteColumnsForJoin(
332+
ctx *plancontext.PlanningContext,
333+
columns []applyJoinColumn,
334+
lhsIDs, rhsIDs semantics.TableSet,
335+
lhs, rhs *projector,
336+
exposeRHS bool, // we only want to expose the returned columns from the RHS.
337+
// For predicates, we don't need to expose the RHS columns
338+
) {
332339
for colIdx, predicate := range columns {
333340
for lhsIdx, bve := range predicate.LHSExprs {
334341
// since this is on the LHSExprs, we know that dependencies are from that side of the join
@@ -353,7 +360,9 @@ func rewriteColumnsForJoin(ctx *plancontext.PlanningContext, columns []applyJoin
353360
rewriteTo = lhs.get(ctx, expr)
354361
return false
355362
case deps.IsSolvedBy(rhsIDs):
356-
rewriteTo = rhs.get(ctx, expr)
363+
if exposeRHS {
364+
rewriteTo = rhs.get(ctx, expr)
365+
}
357366
return false
358367
default:
359368
return true

go/vt/vtgate/planbuilder/testdata/cte_cases.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,8 +1133,8 @@
11331133
"Name": "user",
11341134
"Sharded": true
11351135
},
1136-
"FieldQuery": "select 1 from (select user_extra.col as `user_extra.col` from user_extra where 1 != 1) as t where 1 != 1",
1137-
"Query": "select 1 from (select user_extra.col as `user_extra.col` from user_extra where user_extra.col = :user_col) as t",
1136+
"FieldQuery": "select 1 from user_extra where 1 != 1",
1137+
"Query": "select 1 from user_extra where user_extra.col = :user_col",
11381138
"Table": "user_extra"
11391139
}
11401140
]

go/vt/vtgate/planbuilder/testdata/from_cases.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,8 +1901,8 @@
19011901
"Name": "user",
19021902
"Sharded": true
19031903
},
1904-
"FieldQuery": "select 1 from (select user_extra.col as `user_extra.col` from user_extra where 1 != 1) as t where 1 != 1",
1905-
"Query": "select 1 from (select user_extra.col as `user_extra.col` from user_extra where user_extra.col = :user_col) as t",
1904+
"FieldQuery": "select 1 from user_extra where 1 != 1",
1905+
"Query": "select 1 from user_extra where user_extra.col = :user_col",
19061906
"Table": "user_extra"
19071907
}
19081908
]
@@ -4296,7 +4296,7 @@
42964296
{
42974297
"OperatorType": "Join",
42984298
"Variant": "Join",
4299-
"JoinColumnIndexes": "R:0,L:1,R:2",
4299+
"JoinColumnIndexes": "R:0,L:1,R:1",
43004300
"JoinVars": {
43014301
"m_id": 0
43024302
},
@@ -4320,8 +4320,8 @@
43204320
"Name": "user",
43214321
"Sharded": true
43224322
},
4323-
"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",
4324-
"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",
4323+
"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",
4324+
"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",
43254325
"Table": "`user`, user_extra",
43264326
"Values": [
43274327
":m_id"

go/vt/vtgate/planbuilder/testdata/tpch_cases.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,8 @@
643643
"Name": "main",
644644
"Sharded": true
645645
},
646-
"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",
647-
"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",
646+
"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",
647+
"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",
648648
"Table": "orders",
649649
"Values": [
650650
":l_orderkey"
@@ -679,8 +679,8 @@
679679
"Name": "main",
680680
"Sharded": true
681681
},
682-
"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",
683-
"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",
682+
"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",
683+
"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",
684684
"Table": "supplier",
685685
"Values": [
686686
":l_suppkey"
@@ -694,8 +694,8 @@
694694
"Name": "main",
695695
"Sharded": true
696696
},
697-
"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)",
698-
"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)",
697+
"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)",
698+
"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)",
699699
"Table": "nation",
700700
"Values": [
701701
":s_nationkey"
@@ -734,8 +734,8 @@
734734
"Name": "main",
735735
"Sharded": true
736736
},
737-
"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",
738-
"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",
737+
"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",
738+
"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",
739739
"Table": "customer",
740740
"Values": [
741741
":o_custkey"
@@ -749,8 +749,8 @@
749749
"Name": "main",
750750
"Sharded": true
751751
},
752-
"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)",
753-
"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)",
752+
"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)",
753+
"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)",
754754
"Table": "nation",
755755
"Values": [
756756
":c_nationkey"

0 commit comments

Comments
 (0)