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

L031 failure in valid TSQL (Azure Synapse Analytics) #1639

Closed
jpers36 opened this issue Oct 13, 2021 · 1 comment · Fixed by #1643
Closed

L031 failure in valid TSQL (Azure Synapse Analytics) #1639

jpers36 opened this issue Oct 13, 2021 · 1 comment · Fixed by #1643
Labels
bug Something isn't working

Comments

@jpers36
Copy link
Contributor

jpers36 commented Oct 13, 2021

This is as minimal as I can make it:

DECLARE @VariableE date = GETDATE()

CREATE TABLE #TempTable
AS 
(
	Select ColumnD
	from SchemaA.TableB AliasC 
	where ColumnD  >= @VariableE
)

Expected Behaviour

File lints without a Python error.

Observed Behaviour

Receiving the following response:

CRITICAL   [L031] Applying rule L031 threw an Exception: 'NoneType' object has no attribute 'raw'
Traceback (most recent call last):
  File "C:\users\jpersons\source\repos\sqlfluff\src\sqlfluff\core\rules\base.py", line 304, in crawl
    res = self._eval(
  File "C:\users\jpersons\source\repos\sqlfluff\src\sqlfluff\rules\L031.py", line 95, in _eval
    self._lint_aliases_in_join(
  File "C:\users\jpersons\source\repos\sqlfluff\src\sqlfluff\rules\L031.py", line 196, in _lint_aliases_in_join
    if used_alias_ref.raw == alias_name and exp_ref.get_child("dot"):
AttributeError: 'NoneType' object has no attribute 'raw'
== [C:\Users\jpersons\Documents\LinterTest.sql] FAIL
L:   4 | P:   3 | L001 | Unnecessary trailing whitespace.
L:   6 | P:   1 | L004 | Incorrect indentation type found in file.
L:   6 | P:   2 | L010 | Keywords must be consistently upper case.
L:   6 | P:   2 | L031 | Unexpected exception: 'NoneType' object has no attribute
                       | 'raw'; Could you open an issue at
                       | https://github.com/sqlfluff/sqlfluff/issues ? You can
                       | ignore this exception for now, by adding '--noqa: L031'
                       | at the end of line 6
L:   6 | P:   9 | L014 | Unquoted identifiers must be consistently lower case.
L:   7 | P:   1 | L004 | Incorrect indentation type found in file.
L:   7 | P:   2 | L010 | Keywords must be consistently upper case.
L:   7 | P:   7 | L014 | Unquoted identifiers must be consistently lower case.
L:   7 | P:  15 | L014 | Unquoted identifiers must be consistently lower case.
L:   7 | P:  22 | L011 | Implicit/explicit aliasing of table.
L:   7 | P:  22 | L014 | Unquoted identifiers must be consistently lower case.
L:   7 | P:  22 | L025 | Alias 'AliasC' is never used in SELECT statement.
L:   7 | P:  28 | L001 | Unnecessary trailing whitespace.
L:   8 | P:   1 | L004 | Incorrect indentation type found in file.
L:   8 | P:   2 | L010 | Keywords must be consistently upper case.
L:   8 | P:   8 | L014 | Unquoted identifiers must be consistently lower case.
L:   8 | P:  15 | L039 | Unnecessary whitespace found.

Steps to Reproduce

Lint the above TSQL code.

Dialect

TSQL

Version

SQLFluff: Most recent main (d96f0b0)
Python 3.9.6

Configuration

All defaults

@jpers36 jpers36 added the bug Something isn't working label Oct 13, 2021
@tunetheweb
Copy link
Member

Looks to be as simple as this to fix it:

@@ -193,7 +193,7 @@ class Rule_L031(BaseRule):
             for exp_ref in column_reference_segments:
                 used_alias_ref = exp_ref.get_child("identifier")
                 # exp_ref.get_child('dot') ensures that the column reference includes a table reference
-                if used_alias_ref.raw == alias_name and exp_ref.get_child("dot"):
+                if used_alias_ref and used_alias_ref.raw == alias_name and exp_ref.get_child("dot"):
                     ids_refs.append(used_alias_ref)
 
             # Fixes for deleting ` as sth` and for editing references to aliased tables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants