Skip to content
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

add {pre,post}_visit_query to Visitor #1044

Merged
merged 4 commits into from
Nov 27, 2023
Merged

Conversation

jmhain
Copy link
Contributor

@jmhain jmhain commented Nov 9, 2023

Title says it all.

@coveralls
Copy link

coveralls commented Nov 9, 2023

Pull Request Test Coverage Report for Build 6934216371

  • 0 of 14 (0.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.06%) to 87.645%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/ast/visitor.rs 0 14 0.0%
Totals Coverage Status
Change from base Build 6932119322: -0.06%
Covered Lines: 17791
Relevant Lines: 20299

💛 - Coveralls

Copy link
Collaborator

@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.

Thank you for this contribution @jmhain -- can you please add a test for this new functionality?

derive/src/lib.rs Outdated Show resolved Hide resolved
@jmhain
Copy link
Contributor Author

jmhain commented Nov 20, 2023

@alamb Sorry for not including tests in the original PR, I just wanted to make sure this functionality was desired first. :) In adding the tests I found a few bugs that resulted from me misunderstanding how the visit macro works. If you include the #[visit] attribute on both the type and the field, then the visitor methods actually end up being called twice. My recent PR that added UNPIVOT support had the same issue, as did the existing PIVOT table factor that I had used as a reference.

I've fixed each of these, added tests that previously reproduced the redundant calls, and added some additional details to the README.md for the derive crate to ensure that others don't make the same error.

Copy link
Collaborator

@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.

Thank you @jmhain -- this looks like a nice contribution to me. @tustvold do you have a few minutes to review this?

@@ -739,7 +740,6 @@ pub enum TableFactor {
/// For example `FROM monthly_sales PIVOT(sum(amount) FOR MONTH IN ('JAN', 'FEB'))`
/// See <https://docs.snowflake.com/en/sql-reference/constructs/pivot>
Pivot {
#[cfg_attr(feature = "visitor", visit(with = "visit_table_factor"))]
Copy link
Collaborator

Choose a reason for hiding this comment

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

why were these removed?

Copy link
Contributor Author

@jmhain jmhain Nov 20, 2023

Choose a reason for hiding this comment

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

TableFactor is already annotated with this, so it results in two calls to both the pre_ and post_visit_table_factor for the same table factor, which besides being unnecessary is likely to cause issues in user's code if their visitor doesn't happen to be idempotent.

As an example, this is the output of one of the new tests I added without this change:

 [
     "PRE: STATEMENT: SELECT * FROM monthly_sales PIVOT(SUM(a.amount) FOR a.MONTH IN ('JAN', 'FEB', 'MAR', 'APR')) AS p (c, d) ORDER BY EMPID",
     "PRE: QUERY: SELECT * FROM monthly_sales PIVOT(SUM(a.amount) FOR a.MONTH IN ('JAN', 'FEB', 'MAR', 'APR')) AS p (c, d) ORDER BY EMPID",
     "PRE: TABLE FACTOR: monthly_sales PIVOT(SUM(a.amount) FOR a.MONTH IN ('JAN', 'FEB', 'MAR', 'APR')) AS p (c, d)",
     "PRE: TABLE FACTOR: monthly_sales",
     "PRE: TABLE FACTOR: monthly_sales",
     "PRE: RELATION: monthly_sales",
     "POST: RELATION: monthly_sales",
     "POST: TABLE FACTOR: monthly_sales",
     "POST: TABLE FACTOR: monthly_sales",
     "PRE: EXPR: SUM(a.amount)",
     "PRE: EXPR: a.amount",
     "POST: EXPR: a.amount",
     "POST: EXPR: SUM(a.amount)",
     "POST: TABLE FACTOR: monthly_sales PIVOT(SUM(a.amount) FOR a.MONTH IN ('JAN', 'FEB', 'MAR', 'APR')) AS p (c, d)",
     "PRE: EXPR: EMPID",
     "POST: EXPR: EMPID",
     "POST: QUERY: SELECT * FROM monthly_sales PIVOT(SUM(a.amount) FOR a.MONTH IN ('JAN', 'FEB', 'MAR', 'APR')) AS p (c, d) ORDER BY EMPID",
     "POST: STATEMENT: SELECT * FROM monthly_sales PIVOT(SUM(a.amount) FOR a.MONTH IN ('JAN', 'FEB', 'MAR', 'APR')) AS p (c, d) ORDER BY EMPID",
 ]

Note the duplicate calls for TABLE FACTOR: monthly_sales".

I added a section in the README.md for the derive crate that explains this in more detail: https://github.com/jmhain/sqlparser-rs/blob/visit_query/derive/README.md

I can split this into a separate PR if you'd like since it's separate from adding {pre,post}_visit_query, I just discovered it now because I almost introduced another instance of this same bug here.

Copy link
Collaborator

@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.

Thank you very much @jmhain

@alamb alamb mentioned this pull request Nov 27, 2023
5 tasks
@alamb alamb merged commit 86aa044 into sqlparser-rs:main Nov 27, 2023
10 checks passed
@jmhain jmhain deleted the visit_query branch November 27, 2023 17:51
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.

None yet

3 participants