Skip to content

Commit db1d3ce

Browse files
Pass through predicates when trying to merge apply join.
Signed-off-by: Arthur Schreiber <[email protected]>
1 parent 123e309 commit db1d3ce

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

go/vt/vtgate/planbuilder/operators/query_planning.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io"
2222
"strconv"
2323

24+
"vitess.io/vitess/go/slice"
2425
"vitess.io/vitess/go/vt/vtgate/planbuilder/operators/predicates"
2526

2627
"vitess.io/vitess/go/vt/sqlparser"
@@ -123,7 +124,11 @@ func runRewriters(ctx *plancontext.PlanningContext, root Operator) Operator {
123124
}
124125

125126
func tryMergeApplyJoin(in *ApplyJoin, ctx *plancontext.PlanningContext) (_ Operator, res *ApplyResult) {
126-
jm := newJoinMerge(nil, in.JoinType)
127+
preds := slice.Map(in.JoinPredicates.columns, func(col applyJoinColumn) sqlparser.Expr {
128+
return col.Original
129+
})
130+
131+
jm := newJoinMerge(preds, in.JoinType)
127132
r := jm.mergeJoinInputs(ctx, in.LHS, in.RHS)
128133
if r == nil {
129134
return in, NoRewrite

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

+27
Original file line numberDiff line numberDiff line change
@@ -5450,6 +5450,33 @@
54505450
]
54515451
}
54525452
},
5453+
{
5454+
"comment": "Joining with a subquery that uses an aggregate column and an `EqualUnique` route with additional subquery can be merged together",
5455+
"query": "SELECT music.id FROM music INNER JOIN (SELECT MAX(id) as maxt FROM music WHERE music.user_id = 5 AND music.foobar NOT IN (SELECT foobar FROM music_extra WHERE music_extra.user_id = 5)) other ON other.maxt = music.id",
5456+
"plan": {
5457+
"Type": "Passthrough",
5458+
"QueryType": "SELECT",
5459+
"Original": "SELECT music.id FROM music INNER JOIN (SELECT MAX(id) as maxt FROM music WHERE music.user_id = 5 AND music.foobar NOT IN (SELECT foobar FROM music_extra WHERE music_extra.user_id = 5)) other ON other.maxt = music.id",
5460+
"Instructions": {
5461+
"OperatorType": "Route",
5462+
"Variant": "EqualUnique",
5463+
"Keyspace": {
5464+
"Name": "user",
5465+
"Sharded": true
5466+
},
5467+
"FieldQuery": "select music.id from (select max(id) as maxt from music where 1 != 1) as other, music where 1 != 1",
5468+
"Query": "select music.id from (select max(id) as maxt from music where music.user_id = 5 and music.foobar not in (select foobar from music_extra where music_extra.user_id = 5)) as other, music where other.maxt = music.id",
5469+
"Table": "music",
5470+
"Values": [
5471+
"5"
5472+
],
5473+
"Vindex": "user_index"
5474+
},
5475+
"TablesUsed": [
5476+
"user.music"
5477+
]
5478+
}
5479+
},
54535480
{
54545481
"comment": "Joining with a subquery that uses an `EqualUnique` route can be merged",
54555482
"query": "SELECT music.id FROM music INNER JOIN (SELECT id FROM music WHERE music.user_id = 5) other ON other.id = music.id",

0 commit comments

Comments
 (0)