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

Fix L022 to not flag CTE column definitions #2139

Merged
merged 4 commits into from Dec 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/sqlfluff/rules/L022.py
Expand Up @@ -54,7 +54,20 @@ def _eval(self, context: RuleContext) -> Optional[List[LintResult]]:
)
for idx, seg in enumerate(expanded_segments):
if seg.is_type("bracketed"):
bracket_indices.append(idx)
# Check if the preceding keyword is AS, otherwise it's a column name definition in the CTE.
preceding_keyword = next(
(
s
for s in expanded_segments[:idx][::-1]
if s.is_type("keyword")
),
None,
)
if (
preceding_keyword is not None
and preceding_keyword.raw.upper() == "AS"
):
bracket_indices.append(idx)

# Work through each point and deal with it individually
for bracket_idx in bracket_indices:
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/rules/std_rule_cases/L022.yml
Expand Up @@ -151,3 +151,14 @@ test_fail_cte_floating_comma:
other_cte as (select 1)

select * from my_cte cross join other_cte

test_pass_column_name_definition:
# Issue #2136
pass_str: |
with recursive t(n) as (
select 1
union all
select n + 1 from t
)

select n from t limit 100;