Skip to content

use post_* visitors for mutable visits #789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2023

Conversation

lovasoa
Copy link
Contributor

@lovasoa lovasoa commented Jan 4, 2023

My initial implementation of mutable visitors closely matched the existing immutable visitor implementations,
and used pre_* visitors (a mutable expression was first passed to the visitor, then its children were visited).

After some initial usage, I realize this is actually impractical, and can easily lead to infinite recursion when one isn't careful.

For instance a mutable visitor would replace x by f(x), then would be called again on the nested x and result in f(f(x)), then again resulting in f(f(f(x))), and so on.

So, this commit changes the behavior of the visit_*_mut functions to call the visitor on an expression only once all of its children have been visited.

It also makes the documentation more explicit and adds an example.

My initial implementation of mutable visitors closely matched the existing immutable visitor implementations,
and used pre_* visitors (a mutable expression was first passed the visitor, then its children were visited).

After some initial usage, I realize this actually impractical, and can easily lead to infinite recursion when one isn't careful.
For instance a mutable visitor would replace x by f(x),
then would be called again on the nested x and result in f(f(x)),
then again resulting in f(f(f(x))), and so on.

So, this commit changes the behavior of the visit_*_mut functions to call the visitor on an expression only
once all of its children have been visited.

It also makes the documentation more explicit and adds an example.
@coveralls
Copy link

Pull Request Test Coverage Report for Build 3838883231

  • 0 of 3 (0.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 85.941%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/ast/visitor.rs 0 3 0.0%
Totals Coverage Status
Change from base Build 3823674519: 0.0%
Covered Lines: 13185
Relevant Lines: 15342

💛 - Coveralls

@alamb
Copy link
Contributor

alamb commented Jan 9, 2023

I am sorry I am a bit behind on reviews / merging in sqlparser-rs. I hope to be able to catch up later this week

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me @lovasoa

@alamb
Copy link
Contributor

alamb commented Jan 9, 2023

Thank you for the contribution

@alamb alamb merged commit 3f874f4 into apache:main Jan 9, 2023
@lovasoa lovasoa deleted the mutable-visit-dfs branch January 10, 2023 09:24
@lovasoa
Copy link
Contributor Author

lovasoa commented Jan 10, 2023

Thanks for merging, @alamb !

@alamb
Copy link
Contributor

alamb commented Jan 11, 2023

Thank you for the contribution @lovasoa

Ziinc pushed a commit to Logflare/sqlparser-rs that referenced this pull request Jan 20, 2023
My initial implementation of mutable visitors closely matched the existing immutable visitor implementations,
and used pre_* visitors (a mutable expression was first passed the visitor, then its children were visited).

After some initial usage, I realize this actually impractical, and can easily lead to infinite recursion when one isn't careful.
For instance a mutable visitor would replace x by f(x),
then would be called again on the nested x and result in f(f(x)),
then again resulting in f(f(f(x))), and so on.

So, this commit changes the behavior of the visit_*_mut functions to call the visitor on an expression only
once all of its children have been visited.

It also makes the documentation more explicit and adds an example.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants