mirror of
https://github.com/apache/superset.git
synced 2026-04-19 08:04:53 +00:00
[format] Using Black (#7769)
This commit is contained in:
@@ -23,12 +23,10 @@ from sqlparse.sql import Identifier, IdentifierList, remove_quotes, Token, Token
|
||||
from sqlparse.tokens import Keyword, Name, Punctuation, String, Whitespace
|
||||
from sqlparse.utils import imt
|
||||
|
||||
RESULT_OPERATIONS = {'UNION', 'INTERSECT', 'EXCEPT', 'SELECT'}
|
||||
ON_KEYWORD = 'ON'
|
||||
PRECEDES_TABLE_NAME = {
|
||||
'FROM', 'JOIN', 'DESCRIBE', 'WITH', 'LEFT JOIN', 'RIGHT JOIN',
|
||||
}
|
||||
CTE_PREFIX = 'CTE__'
|
||||
RESULT_OPERATIONS = {"UNION", "INTERSECT", "EXCEPT", "SELECT"}
|
||||
ON_KEYWORD = "ON"
|
||||
PRECEDES_TABLE_NAME = {"FROM", "JOIN", "DESCRIBE", "WITH", "LEFT JOIN", "RIGHT JOIN"}
|
||||
CTE_PREFIX = "CTE__"
|
||||
|
||||
|
||||
class ParsedQuery(object):
|
||||
@@ -38,7 +36,7 @@ class ParsedQuery(object):
|
||||
self._alias_names = set()
|
||||
self._limit = None
|
||||
|
||||
logging.info('Parsing with sqlparse statement {}'.format(self.sql))
|
||||
logging.info("Parsing with sqlparse statement {}".format(self.sql))
|
||||
self._parsed = sqlparse.parse(self.stripped())
|
||||
for statement in self._parsed:
|
||||
self.__extract_from_token(statement)
|
||||
@@ -54,24 +52,24 @@ class ParsedQuery(object):
|
||||
return self._limit
|
||||
|
||||
def is_select(self):
|
||||
return self._parsed[0].get_type() == 'SELECT'
|
||||
return self._parsed[0].get_type() == "SELECT"
|
||||
|
||||
def is_explain(self):
|
||||
return self.stripped().upper().startswith('EXPLAIN')
|
||||
return self.stripped().upper().startswith("EXPLAIN")
|
||||
|
||||
def is_readonly(self):
|
||||
"""Pessimistic readonly, 100% sure statement won't mutate anything"""
|
||||
return self.is_select() or self.is_explain()
|
||||
|
||||
def stripped(self):
|
||||
return self.sql.strip(' \t\n;')
|
||||
return self.sql.strip(" \t\n;")
|
||||
|
||||
def get_statements(self):
|
||||
"""Returns a list of SQL statements as strings, stripped"""
|
||||
statements = []
|
||||
for statement in self._parsed:
|
||||
if statement:
|
||||
sql = str(statement).strip(' \n;\t')
|
||||
sql = str(statement).strip(" \n;\t")
|
||||
if sql:
|
||||
statements.append(sql)
|
||||
return statements
|
||||
@@ -98,11 +96,11 @@ class ParsedQuery(object):
|
||||
tokens = tlist.tokens[:idx]
|
||||
|
||||
if (
|
||||
len(tokens) in (1, 3, 5) and
|
||||
all(imt(token, t=[Name, String]) for token in tokens[0::2]) and
|
||||
all(imt(token, m=(Punctuation, '.')) for token in tokens[1::2])
|
||||
len(tokens) in (1, 3, 5)
|
||||
and all(imt(token, t=[Name, String]) for token in tokens[0::2])
|
||||
and all(imt(token, m=(Punctuation, ".")) for token in tokens[1::2])
|
||||
):
|
||||
return '.'.join([remove_quotes(token.value) for token in tokens[0::2]])
|
||||
return ".".join([remove_quotes(token.value) for token in tokens[0::2]])
|
||||
|
||||
return None
|
||||
|
||||
@@ -112,7 +110,7 @@ class ParsedQuery(object):
|
||||
|
||||
def __process_tokenlist(self, tlist: TokenList):
|
||||
# exclude subselects
|
||||
if '(' not in str(tlist):
|
||||
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)
|
||||
@@ -138,15 +136,15 @@ class ParsedQuery(object):
|
||||
:param overwrite, boolean, table table_name will be dropped if true
|
||||
:return: string, create table as query
|
||||
"""
|
||||
exec_sql = ''
|
||||
exec_sql = ""
|
||||
sql = self.stripped()
|
||||
if overwrite:
|
||||
exec_sql = f'DROP TABLE IF EXISTS {table_name};\n'
|
||||
exec_sql += f'CREATE TABLE {table_name} AS \n{sql}'
|
||||
exec_sql = f"DROP TABLE IF EXISTS {table_name};\n"
|
||||
exec_sql += f"CREATE TABLE {table_name} AS \n{sql}"
|
||||
return exec_sql
|
||||
|
||||
def __extract_from_token(self, token, depth=0):
|
||||
if not hasattr(token, 'tokens'):
|
||||
if not hasattr(token, "tokens"):
|
||||
return
|
||||
|
||||
table_name_preceding_token = False
|
||||
@@ -155,11 +153,10 @@ class ParsedQuery(object):
|
||||
if item.is_group and not self.__is_identifier(item):
|
||||
self.__extract_from_token(item, depth=depth + 1)
|
||||
|
||||
if (
|
||||
item.ttype in Keyword and (
|
||||
item.normalized in PRECEDES_TABLE_NAME or
|
||||
item.normalized.endswith(' JOIN')
|
||||
)):
|
||||
if item.ttype in Keyword and (
|
||||
item.normalized in PRECEDES_TABLE_NAME
|
||||
or item.normalized.endswith(" JOIN")
|
||||
):
|
||||
table_name_preceding_token = True
|
||||
continue
|
||||
|
||||
@@ -180,7 +177,7 @@ class ParsedQuery(object):
|
||||
self.__extract_from_token(item, depth=depth + 1)
|
||||
|
||||
def _extract_limit_from_query(self, statement):
|
||||
idx, _ = statement.token_next_by(m=(Keyword, 'LIMIT'))
|
||||
idx, _ = statement.token_next_by(m=(Keyword, "LIMIT"))
|
||||
if idx is not None:
|
||||
_, token = statement.token_next(idx=idx)
|
||||
if token:
|
||||
@@ -193,23 +190,23 @@ class ParsedQuery(object):
|
||||
"""returns the query with the specified limit"""
|
||||
"""does not change the underlying query"""
|
||||
if not self._limit:
|
||||
return f'{self.stripped()}\nLIMIT {new_limit}'
|
||||
return f"{self.stripped()}\nLIMIT {new_limit}"
|
||||
limit_pos = None
|
||||
tokens = self._parsed[0].tokens
|
||||
# Add all items to before_str until there is a limit
|
||||
for pos, item in enumerate(tokens):
|
||||
if item.ttype in Keyword and item.value.lower() == 'limit':
|
||||
if item.ttype in Keyword and item.value.lower() == "limit":
|
||||
limit_pos = pos
|
||||
break
|
||||
limit = tokens[limit_pos + 2]
|
||||
if limit.ttype == sqlparse.tokens.Literal.Number.Integer:
|
||||
tokens[limit_pos + 2].value = new_limit
|
||||
elif limit.is_group:
|
||||
tokens[limit_pos + 2].value = (
|
||||
'{}, {}'.format(next(limit.get_identifiers()), new_limit)
|
||||
tokens[limit_pos + 2].value = "{}, {}".format(
|
||||
next(limit.get_identifiers()), new_limit
|
||||
)
|
||||
|
||||
str_res = ''
|
||||
str_res = ""
|
||||
for i in tokens:
|
||||
str_res += str(i.value)
|
||||
return str_res
|
||||
|
||||
Reference in New Issue
Block a user