Skip to content

Commit

Permalink
[release-17.0] fix: don't forget DISTINCT for derived tables (#15672) (
Browse files Browse the repository at this point in the history
…#15676)

Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
Co-authored-by: Andres Taylor <[email protected]>
  • Loading branch information
vitess-bot[bot] and systay authored Apr 9, 2024
1 parent 1d4f195 commit 8a4fba0
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 38 deletions.
1 change: 1 addition & 0 deletions go/vt/vtgate/planbuilder/operators/SQL_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ func buildDerived(op *Derived, qb *queryBuilder) error {
sel.GroupBy = opQuery.GroupBy
sel.Having = mergeHaving(sel.Having, opQuery.Having)
sel.SelectExprs = opQuery.SelectExprs
sel.Distinct = opQuery.Distinct
qb.addTableExpr(op.Alias, op.Alias, TableID(op), &sqlparser.DerivedTable{
Select: sel,
}, nil, op.ColumnAliases)
Expand Down
93 changes: 55 additions & 38 deletions go/vt/vtgate/planbuilder/testdata/from_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,60 @@
]
}
},
{
"comment": "DISTINCT inside derived table",
"query": "select * from (select distinct name from user) as t",
"v3-plan": {
"QueryType": "SELECT",
"Original": "select * from (select distinct name from user) as t",
"Instructions": {
"OperatorType": "SimpleProjection",
"Columns": [
0
],
"Inputs": [
{
"OperatorType": "Aggregate",
"Variant": "Ordered",
"GroupBy": "0",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select `name`, weight_string(`name`) from `user` where 1 != 1",
"OrderBy": "(0|1) ASC",
"Query": "select distinct `name`, weight_string(`name`) from `user` order by `name` asc",
"ResultColumns": 1,
"Table": "`user`"
}
]
}
]
}
},
"gen4-plan": {
"QueryType": "SELECT",
"Original": "select * from (select distinct name from user) as t",
"Instructions": {
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select t.`name` from (select `name` from `user` where 1 != 1) as t where 1 != 1",
"Query": "select t.`name` from (select distinct `name` from `user`) as t",
"Table": "`user`"
},
"TablesUsed": [
"user.user"
]
}
},
{
"comment": "',' join unsharded",
"query": "select u1.a, u2.a from unsharded u1, unsharded u2",
Expand Down Expand Up @@ -6374,44 +6428,7 @@
{
"comment": "join predicate only depending on the RHS should not turn outer join into inner join",
"query": "select t1.id1, t2.id1 from t1 left join t1 as t2 on t2.id1 = t2.id2",
"v3-plan": {
"QueryType": "SELECT",
"Original": "select t1.id1, t2.id1 from t1 left join t1 as t2 on t2.id1 = t2.id2",
"Instructions": {
"OperatorType": "Join",
"Variant": "LeftJoin",
"JoinColumnIndexes": "L:0,R:0",
"TableName": "t1_t1",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "zlookup_unique",
"Sharded": true
},
"FieldQuery": "select t1.id1 from t1 where 1 != 1",
"Query": "select t1.id1 from t1",
"Table": "t1"
},
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "zlookup_unique",
"Sharded": true
},
"FieldQuery": "select t2.id1 from t1 as t2 where 1 != 1",
"Query": "select t2.id1 from t1 as t2 where t2.id1 = t2.id2",
"Table": "t1"
}
]
},
"TablesUsed": [
"zlookup_unique.t1"
]
},
"gen4-plan": {
"plan": {
"QueryType": "SELECT",
"Original": "select t1.id1, t2.id1 from t1 left join t1 as t2 on t2.id1 = t2.id2",
"Instructions": {
Expand Down

0 comments on commit 8a4fba0

Please sign in to comment.