Skip to content
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/route_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func pushDerived(ctx *plancontext.PlanningContext, op *Horizon) (Operator, *Appl
return op, NoRewrite
}

if (innerRoute.Routing.OpCode() != engine.EqualUnique) && !op.IsMergeable(ctx) {
if !innerRoute.Routing.OpCode().IsSingleShard() && !op.IsMergeable(ctx) {
// no need to check anything if we are sure that we will only hit a single shard
return op, NoRewrite
}
Expand Down
24 changes: 23 additions & 1 deletion go/vt/vtgate/planbuilder/testdata/reference_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -893,5 +893,27 @@
"user.music"
]
}
},
{
"comment": "Derived table pushdown optimization",
"query": "SELECT * FROM (SELECT DISTINCT col FROM user.ref WHERE col = 1 LIMIT 10) as sub",
"plan": {
"Type": "Passthrough",
"QueryType": "SELECT",
"Original": "SELECT * FROM (SELECT DISTINCT col FROM user.ref WHERE col = 1 LIMIT 10) as sub",
"Instructions": {
"OperatorType": "Route",
"Variant": "Reference",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select col from (select col from ref where 1 != 1) as sub where 1 != 1",
"Query": "select col from (select distinct col from ref where col = 1 limit 10) as sub"
},
"TablesUsed": [
"user.ref"
]
}
}
]
]
Loading