john-bodley commented on a change in pull request #5295: [sqllab] Fix sqllab
limit regex issue with sqlparse
URL:
https://github.com/apache/incubator-superset/pull/5295#discussion_r200432788
##########
File path: superset/sql_parse.py
##########
@@ -128,3 +134,43 @@ def __extract_from_token(self, token):
for token in item.tokens:
if self.__is_identifier(token):
self.__process_identifier(token)
+
+ def _get_limit_from_token(self, token):
+ if token.ttype == sqlparse.tokens.Literal.Number.Integer:
+ return int(token.value)
+ elif token.is_group:
+ return int(token.get_token_at_offset(1).value)
+
+ def _extract_limit_from_outermost_layer(self, statement):
+ limit_token = None
+ for pos, item in enumerate(statement.tokens):
+ if item.ttype in Keyword and item.value.lower() == 'limit':
+ limit_token = statement.tokens[pos + 2]
+ break
+ if not limit_token:
+ return limit_token
+ return self._get_limit_from_token(limit_token)
+
+ def get_query_with_new_limit(self, new_limit):
+ """returns the query with the specified limit"""
+ """does not change the underlying query"""
+ if not self._limit:
+ return self.sql + ' LIMIT ' + str(new_limit)
+ limit_pos = None
+ # Add all items to before_str until there is a limit
+ for pos, item in enumerate(self._parsed[0].tokens):
+ if item.ttype in Keyword and item.value.lower() == 'limit':
+ limit_pos = pos
+ break
+ limit = self._parsed[0].tokens[limit_pos + 2]
+ if limit.ttype == sqlparse.tokens.Literal.Number.Integer:
+ self._parsed[0].tokens[limit_pos + 2].value = new_limit
+ elif limit.is_group:
+ self._parsed[0].tokens[limit_pos + 2].value = (
+ '{}, {}'.format(next(limit.get_identifiers()), new_limit)
+ )
+ flattened = self._parsed[0].tokens
+ str_res = ''
Review comment:
I think you can simply do `str(flattened)` or `flattened.value`.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]