sql-metadata icon indicating copy to clipboard operation
sql-metadata copied to clipboard

error detect with block for bracketed select statement with as for columns

Open lisennku opened this issue 3 years ago • 1 comments

sql_stat = """
with a as (select id, a from tbl1 ),
with b as (select id, b from tbl2)
(
select a.id, a.a + b.b as t 
from a
left join b 
on a.id=b.id
)
"""
tbls = Parser(sql_stat)
tbls.tables

get this result: 'NoneType' object has no attribute 'normalized'

I think the root cause is that the select statment is rounded with brackets and inside the select statement a columns use as keyword, this makes the Parser think it is a with block and the code if token.next_token.is_as_keyword in Parser.with_names get shot.

lisennku avatar Jun 29 '22 03:06 lisennku

I'm getting the same for this case:

Parser("""
        select * from (
            with dummy + 1 as dummyPlusOne
            select dummy, dummyPlusOne from system.one
        )
""").tables

It throws

  File "/home/vitalijus/.pyenv/versions/3.9.6/envs/python-apps/lib/python3.9/site-packages/sql_metadata/parser.py", line 344, in tables
    with_names = self.with_names
  File "/home/vitalijus/.pyenv/versions/3.9.6/envs/python-apps/lib/python3.9/site-packages/sql_metadata/parser.py", line 450, in with_names
    token.next_token_not_comment.normalized
AttributeError: 'NoneType' object has no attribute 'normalized'

vitalvi avatar Jan 17 '23 06:01 vitalvi