From 5fc37e297bdda371b8e806eb787da413aa2636ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Taylor?= Date: Wed, 16 Oct 2024 16:28:39 +0200 Subject: [PATCH] bugfix: add HAVING columns inside derived tables (#16976) Signed-off-by: Andres Taylor --- .../operators/horizon_expanding.go | 6 ++ .../planbuilder/testdata/aggr_cases.json | 76 +++++++++++++++---- 2 files changed, 67 insertions(+), 15 deletions(-) diff --git a/go/vt/vtgate/planbuilder/operators/horizon_expanding.go b/go/vt/vtgate/planbuilder/operators/horizon_expanding.go index a2f0799c547..bf7e95f5a17 100644 --- a/go/vt/vtgate/planbuilder/operators/horizon_expanding.go +++ b/go/vt/vtgate/planbuilder/operators/horizon_expanding.go @@ -90,6 +90,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, err := createProjectionFromSelect(ctx, horizon) diff --git a/go/vt/vtgate/planbuilder/testdata/aggr_cases.json b/go/vt/vtgate/planbuilder/testdata/aggr_cases.json index 127dd2c4837..bf12cd7b884 100644 --- a/go/vt/vtgate/planbuilder/testdata/aggr_cases.json +++ b/go/vt/vtgate/planbuilder/testdata/aggr_cases.json @@ -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 2", + "Table": "`user`" + } + ] + }, + "TablesUsed": [ + "user.user" + ] + } + }, { "comment": "sum with distinct no unique vindex", "query": "select col1, sum(distinct col2) from user group by col1", @@ -3613,25 +3642,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`" + } + ] + } + ] } ] }