Skip to content

Commit

Permalink
cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
OTooleMichael committed Mar 3, 2022
1 parent 5b3b083 commit ef74a8d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 48 deletions.
1 change: 0 additions & 1 deletion src/sqlfluff/core/default_config.cfg
Expand Up @@ -104,7 +104,6 @@ force_enable = False
# References must be consistently used
# Disabled for some dialects (e.g. bigquery)
force_enable = False
fix_inconsistent_to = None

[sqlfluff:rules:L029]
# Keywords should not be used as identifiers.
Expand Down
4 changes: 0 additions & 4 deletions src/sqlfluff/core/rules/config_info.py
Expand Up @@ -42,10 +42,6 @@
"validation": ["consistent", "qualified", "unqualified"],
"definition": "The expectation for references in single-table select.",
},
"fix_inconsistent_to": {
"validation": [None, "qualified", "unqualified"],
"definition": "Fixing behaviour for inconsistent single-table select.",
},
"force_enable": {
"validation": [True, False],
"definition": (
Expand Down
44 changes: 30 additions & 14 deletions src/sqlfluff/rules/L028.py
Expand Up @@ -6,11 +6,15 @@
from sqlfluff.core.parser.segments.base import BaseSegment
from sqlfluff.core.parser.segments.raw import CodeSegment, SymbolSegment
from sqlfluff.core.rules.base import LintFix, LintResult, EvalResultType, RuleContext
from sqlfluff.core.rules.doc_decorators import document_configuration
from sqlfluff.core.rules.doc_decorators import (
document_configuration,
document_fix_compatible,
)
from sqlfluff.rules.L020 import Rule_L020


@document_configuration
@document_fix_compatible
class Rule_L028(Rule_L020):
"""References should be consistent in statements with a single table.
Expand All @@ -19,6 +23,8 @@ class Rule_L028(Rule_L020):
structs which trigger false positives. It can be enabled with the
``force_enable = True`` flag.
"consistent" will be fixed to "qualified" if inconsistency is found.
**Anti-pattern**
In this example, only the field ``b`` is referenced.
Expand Down Expand Up @@ -53,9 +59,14 @@ class Rule_L028(Rule_L020):
config_keywords = [
"single_table_references",
"force_enable",
"fix_inconsistent_to",
]
_allow_select_alias = False
# Lateral references are when we reference a column just created in the select above
# This can be in a WHERE clause or any column expression further on than the def.
# https://aws.amazon.com/about-aws/whats-new/2018/08/amazon-redshift-announces-support-for-lateral-column-alias-reference
_allow_lateral_reference = False
_dialects_allowing_lateral_reference = ["snowflake", "redshift"]
_is_struct_dialect = False
_dialects_with_structs = ["bigquery", "hive", "redshift"]

def _lint_references_and_aliases(
self,
Expand All @@ -68,16 +79,17 @@ def _lint_references_and_aliases(
):
"""Iterate through references and check consistency."""
self.single_table_references: str
self.fix_inconsistent_to: Optional[str]
# This could be turned into an option
fix_inconsistent_to = "qualified"
return _generate_fixes(
table_aliases,
standalone_aliases,
references,
col_aliases,
self.single_table_references,
self._allow_select_alias,
self.dialect,
self.fix_inconsistent_to,
self._allow_lateral_reference,
self._is_struct_dialect,
fix_inconsistent_to,
)

def _eval(self, context: RuleContext) -> EvalResultType:
Expand All @@ -88,14 +100,17 @@ def _eval(self, context: RuleContext) -> EvalResultType:
# Some dialects use structs (e.g. column.field) which look like
# table references and so incorrectly trigger this rule.
if (
context.dialect.name in ["bigquery", "hive", "redshift"]
context.dialect.name in self._dialects_with_structs
and not self.force_enable
):
return LintResult()

# Certain dialects allow use of SELECT alias in WHERE clauses
if context.dialect.name in ["snowflake", "redshift"]:
self._allow_select_alias = True
# See comment above (by prop definition)
if context.dialect.name in self._dialects_allowing_lateral_reference:
self._allow_lateral_reference = True

if context.dialect.name in self._dialects_with_structs:
self._is_struct_dialect = True

return super()._eval(context=context)

Expand All @@ -107,7 +122,7 @@ def _generate_fixes(
col_aliases: List[ColumnAliasInfo],
single_table_references: str,
allow_select_alias: bool,
dialect: str,
is_struct_dialect: bool,
fix_inconsistent_to: Optional[str],
) -> Optional[List[LintResult]]:
"""Iterate through references and check consistency."""
Expand All @@ -122,7 +137,7 @@ def _generate_fixes(
seen_ref_types: Set[str] = set()
for ref in references:
this_ref_type: str = ref.qualification() # type: ignore
if this_ref_type == "qualified" and dialect in ["bigquery", "hive", "redshift"]:
if this_ref_type == "qualified" and is_struct_dialect:
# If this col appears "qualified" check if it is more logically a struct.
if next(ref.iter_raw_references()).part != table_ref_str: # type: ignore
this_ref_type = "unqualified"
Expand Down Expand Up @@ -150,9 +165,10 @@ def _generate_fixes(
standalone_aliases,
references,
col_aliases,
# NB vars are pssed in a different order here
single_table_references=fix_inconsistent_to,
allow_select_alias=allow_select_alias,
dialect=dialect,
is_struct_dialect=is_struct_dialect,
fix_inconsistent_to=None,
)

Expand Down
40 changes: 11 additions & 29 deletions test/fixtures/rules/std_rule_cases/L028.yml
Expand Up @@ -3,22 +3,7 @@ rule: L028
# Mixed qualification of references.
test_fail_single_table_mixed_qualification_of_references:
fail_str: SELECT my_tbl.bar, baz FROM my_tbl

test_fail_mixed_qualification_fix_to_unqualified:
fail_str: SELECT my_tbl.bar, baz FROM my_tbl
fix_str: SELECT bar, baz FROM my_tbl
configs:
rules:
L028:
fix_inconsistent_to: unqualified

test_fail_mixed_qualification_fix_to_qualified:
fail_str: SELECT my_tbl.bar, baz FROM "my_tbl"
fix_str: SELECT my_tbl.bar, "my_tbl".baz FROM "my_tbl"
configs:
rules:
L028:
fix_inconsistent_to: qualified
fix_str: SELECT my_tbl.bar, my_tbl.baz FROM my_tbl

test_pass_single_table_consistent_references_1:
pass_str: SELECT bar FROM my_tbl
Expand All @@ -28,6 +13,7 @@ test_pass_single_table_consistent_references_2:

test_fail_single_table_mixed_qualification_of_references_subquery:
fail_str: SELECT * FROM (SELECT my_tbl.bar, baz FROM my_tbl)
fix_str: SELECT * FROM (SELECT my_tbl.bar, my_tbl.baz FROM my_tbl)

test_pass_single_table_consistent_references_1_subquery:
pass_str: SELECT * FROM (SELECT bar FROM my_tbl)
Expand Down Expand Up @@ -75,7 +61,9 @@ test_value_table_functions_do_not_require_qualification:

test_object_references_1a:
# This should fail as "a" is an unreference object
# We dont try to be smart.
fail_str: SELECT a.bar, b FROM my_tbl
fix_str: SELECT a.bar, my_tbl.b FROM my_tbl

test_object_references_1b:
# This should not-fail as "a" is potentially a STRUCT
Expand Down Expand Up @@ -118,25 +106,13 @@ test_object_references_1e:

test_object_references_struct_inconsistent_fix_a:
fail_str: SELECT a.bar, my_tbl.b FROM my_tbl
fix_str: SELECT a.bar, b FROM my_tbl
configs:
core:
dialect: bigquery
rules:
L028:
force_enable: true
fix_inconsistent_to: unqualified

test_object_references_struct_inconsistent_fix_b:
fail_str: SELECT my_tbl.a.bar, b FROM my_tbl
fix_str: SELECT a.bar, b FROM my_tbl
fix_str: SELECT my_tbl.a.bar, my_tbl.b FROM my_tbl
configs:
core:
dialect: bigquery
rules:
L028:
force_enable: true
fix_inconsistent_to: unqualified

test_object_references_1f:
# This should not-fail as "a" is potentially a STRUCT
Expand Down Expand Up @@ -218,6 +194,12 @@ test_select_alias_in_where_clause_3:
t.col1 + 1 as alias_col1
from table1 as t
where alias_col1 > 5
fix_str: |
select
t.col0,
t.col1 + 1 as alias_col1
from table1 as t
where t.alias_col1 > 5
test_select_alias_in_where_clause_4:
# This should fail for ansi
Expand Down

0 comments on commit ef74a8d

Please sign in to comment.