Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-21.0] [Direct PR] bugfix: add HAVING columns inside derived tables (#16976) #16980

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,7 @@ from (select 1 as one

select u.id, u.t1_id, t.num_segments
from (select id, count(*) as num_segments from t1 group by 1 order by 2 desc limit 20) t
join t2 u on u.id = t.id;
join t2 u on u.id = t.id;

select name
from (select name from t1 group by name having count(t1.id) > 1) t1;
6 changes: 6 additions & 0 deletions go/vt/vtgate/planbuilder/operators/horizon_expanding.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,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)
Expand Down
76 changes: 61 additions & 15 deletions go/vt/vtgate/planbuilder/testdata/aggr_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,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 2",
"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 @@ -3774,25 +3803,42 @@
"QueryType": "SELECT",
"Original": "select * from (select id from user having count(*) = 1) s",
"Instructions": {
"OperatorType": "Filter",
"Predicate": "count(*) = 1",
"ResultColumns": 1,
"OperatorType": "SimpleProjection",
"ColumnNames": [
"0:id"
],
"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
47 changes: 32 additions & 15 deletions go/vt/vtgate/planbuilder/testdata/cte_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -379,25 +379,42 @@
"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",
"ColumnNames": [
"0:id"
],
"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
Loading