diff --git a/go/vt/vtgate/planbuilder/rewrite.go b/go/vt/vtgate/planbuilder/rewrite.go index d1b6a789a1b..284ffa9c079 100644 --- a/go/vt/vtgate/planbuilder/rewrite.go +++ b/go/vt/vtgate/planbuilder/rewrite.go @@ -182,12 +182,8 @@ func rewriteHavingClause(node *sqlparser.Select) { return false } originalExpr, isInMap := selectExprMap[x.Name.Lowered()] - if isInMap { - if sqlparser.ContainsAggregation(originalExpr) { - hasAggr = true - } else { - cursor.Replace(originalExpr) - } + if isInMap && sqlparser.ContainsAggregation(originalExpr) { + hasAggr = true } return false default: diff --git a/go/vt/vtgate/planbuilder/testdata/filter_cases.json b/go/vt/vtgate/planbuilder/testdata/filter_cases.json index b6230ae2345..3f7d0ea26c1 100644 --- a/go/vt/vtgate/planbuilder/testdata/filter_cases.json +++ b/go/vt/vtgate/planbuilder/testdata/filter_cases.json @@ -4128,6 +4128,7 @@ } }, { + "comment": "select * from samecolvin where col = :col", "query": "select * from samecolvin where col = :col", "v3-plan": { "QueryType": "SELECT", @@ -6383,5 +6384,42 @@ "user.music" ] } + }, + { + "comment": "Self referencing columns in HAVING should work", + "query": "select a+2 as a from user having a = 42", + "v3-plan": { + "QueryType": "SELECT", + "Original": "select a+2 as a from user having a = 42", + "Instructions": { + "OperatorType": "Route", + "Variant": "Scatter", + "Keyspace": { + "Name": "user", + "Sharded": true + }, + "FieldQuery": "select a + 2 as a from `user` where 1 != 1", + "Query": "select a + 2 as a from `user` having a = 42", + "Table": "`user`" + } + }, + "gen4-plan": { + "QueryType": "SELECT", + "Original": "select a+2 as a from user having a = 42", + "Instructions": { + "OperatorType": "Route", + "Variant": "Scatter", + "Keyspace": { + "Name": "user", + "Sharded": true + }, + "FieldQuery": "select a + 2 as a from `user` where 1 != 1", + "Query": "select a + 2 as a from `user` where a + 2 = 42", + "Table": "`user`" + }, + "TablesUsed": [ + "user.user" + ] + } } ]