Skip to content

Commit

Permalink
Merge branch 'main' into mysql_system_variables
Browse files Browse the repository at this point in the history
  • Loading branch information
barrywhart committed Jul 9, 2022
2 parents b88321e + 50f0638 commit f9a071b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/sqlfluff/rules/L013.py
Expand Up @@ -48,7 +48,8 @@ def _eval(self, context: RuleContext) -> Optional[LintResult]:
elements there are.
"""
segment = context.functional.segment
functional_context = context.functional
segment = functional_context.segment
children = segment.children()
# If we have an alias its all good
if children.any(sp.is_type("alias_expression")):
Expand All @@ -66,6 +67,16 @@ def _eval(self, context: RuleContext) -> Optional[LintResult]:
):
return None

parent_stack = functional_context.parent_stack

# Ignore if it is part of a CTE with column names
if (
parent_stack.last(sp.is_type("common_table_expression"))
.children()
.any(sp.is_type("cte_column_list"))
):
return None

select_clause_children = children.select(sp.not_(sp.is_name("star")))
is_complex_clause = _recursively_check_is_complex(select_clause_children)
if not is_complex_clause:
Expand All @@ -76,7 +87,7 @@ def _eval(self, context: RuleContext) -> Optional[LintResult]:
# Check *how many* elements/columns there are in the select
# statement. If this is the only one, then we won't
# report an error.
immediate_parent = context.functional.parent_stack.last()
immediate_parent = parent_stack.last()
elements = immediate_parent.children(sp.is_type("select_clause_element"))
num_elements = len(elements)

Expand Down
30 changes: 30 additions & 0 deletions test/fixtures/rules/std_rule_cases/L013.yml
Expand Up @@ -47,3 +47,33 @@ test_pass_function_emits:
configs:
core:
dialect: exasol

test_fail_cte_no_column_list:
fail_str: |
WITH cte AS (
SELECT
col_a,
min(col_b)
FROM my_table
GROUP BY 1
)
SELECT
a,
b
FROM cte
test_pass_cte_column_list:
pass_str: |
WITH cte(a, b) AS (
SELECT
col_a,
min(col_b)
FROM my_table
GROUP BY 1
)
SELECT
a,
b
FROM cte

0 comments on commit f9a071b

Please sign in to comment.