Skip to content

Commit

Permalink
[release-19.0] bugfix: add HAVING columns inside derived tables (#16976
Browse files Browse the repository at this point in the history
…) (#16978)

Signed-off-by: Andres Taylor <andres@planetscale.com>
Co-authored-by: Andrés Taylor <andres@planetscale.com>
  • Loading branch information
vitess-bot[bot] and systay authored Oct 17, 2024
1 parent 53e72f8 commit 60ef036
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 35 deletions.
7 changes: 7 additions & 0 deletions go/vt/vtgate/planbuilder/operators/horizon_expanding.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ func expandSelectHorizon(ctx *plancontext.PlanningContext, horizon *Horizon, sel
for _, order := range horizon.Query.GetOrderBy() {
qp.addDerivedColumn(ctx, order.Expr)
}
sel, isSel := horizon.Query.(*sqlparser.Select)
if isSel && sel.Having != nil {
for _, pred := range sqlparser.SplitAndExpression(nil, sel.Having.Expr) {
qp.addDerivedColumn(ctx, pred)
}
}
}
op := createProjectionFromSelect(ctx, horizon)
if qp.HasAggr {
Expand Down Expand Up @@ -294,6 +300,7 @@ outer:
func createProjectionForComplexAggregation(a *Aggregator, qp *QueryProjection) Operator {
p := newAliasedProjection(a)
p.DT = a.DT
a.DT = nil // we don't need the derived table twice
for _, expr := range qp.SelectExprs {
ae, err := expr.GetAliasedExpr()
if err != nil {
Expand Down
85 changes: 65 additions & 20 deletions go/vt/vtgate/planbuilder/testdata/aggr_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,35 @@
]
}
},
{
"comment": "using HAVING inside a derived table still produces viable plans",
"query": "select id from (select id from user group by id having (count(user.id) = 2) limit 2 offset 0) subquery_for_limit",
"plan": {
"QueryType": "SELECT",
"Original": "select id from (select id from user group by id having (count(user.id) = 2) limit 2 offset 0) subquery_for_limit",
"Instructions": {
"OperatorType": "Limit",
"Count": "2",
"Offset": "0",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id from (select id, count(`user`.id) = 2 from `user` where 1 != 1 group by id) as subquery_for_limit where 1 != 1",
"Query": "select id from (select id, count(`user`.id) = 2 from `user` group by id having count(`user`.id) = 2) as subquery_for_limit limit :__upper_limit",
"Table": "`user`"
}
]
},
"TablesUsed": [
"user.user"
]
}
},
{
"comment": "sum with distinct no unique vindex",
"query": "select col1, sum(distinct col2) from user group by col1",
Expand Down Expand Up @@ -3614,25 +3643,41 @@
"QueryType": "SELECT",
"Original": "select * from (select id from user having count(*) = 1) s",
"Instructions": {
"OperatorType": "Filter",
"Predicate": "count(*) = 1",
"ResultColumns": 1,
"OperatorType": "SimpleProjection",
"Columns": [
0
],
"Inputs": [
{
"OperatorType": "Aggregate",
"Variant": "Scalar",
"Aggregates": "any_value(0) AS id, sum_count_star(1) AS count(*)",
"OperatorType": "Projection",
"Expressions": [
":0 as id",
"count(*) = 1 as count(*) = 1"
],
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id, count(*) from `user` where 1 != 1",
"Query": "select id, count(*) from `user`",
"Table": "`user`"
"OperatorType": "Filter",
"Predicate": "count(*) = 1",
"Inputs": [
{
"OperatorType": "Aggregate",
"Variant": "Scalar",
"Aggregates": "any_value(0) AS id, sum_count_star(1) AS count(*), any_value(2)",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id, count(*), 1 from `user` where 1 != 1",
"Query": "select id, count(*), 1 from `user`",
"Table": "`user`"
}
]
}
]
}
]
}
Expand Down Expand Up @@ -6548,11 +6593,11 @@
{
"OperatorType": "SimpleProjection",
"Columns": [
2,
0,
1,
3
],
2,
0,
1,
3
],
"Inputs": [
{
"OperatorType": "Sort",
Expand Down
46 changes: 31 additions & 15 deletions go/vt/vtgate/planbuilder/testdata/cte_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -369,25 +369,41 @@
"QueryType": "SELECT",
"Original": "with s as (select id from user having count(*) = 1) select * from s",
"Instructions": {
"OperatorType": "Filter",
"Predicate": "count(*) = 1",
"ResultColumns": 1,
"OperatorType": "SimpleProjection",
"Columns": [
0
],
"Inputs": [
{
"OperatorType": "Aggregate",
"Variant": "Scalar",
"Aggregates": "any_value(0) AS id, sum_count_star(1) AS count(*)",
"OperatorType": "Projection",
"Expressions": [
":0 as id",
"count(*) = 1 as count(*) = 1"
],
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id, count(*) from `user` where 1 != 1",
"Query": "select id, count(*) from `user`",
"Table": "`user`"
"OperatorType": "Filter",
"Predicate": "count(*) = 1",
"Inputs": [
{
"OperatorType": "Aggregate",
"Variant": "Scalar",
"Aggregates": "any_value(0) AS id, sum_count_star(1) AS count(*), any_value(2)",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select id, count(*), 1 from `user` where 1 != 1",
"Query": "select id, count(*), 1 from `user`",
"Table": "`user`"
}
]
}
]
}
]
}
Expand Down

0 comments on commit 60ef036

Please sign in to comment.