Skip to content

Commit aa56cad

Browse files
Merge pull request #174 from github/mhamza/backport-18369
2 parents 50a5578 + 6d47856 commit aa56cad

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

go/vt/vtgate/planbuilder/operators/subquery_planning.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,9 @@ func tryMergeSubqueryWithOuter(ctx *plancontext.PlanningContext, subQuery *SubQu
514514
if !subQuery.IsArgument {
515515
op.Source = newFilter(outer.Source, subQuery.Original)
516516
}
517+
if outer.Comments != nil {
518+
op.Comments = outer.Comments
519+
}
517520
ctx.MergedSubqueries = append(ctx.MergedSubqueries, subQuery.originalSubquery)
518521
return op, Rewrote("merged subquery with outer")
519522
}

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,73 @@
876876
]
877877
}
878878
},
879+
{
880+
"comment": "Comments with subquery",
881+
"query": "select /* comment */ user.col from user where id IN (select id from user where id > 1 and id < 10)",
882+
"plan": {
883+
"QueryType": "SELECT",
884+
"Original": "select /* comment */ user.col from user where id IN (select id from user where id > 1 and id < 10)",
885+
"Instructions": {
886+
"OperatorType": "Route",
887+
"Variant": "Scatter",
888+
"Keyspace": {
889+
"Name": "user",
890+
"Sharded": true
891+
},
892+
"FieldQuery": "select `user`.col from `user` where 1 != 1",
893+
"Query": "select /* comment */ `user`.col from `user` where id in (select id from `user` where id > 1 and id < 10)",
894+
"Table": "`user`"
895+
},
896+
"TablesUsed": [
897+
"user.user"
898+
]
899+
}
900+
},
901+
{
902+
"comment": "Comments with subquery not merged into a single route",
903+
"query": "select /* comment */ user.col from user where foo IN (select id from user where id > 1 and id < 10)",
904+
"plan": {
905+
"QueryType": "SELECT",
906+
"Original": "select /* comment */ user.col from user where foo IN (select id from user where id > 1 and id < 10)",
907+
"Instructions": {
908+
"OperatorType": "UncorrelatedSubquery",
909+
"Variant": "PulloutIn",
910+
"PulloutVars": [
911+
"__sq_has_values",
912+
"__sq1"
913+
],
914+
"Inputs": [
915+
{
916+
"InputName": "SubQuery",
917+
"OperatorType": "Route",
918+
"Variant": "Scatter",
919+
"Keyspace": {
920+
"Name": "user",
921+
"Sharded": true
922+
},
923+
"FieldQuery": "select id from `user` where 1 != 1",
924+
"Query": "select /* comment */ id from `user` where id > 1 and id < 10",
925+
"Table": "`user`"
926+
},
927+
{
928+
"InputName": "Outer",
929+
"OperatorType": "Route",
930+
"Variant": "Scatter",
931+
"Keyspace": {
932+
"Name": "user",
933+
"Sharded": true
934+
},
935+
"FieldQuery": "select `user`.col from `user` where 1 != 1",
936+
"Query": "select /* comment */ `user`.col from `user` where :__sq_has_values and foo in ::__sq1",
937+
"Table": "`user`"
938+
}
939+
]
940+
},
941+
"TablesUsed": [
942+
"user.user"
943+
]
944+
}
945+
},
879946
{
880947
"comment": "for update",
881948
"query": "select user.col from user join user_extra for update",

0 commit comments

Comments
 (0)