-
Notifications
You must be signed in to change notification settings - Fork 14k
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
[fix] Fixing SQL parsing issue #7374
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
import logging | ||
|
||
import sqlparse | ||
from sqlparse.sql import Identifier, IdentifierList | ||
from sqlparse.sql import Identifier, IdentifierList, Token, TokenList | ||
from sqlparse.tokens import Keyword, Name | ||
|
||
RESULT_OPERATIONS = {'UNION', 'INTERSECT', 'EXCEPT', 'SELECT'} | ||
|
@@ -75,32 +75,32 @@ def get_statements(self): | |
return statements | ||
|
||
@staticmethod | ||
def __get_full_name(identifier): | ||
if len(identifier.tokens) > 2 and identifier.tokens[1].value == '.': | ||
return '{}.{}'.format(identifier.tokens[0].value, | ||
identifier.tokens[2].value) | ||
return identifier.get_real_name() | ||
def __get_full_name(tlist: TokenList): | ||
if len(tlist.tokens) > 2 and tlist.tokens[1].value == '.': | ||
return '{}.{}'.format(tlist.tokens[0].value, | ||
tlist.tokens[2].value) | ||
return tlist.get_real_name() | ||
|
||
@staticmethod | ||
def __is_identifier(token): | ||
def __is_identifier(token: Token): | ||
return isinstance(token, (IdentifierList, Identifier)) | ||
|
||
def __process_identifier(self, identifier): | ||
def __process_tokenlist(self, tlist: TokenList): | ||
# exclude subselects | ||
if '(' not in str(identifier): | ||
table_name = self.__get_full_name(identifier) | ||
if '(' not in str(tlist): | ||
table_name = self.__get_full_name(tlist) | ||
if table_name and not table_name.startswith(CTE_PREFIX): | ||
self._table_names.add(table_name) | ||
return | ||
|
||
# store aliases | ||
if hasattr(identifier, 'get_alias'): | ||
self._alias_names.add(identifier.get_alias()) | ||
if hasattr(identifier, 'tokens'): | ||
# some aliases are not parsed properly | ||
if identifier.tokens[0].ttype == Name: | ||
self._alias_names.add(identifier.tokens[0].value) | ||
self.__extract_from_token(identifier) | ||
if tlist.has_alias(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By construction there's no need to check whether the |
||
self._alias_names.add(tlist.get_alias()) | ||
|
||
# some aliases are not parsed properly | ||
if tlist.tokens[0].ttype == Name: | ||
self._alias_names.add(tlist.tokens[0].value) | ||
self.__extract_from_token(tlist) | ||
|
||
def as_create_table(self, table_name, overwrite=False): | ||
"""Reformats the query into the create table as query. | ||
|
@@ -144,10 +144,11 @@ def __extract_from_token(self, token, depth=0): | |
|
||
if table_name_preceding_token: | ||
if isinstance(item, Identifier): | ||
self.__process_identifier(item) | ||
self.__process_tokenlist(item) | ||
elif isinstance(item, IdentifierList): | ||
for token in item.get_identifiers(): | ||
self.__process_identifier(token) | ||
if isinstance(token, TokenList): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no guarantee that the "identifiers" associated withe |
||
self.__process_tokenlist(token) | ||
elif isinstance(item, IdentifierList): | ||
for token in item.tokens: | ||
if not self.__is_identifier(token): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling this
identifiers
is somewhat misleading as in reality it's processingTokenList
objects.