diff --git a/src/sqlfluff/rules/L022.py b/src/sqlfluff/rules/L022.py index b9404f3450f..a7f7aa6bff8 100644 --- a/src/sqlfluff/rules/L022.py +++ b/src/sqlfluff/rules/L022.py @@ -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: diff --git a/test/fixtures/rules/std_rule_cases/L022.yml b/test/fixtures/rules/std_rule_cases/L022.yml index 7a6a3cc007c..06ff11cce5d 100644 --- a/test/fixtures/rules/std_rule_cases/L022.yml +++ b/test/fixtures/rules/std_rule_cases/L022.yml @@ -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;