Skip to content

Commit

Permalink
bugfix: do not rewrite an expression twice (vitessio#14641)
Browse files Browse the repository at this point in the history
  • Loading branch information
systay authored Nov 30, 2023
1 parent 43f754e commit 3176543
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
1 change: 0 additions & 1 deletion go/vt/vtgate/planbuilder/operators/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ func (a *Aggregator) findColInternal(ctx *plancontext.PlanningContext, ae *sqlpa
if offset >= 0 {
return offset
}
expr = a.DT.RewriteExpression(ctx, expr)

// Aggregator is little special and cannot work if the input offset are not matched with the aggregation columns.
// So, before pushing anything from above the aggregator offset planning needs to be completed.
Expand Down
75 changes: 75 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/aggr_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -6297,5 +6297,80 @@
"user.user"
]
}
},
{
"comment": "Group by aggregated column should not be a problem",
"query": "SELECT b.col FROM music AS b JOIN (SELECT MIN(bb.id) AS min_id, MAX(bb.id) AS max_id FROM user AS bb) AS foobars WHERE b.id > foobars.min_id GROUP BY b.col",
"plan": {
"QueryType": "SELECT",
"Original": "SELECT b.col FROM music AS b JOIN (SELECT MIN(bb.id) AS min_id, MAX(bb.id) AS max_id FROM user AS bb) AS foobars WHERE b.id > foobars.min_id GROUP BY b.col",
"Instructions": {
"OperatorType": "Aggregate",
"Variant": "Ordered",
"GroupBy": "(0|1)",
"ResultColumns": 1,
"Inputs": [
{
"OperatorType": "Sort",
"Variant": "Memory",
"OrderBy": "(0|1) ASC",
"Inputs": [
{
"OperatorType": "Join",
"Variant": "Join",
"JoinColumnIndexes": "R:0,R:1",
"JoinVars": {
"foobars_min_id": 0
},
"TableName": "`user`_music",
"Inputs": [
{
"OperatorType": "Aggregate",
"Variant": "Ordered",
"GroupBy": "0 COLLATE utf8mb4_0900_ai_ci",
"Inputs": [
{
"OperatorType": "Aggregate",
"Variant": "Scalar",
"Aggregates": "min(0|2) AS min_id, max(1|2) AS max_id",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select min(bb.id) as min_id, max(bb.id) as max_id, weight_string(bb.id) from `user` as bb where 1 != 1 group by weight_string(bb.id)",
"OrderBy": "0 ASC COLLATE utf8mb4_0900_ai_ci",
"Query": "select min(bb.id) as min_id, max(bb.id) as max_id, weight_string(bb.id) from `user` as bb group by weight_string(bb.id) order by min(bb.id) asc",
"Table": "`user`"
}
]
}
]
},
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select b.col, weight_string(b.col) from music as b where 1 != 1 group by b.col, weight_string(b.col)",
"Query": "select b.col, weight_string(b.col) from music as b where b.id > :foobars_min_id group by b.col, weight_string(b.col)",
"Table": "music"
}
]
}
]
}
]
},
"TablesUsed": [
"user.music",
"user.user"
]
}
}
]

0 comments on commit 3176543

Please sign in to comment.