Skip to content

Commit

Permalink
L028, L032: Fix bug where fixes were copying templated table names (s…
Browse files Browse the repository at this point in the history
…qlfluff#3699)

* L028, L032: Fix bug where fixes were copying templated table names

* Fix unrelated Black formatting issue

* Fix crash when source is None

* PR review: add new test case
  • Loading branch information
barrywhart authored Jul 31, 2022
1 parent 7f0180e commit 213dd62
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/sqlfluff/rules/L028.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def _check_references(
# A buffer to keep any violations.
col_alias_names: List[str] = [c.alias_identifier_name for c in col_aliases]
table_ref_str: str = table_aliases[0].ref_str
table_ref_str_source = table_aliases[0].segment
# Check all the references that we have.
seen_ref_types: Set[str] = set()
for ref in references:
Expand All @@ -168,6 +169,7 @@ def _check_references(
this_ref_type,
standalone_aliases,
table_ref_str,
table_ref_str_source,
col_alias_names,
seen_ref_types,
fixable,
Expand Down Expand Up @@ -201,6 +203,7 @@ def _validate_one_reference(
this_ref_type: str,
standalone_aliases: List[str],
table_ref_str: str,
table_ref_str_source: Optional[BaseSegment],
col_alias_names: List[str],
seen_ref_types: Set[str],
fixable: bool,
Expand Down Expand Up @@ -253,6 +256,7 @@ def _validate_one_reference(
fixes = [
LintFix.create_before(
ref.segments[0] if len(ref.segments) else ref,
source=[table_ref_str_source] if table_ref_str_source else None,
edit_segments=[
CodeSegment(
raw=table_ref_str,
Expand Down
3 changes: 3 additions & 0 deletions src/sqlfluff/rules/L032.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,12 @@ def _eval(self, context: RuleContext) -> Optional[LintResult]:
_extract_cols_from_using(segment, using_anchor),
)

assert table_a.segment
assert table_b.segment
fixes = [
LintFix.create_before(
anchor_segment=insert_after_anchor,
source=[table_a.segment, table_b.segment],
edit_segments=edit_segments,
),
*[LintFix.delete(seg) for seg in to_delete],
Expand Down
19 changes: 18 additions & 1 deletion test/fixtures/rules/std_rule_cases/L028.yml
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ test_pass_snowflake_flatten_function:
core:
dialect: snowflake


passes_tql_table_variable:
# Issue 3243
pass_str: select a, b from @tablevar
Expand All @@ -303,3 +302,21 @@ passes_tql_table_variable:
rules:
L028:
single_table_references: qualified

fail_but_dont_fix_templated_table_name_consistent:
fail_str: |
SELECT
a,
{{ "foo" }}.b
FROM {{ "foo" }}
fail_but_dont_fix_templated_table_name_qualified:
fail_str: |
SELECT
a,
{{ "foo" }}.b
FROM {{ "foo" }}
configs:
rules:
L028:
single_table_references: qualified
9 changes: 9 additions & 0 deletions test/fixtures/rules/std_rule_cases/L032.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,12 @@ test_fail_parent_child_positioning:
fix_str: |
select * from c1 join c2 ON c1.ID = c2.ID
join (select * from c3 join c4 ON c3.ID = c4.ID) as c5 on c1.ID = c5.ID
fail_but_dont_fix_templated_table_names:
fail_str: |
SELECT
{{ "table_a" }}.field_1,
table_b.field_2
FROM
{{ "table_a" }}
INNER JOIN table_b USING (id)

0 comments on commit 213dd62

Please sign in to comment.