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

Allow no alias for selects in CTEs with a column list #3580

Merged
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: 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