diff --git a/go/vt/vtgate/planbuilder/operators/subquery_planning.go b/go/vt/vtgate/planbuilder/operators/subquery_planning.go index 11ff963503d..774427ca2d8 100644 --- a/go/vt/vtgate/planbuilder/operators/subquery_planning.go +++ b/go/vt/vtgate/planbuilder/operators/subquery_planning.go @@ -550,6 +550,9 @@ func tryMergeSubqueryWithOuter(ctx *plancontext.PlanningContext, subQuery *SubQu if !subQuery.IsArgument { op.Source = newFilter(outer.Source, subQuery.Original) } + if outer.Comments != nil { + op.Comments = outer.Comments + } ctx.MergedSubqueries = append(ctx.MergedSubqueries, subQuery.originalSubquery) return op, Rewrote("merged subquery with outer") } diff --git a/go/vt/vtgate/planbuilder/testdata/select_cases.json b/go/vt/vtgate/planbuilder/testdata/select_cases.json index 9091c5d8613..ef6df93441e 100644 --- a/go/vt/vtgate/planbuilder/testdata/select_cases.json +++ b/go/vt/vtgate/planbuilder/testdata/select_cases.json @@ -933,6 +933,73 @@ ] } }, + { + "comment": "Comments with subquery", + "query": "select /* comment */ user.col from user where id IN (select id from user where id > 1 and id < 10)", + "plan": { + "QueryType": "SELECT", + "Original": "select /* comment */ user.col from user where id IN (select id from user where id > 1 and id < 10)", + "Instructions": { + "OperatorType": "Route", + "Variant": "Scatter", + "Keyspace": { + "Name": "user", + "Sharded": true + }, + "FieldQuery": "select `user`.col from `user` where 1 != 1", + "Query": "select /* comment */ `user`.col from `user` where id in (select id from `user` where id > 1 and id < 10)", + "Table": "`user`" + }, + "TablesUsed": [ + "user.user" + ] + } + }, + { + "comment": "Comments with subquery not merged into a single route", + "query": "select /* comment */ user.col from user where foo IN (select id from user where id > 1 and id < 10)", + "plan": { + "QueryType": "SELECT", + "Original": "select /* comment */ user.col from user where foo IN (select id from user where id > 1 and id < 10)", + "Instructions": { + "OperatorType": "UncorrelatedSubquery", + "Variant": "PulloutIn", + "PulloutVars": [ + "__sq_has_values", + "__sq1" + ], + "Inputs": [ + { + "InputName": "SubQuery", + "OperatorType": "Route", + "Variant": "Scatter", + "Keyspace": { + "Name": "user", + "Sharded": true + }, + "FieldQuery": "select id from `user` where 1 != 1", + "Query": "select /* comment */ id from `user` where id > 1 and id < 10", + "Table": "`user`" + }, + { + "InputName": "Outer", + "OperatorType": "Route", + "Variant": "Scatter", + "Keyspace": { + "Name": "user", + "Sharded": true + }, + "FieldQuery": "select `user`.col from `user` where 1 != 1", + "Query": "select /* comment */ `user`.col from `user` where :__sq_has_values and foo in ::__sq1", + "Table": "`user`" + } + ] + }, + "TablesUsed": [ + "user.user" + ] + } + }, { "comment": "for update", "query": "select user.col from user join user_extra for update",