AttributeError: 'NoneType' object has no attribute 'value'
Hi, I am using this library for extracting columns and tables from a view definition, and when I run it against a view definition, it shows the following error:
AttributeError Traceback (most recent call last)
G:\Anaconda3\envs\py36\lib\site-packages\sql_metadata\parser.py in columns(self) 211 and not token.is_conversion_specifier 212 ): --> 213 self._handle_column_save(token=token, columns=columns) 214 215 elif token.is_column_name_inside_insert_clause:
G:\Anaconda3\envs\py36\lib\site-packages\sql_metadata\parser.py in _handle_column_save(self, token, columns) 629 token.token_type = TokenType.COLUMN_ALIAS 630 return --> 631 column = self._resolve_sub_queries(column) 632 self._add_to_columns_with_tables(token, column) 633 self._add_to_columns_subsection(
G:\Anaconda3\envs\py36\lib\site-packages\sql_metadata\parser.py in _resolve_sub_queries(self, column) 767 subquery_alias=column, 768 nested_queries_names=self.subqueries_names, --> 769 nested_queries=self.subqueries, 770 already_parsed=self._subqueries_parsers, 771 )
G:\Anaconda3\envs\py36\lib\site-packages\sql_metadata\parser.py in subqueries(self) 518 query_name = inner_token.next_token.value 519 else: --> 520 query_name = inner_token.next_token.next_token.value 521 subquery_text = "".join([x.stringified_token for x in current_subquery]) 522 subqueries[query_name] = subquery_text
AttributeError: 'NoneType' object has no attribute 'value'
Here is a snippet of the view definition generated by the database that caused that issue(it is MySQL and can run successfully in a MySQL console):
select t.symbol AS symbol from (stock.top_momentum_sector s join stock.daily_companies t on((s.symbol = t.symbol)))
Thanks a lot in advance
Removing the brackets after 'from' would make it work, like this: 'select t.symbol AS symbol from stock.top_momentum_sector s join stock.daily_companies t on((s.symbol = t.symbol))'
However, I am trying to extract the columns straight from the view_definiton from the database(in bunch too), so I prefer not to touch the original definition as much as possible to avoid complications.
I am also in the process of extracting column names (and more) from some DB2/z SQL. I have to minimally pre-process the SQL to remove additional statements before the SELECT/INSERT/etc (like DECLARE C1 CURSOR FOR ) and change the program variable notation (:H) for something that does not complicate the parsing (e.g. pgm$var). It parsed successfully 36,000 SQL statements and identified 280,000 columns (Kudos to all of you for the great job !!!!).
Unfortunately 50 of them fail with similar error: "'NoneType' object has no attribute 'value'" (all have a sub select) when checking for the existence of the object if parsed.columns:
"sql_metadata/parser.py", line 520, in subqueries query_name = inner_token.next_token.next_token.value".
The 1st attached SQL is probably the shortest (77 tokens) . I hope it helps.
The package version is 2.3.0 running Python 3.8.10 (Linux) sqlparsed.txt
From what I see this also is related to redundant brackets (i.e. in first query you have from ( ( with double brackets.
We will try to fix this, but handling brackets is quite complicated as it can indicate multiple sub clauses (from sub query, through function to column definition).
If you can modify the queries you can try to remove the double brackets as a temporary fix