Skip to content

Commit

Permalink
planner fix: using self-referencing columns in HAVING should not stac…
Browse files Browse the repository at this point in the history
…k overflow (#11504)

Signed-off-by: Andres Taylor <[email protected]>

Signed-off-by: Andres Taylor <[email protected]>
Co-authored-by: Andres Taylor <[email protected]>
  • Loading branch information
vitess-bot[bot] and systay authored Oct 17, 2022
1 parent b942dc3 commit 8e40e6f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
8 changes: 2 additions & 6 deletions go/vt/vtgate/planbuilder/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
38 changes: 38 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/filter_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -4128,6 +4128,7 @@
}
},
{
"comment": "select * from samecolvin where col = :col",
"query": "select * from samecolvin where col = :col",
"v3-plan": {
"QueryType": "SELECT",
Expand Down Expand Up @@ -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"
]
}
}
]

0 comments on commit 8e40e6f

Please sign in to comment.