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

L042: Enable autofix for some tricky cases #3700

Merged

Conversation

barrywhart
Copy link
Member

@barrywhart barrywhart commented Jul 31, 2022

Brief summary of the change made

  • Rewrite of L042 to fix a "linter looping" bug. Fixed some other previously unnoticed issues as well. (See L042.yml for details.)
  • The rewrite uses SelectCrawler and is a bit shorter/simpler as a result.
  • The rewrite surfaced and fixes some issues with SelectCrawler, especially involving nested queries and consistency around handling of subqueries in FROM clauses. Adds automated tests for SelectCrawler, which previously had none.

Fixes #3598

Are there any other side effects of this change that we should be aware of?

The changes to SelectCrawler could surface some issues (e.g. I had to update L028 to get a test to pass).

Pull Request checklist

  • Please confirm you have completed any of the necessary steps below.

  • Included test cases to demonstrate any code changes, which may be one or more of the following:

    • .yml rule test cases in test/fixtures/rules/std_rule_cases.
    • .sql/.yml parser test cases in test/fixtures/dialects (note YML files can be auto generated with tox -e generate-fixture-yml).
    • Full autofix test cases in test/fixtures/linter/autofix.
    • Other.
  • Added appropriate documentation for the change.

  • Created GitHub issues for any relevant followup/future enhancements if appropriate.

@barrywhart barrywhart marked this pull request as draft July 31, 2022 18:22
@codecov
Copy link

codecov bot commented Jul 31, 2022

Codecov Report

Base: 100.00% // Head: 100.00% // No change to project coverage 👍

Coverage data is based on head (cf24595) compared to base (1000cf1).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #3700   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          179       179           
  Lines        13729     13752   +23     
=========================================
+ Hits         13729     13752   +23     
Impacted Files Coverage Δ
src/sqlfluff/core/parser/segments/base.py 100.00% <100.00%> (ø)
src/sqlfluff/rules/L028.py 100.00% <100.00%> (ø)
src/sqlfluff/rules/L042.py 100.00% <100.00%> (ø)
src/sqlfluff/utils/analysis/select_crawler.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@alanmcruickshank
Copy link
Member

@barrywhart - are you ok incorporating #3717 into this or do you need a hand?

@barrywhart
Copy link
Member Author

@alanmcruickshank: I'm okay, thanks! This is a slow-moving, slightly experimental PR anyway.

@barrywhart barrywhart marked this pull request as ready for review September 12, 2022 21:42
FROM (SELECT a)
)
SELECT a FROM cte1
fix_str: |
Copy link
Member Author

Choose a reason for hiding this comment

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

Fixes previously unfixable issue that had previously triggered linter looping

children = list(query.children)
# 'query.children' includes CTEs and "main" queries, but not queries in
# the "FROM" list. We want to visit those as well.
for a in select_info.table_aliases if select_info else []:
Copy link
Member Author

Choose a reason for hiding this comment

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

This additional code is now required because of the SelectCrawler improvements. Previously, the behavior of SelectCrawler was inconsistent about whether it returned queries in the FROM list.

violations_after_fix:
- description: select_statement clauses should not contain subqueries. Use CTEs instead
line_no: 2
line_pos: 20
Copy link
Member Author

Choose a reason for hiding this comment

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

violations_after_fix because the two as b aliases collide, preventing autofix from fixing the second one.

Copy link
Contributor

@OTooleMichael OTooleMichael left a comment

Choose a reason for hiding this comment

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

LGTM - mostly style comments

children = list(query.children)
# 'query.children' includes CTEs and "main" queries, but not queries in
# the "FROM" list. We want to visit those as well.
for a in select_info.table_aliases if select_info else []:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
for a in select_info.table_aliases if select_info else []:
for a in (select_info.table_aliases if select_info else []):

perhaps brackets or a variable, the turnary in line with a loop is an overload for me

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll change it. Probably will add an if around it.

Comment on lines 151 to 152
if isinstance(q, Query):
# Check for previously visited selectables to avoid possible
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if isinstance(q, Query):
# Check for previously visited selectables to avoid possible
if not isinstance(q, Query):
continue
# Check for previously visited selectables to avoid possible

Copy link
Member Author

Choose a reason for hiding this comment

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

Will do

Comment on lines 156 to 157
if not any(s.selectable in visited for s in q.selectables):
visited.update(s.selectable for s in q.selectables)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if not any(s.selectable in visited for s in q.selectables):
visited.update(s.selectable for s in q.selectables)
if any(s.selectable in visited for s in q.selectables):
continue
visited.update(s.selectable for s in q.selectables)

Copy link
Member Author

Choose a reason for hiding this comment

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

Will do

Comment on lines 203 to 208
"""Given the root query, compute lint warnings."""
parent_types = self._config_mapping[self.forbid_subquery_in]
for q in [query] + list(query.ctes.values()):
for selectable in q.selectables:
if not selectable.select_info:
continue
Copy link
Contributor

Choose a reason for hiding this comment

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

This method feels quite dense, I can't really make my way through it. Is there any code splitting possible?

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably. I'll take a look.

@barrywhart barrywhart merged commit bca4bfc into sqlfluff:main Sep 13, 2022
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.

L042 loop limit on fixes reached when CTE itself contains a subquery
3 participants