diff --git a/go/vt/vtgate/planbuilder/operators/route_planning.go b/go/vt/vtgate/planbuilder/operators/route_planning.go index c3339a144c4..97c8e0eeba7 100644 --- a/go/vt/vtgate/planbuilder/operators/route_planning.go +++ b/go/vt/vtgate/planbuilder/operators/route_planning.go @@ -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 } diff --git a/go/vt/vtgate/planbuilder/testdata/reference_cases.json b/go/vt/vtgate/planbuilder/testdata/reference_cases.json index 5388512f127..37cbc9c1504 100644 --- a/go/vt/vtgate/planbuilder/testdata/reference_cases.json +++ b/go/vt/vtgate/planbuilder/testdata/reference_cases.json @@ -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" + ] + } } -] +] \ No newline at end of file