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 bug where L034 should ignore INSERT or "CREATE TABLE AS SELECT" with CTE #4108

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/sqlfluff/core/templaters/slicers/tracer.py
Expand Up @@ -76,7 +76,7 @@ def trace(self, append_to_templated: str = "") -> JinjaTrace:
trace_template = self.make_template(trace_template_str)
trace_template_output = trace_template.render()
# Split output by section. Each section has two possible formats.
trace_entries = list(regex.finditer(r"\0", trace_template_output)) # type: ignore[call-overload] # noqa: E501
trace_entries = list(regex.finditer(r"\0", trace_template_output))
for match_idx, match in enumerate(trace_entries):
pos1 = match.span()[0]
try:
Expand Down
14 changes: 14 additions & 0 deletions src/sqlfluff/rules/L034.py
Expand Up @@ -94,10 +94,24 @@ def _eval(self, context: RuleContext) -> EvalResultType:
"insert_statement", "set_expression"
):
return None
if (
len(context.parent_stack) >= 3
and context.parent_stack[-3].is_type("insert_statement", "set_expression")
Copy link
Member

Choose a reason for hiding this comment

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

Should you have a set_expression test case?

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 tried to create one, but couldn't get it to parse.

and context.parent_stack[-2].is_type("with_compound_statement")
):
return None
if len(context.parent_stack) >= 3 and context.parent_stack[-3].is_type(
"create_table_statement", "merge_statement"
):
return None
if (
len(context.parent_stack) >= 4
and context.parent_stack[-4].is_type(
"create_table_statement", "merge_statement"
tunetheweb marked this conversation as resolved.
Show resolved Hide resolved
)
and context.parent_stack[-2].is_type("with_compound_statement")
):
return None

select_clause_segment = context.segment
select_target_elements = context.segment.get_children("select_clause_element")
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/rules/std_rule_cases/L034.yml
Expand Up @@ -121,6 +121,13 @@ test_insert_statements_ignored:
FROM
another_schema.another_table

test_insert_statement_with_cte_ignored:
pass_str: |
INSERT INTO my_table
WITH my_cte AS (SELECT * FROM t1)
SELECT MAX(field1), field2
FROM t1

test_merge_statements_ignored:
pass_str: |
MERGE INTO t
Expand Down Expand Up @@ -152,6 +159,14 @@ test_create_table_as_select_statements_ignored:
another_schema.another_table
)

test_create_table_as_select_with_cte_ignored:
pass_str: |
CREATE TABLE new_table AS (
WITH my_cte AS (SELECT * FROM t1)
SELECT MAX(field1), field2
FROM t1
)

test_fail_fix_explicit_column_references_1:
fail_str: |
SELECT
Expand Down