Skip to content

Commit

Permalink
Issue 1589: L031: AttributeError: 'NoneType' object has no attribute …
Browse files Browse the repository at this point in the history
…'get_child' (#1595)

Co-authored-by: Barry Hart <barry.hart@mailchimp.com>
Co-authored-by: Alan Cruickshank <alanmcruickshank@gmail.com>
  • Loading branch information
3 people committed Oct 11, 2021
1 parent a9a1893 commit 60d2316
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/sqlfluff/rules/L031.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def _eval(self, segment, **kwargs):
"""
if segment.is_type("select_statement"):
# A buffer for all table expressions in join conditions
table_expression_segments = []
from_expression_elements = []
column_reference_segments = []

from_clause_segment = segment.get_child("from_clause")
Expand Down Expand Up @@ -87,14 +87,14 @@ def _eval(self, segment, **kwargs):
for from_expression_element in clause.recursive_crawl(
"from_expression_element"
):
table_expression_segments.append(from_expression_element)
from_expression_elements.append(from_expression_element)
for column_reference in clause.recursive_crawl("column_reference"):
column_reference_segments.append(column_reference)

return (
self._lint_aliases_in_join(
base_table,
table_expression_segments,
from_expression_elements,
column_reference_segments,
segment,
)
Expand All @@ -112,12 +112,13 @@ class TableAliasInfo(NamedTuple):

@classmethod
def _filter_table_expressions(
cls, base_table, table_expression_segments
cls, base_table, from_expression_elements
) -> Generator[TableAliasInfo, None, None]:
for table_exp in table_expression_segments:
table_ref = table_exp.get_child("table_expression").get_child(
"object_reference"
)
for from_expression in from_expression_elements:
table_expression = from_expression.get_child("table_expression")
if not table_expression:
continue
table_ref = table_expression.get_child("object_reference")

# If the from_expression_element has no object_references - skip it
# An example case is a lateral flatten, where we have a function segment
Expand All @@ -133,10 +134,10 @@ def _filter_table_expressions(
):
continue

whitespace_ref = table_exp.get_child("whitespace")
whitespace_ref = from_expression.get_child("whitespace")

# If there's no alias expression - skip it
alias_exp_ref = table_exp.get_child("alias_expression")
alias_exp_ref = from_expression.get_child("alias_expression")
if alias_exp_ref is None:
continue

Expand All @@ -146,14 +147,14 @@ def _filter_table_expressions(
)

def _lint_aliases_in_join(
self, base_table, table_expression_segments, column_reference_segments, segment
self, base_table, from_expression_elements, column_reference_segments, segment
):
"""Lint and fix all aliases in joins - except for self-joins."""
# A buffer to keep any violations.
violation_buff = []

to_check = list(
self._filter_table_expressions(base_table, table_expression_segments)
self._filter_table_expressions(base_table, from_expression_elements)
)

# How many times does each table appear in the FROM clause?
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/rules/std_rule_cases/L031.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,14 @@ issue_610:
FROM aaaaaa
JOIN bbbbbb AS b ON b.a = aaaaaa.id
JOIN bbbbbb AS b2 ON b2.other = b.id
issue_1589:
pass_str: |
select *
from (select random() as v from (values(1))) t1,
(select max(repl) as m from data) t2,
(select * from data
where repl=t2.m and
rnd>=t.v
order by rnd
limit 1)

0 comments on commit 60d2316

Please sign in to comment.