Skip to content

Commit

Permalink
Move more rewriting to SafeRewrite (#12063)
Browse files Browse the repository at this point in the history
* Update SafeRewrite to allow for replacing a node that we don't want to visit the children of

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

* Clean up ast_rewriting before refactoring it

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

* feat: fix usages of Rewrite in ast_rewriting

Signed-off-by: Manan Gupta <[email protected]>

* feat: fix usages of Rewrite in normalizer

Signed-off-by: Manan Gupta <[email protected]>

* safe rewrite having clause during horizon planning

Signed-off-by: Florent Poinsard <[email protected]>

* move rewriteHavingAndOrderBy to safeRewrite

Signed-off-by: Florent Poinsard <[email protected]>

* refactor: clean up code

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

Signed-off-by: Andres Taylor <[email protected]>
Signed-off-by: Manan Gupta <[email protected]>
Signed-off-by: Florent Poinsard <[email protected]>
Co-authored-by: Manan Gupta <[email protected]>
Co-authored-by: Florent Poinsard <[email protected]>
  • Loading branch information
3 people authored Jan 20, 2023
1 parent 35220d7 commit bc94876
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 221 deletions.
16 changes: 10 additions & 6 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ func NewWhere(typ WhereType, expr Expr) *Where {
// and replaces it with to. If from matches root,
// then to is returned.
func ReplaceExpr(root, from, to Expr) Expr {
tmp := Rewrite(root, replaceExpr(from, to), nil)
tmp := SafeRewrite(root, stopWalking, replaceExpr(from, to))

expr, success := tmp.(Expr)
if !success {
Expand All @@ -454,16 +454,20 @@ func ReplaceExpr(root, from, to Expr) Expr {
return expr
}

func stopWalking(e SQLNode, _ SQLNode) bool {
switch e.(type) {
case *ExistsExpr, *Literal, *Subquery, *ValuesFuncExpr, *Default:
return false
default:
return true
}
}

func replaceExpr(from, to Expr) func(cursor *Cursor) bool {
return func(cursor *Cursor) bool {
if cursor.Node() == from {
cursor.Replace(to)
}
switch cursor.Node().(type) {
case *ExistsExpr, *Literal, *Subquery, *ValuesFuncExpr, *Default:
return false
}

return true
}
}
Expand Down
Loading

0 comments on commit bc94876

Please sign in to comment.