Skip to content

Commit

Permalink
Revert the changes made in #11306
Browse files Browse the repository at this point in the history
Signed-off-by: Florent Poinsard <[email protected]>
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Oct 17, 2022
1 parent a2a010c commit 44df41b
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 19 deletions.
49 changes: 30 additions & 19 deletions go/vt/vtgate/planbuilder/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,29 +174,40 @@ func rewriteHavingClause(node *sqlparser.Select) {
exprs := sqlparser.SplitAndExpression(nil, node.Having.Expr)
node.Having = nil
for _, expr := range exprs {
var hasAggr bool
sqlparser.Rewrite(expr, func(cursor *sqlparser.Cursor) bool {
switch x := cursor.Node().(type) {
case *sqlparser.ColName:
if !x.Qualifier.IsEmpty() {
return false
}
originalExpr, isInMap := selectExprMap[x.Name.Lowered()]
if isInMap && sqlparser.ContainsAggregation(originalExpr) {
hasAggr = true
}
return false
default:
_, isAggregate := x.(sqlparser.AggrFunc)
hasAggr = hasAggr || isAggregate
}
return true
}, nil)

hasAggr := sqlparser.ContainsAggregation(expr)
if !hasAggr {
sqlparser.Rewrite(expr, func(cursor *sqlparser.Cursor) bool {
visitColName(cursor.Node(), selectExprMap, func(original sqlparser.Expr) {
if sqlparser.ContainsAggregation(original) {
hasAggr = true
}
})
return true
}, nil)
}
if hasAggr {
node.AddHaving(expr)
} else {
sqlparser.Rewrite(expr, func(cursor *sqlparser.Cursor) bool {
visitColName(cursor.Node(), selectExprMap, func(original sqlparser.Expr) {
cursor.Replace(original)
})
return true
}, nil)
node.AddWhere(expr)
}
}
}
func visitColName(cursor sqlparser.SQLNode, selectExprMap map[string]sqlparser.Expr, f func(original sqlparser.Expr)) {
switch x := cursor.(type) {
case *sqlparser.ColName:
if !x.Qualifier.IsEmpty() {
return
}
originalExpr, isInMap := selectExprMap[x.Name.Lowered()]
if isInMap {
f(originalExpr)
}
return
}
}
74 changes: 74 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/filter_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -6421,5 +6421,79 @@
"user.user"
]
}
},
{
"comment": "HAVING predicates that use table columns are safe to rewrite if we can move them to the WHERE clause",
"query": "select user.col + 2 as a from user having a = 42",
"v3-plan": {
"QueryType": "SELECT",
"Original": "select user.col + 2 as a from user having a = 42",
"Instructions": {
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select `user`.col + 2 as a from `user` where 1 != 1",
"Query": "select `user`.col + 2 as a from `user` having a = 42",
"Table": "`user`"
}
},
"gen4-plan": {
"QueryType": "SELECT",
"Original": "select user.col + 2 as a from user having a = 42",
"Instructions": {
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select `user`.col + 2 as a from `user` where 1 != 1",
"Query": "select `user`.col + 2 as a from `user` where `user`.col + 2 = 42",
"Table": "`user`"
},
"TablesUsed": [
"user.user"
]
}
},
{
"comment": "HAVING predicates that use table columns should not get rewritten on unsharded keyspaces",
"query": "select col + 2 as a from unsharded having a = 42",
"v3-plan": {
"QueryType": "SELECT",
"Original": "select col + 2 as a from unsharded having a = 42",
"Instructions": {
"OperatorType": "Route",
"Variant": "Unsharded",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"FieldQuery": "select col + 2 as a from unsharded where 1 != 1",
"Query": "select col + 2 as a from unsharded having a = 42",
"Table": "unsharded"
}
},
"gen4-plan": {
"QueryType": "SELECT",
"Original": "select col + 2 as a from unsharded having a = 42",
"Instructions": {
"OperatorType": "Route",
"Variant": "Unsharded",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"FieldQuery": "select col + 2 as a from unsharded where 1 != 1",
"Query": "select col + 2 as a from unsharded having a = 42",
"Table": "unsharded"
},
"TablesUsed": [
"main.unsharded"
]
}
}
]

0 comments on commit 44df41b

Please sign in to comment.