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_r200434928
##########
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)
Review comment:
Why not have this logic on like #149 and thus there's no need for the
`break`? This also means that `limit_token` only needs to be scoped if there
exists a `limit` keyword. Functions return `None` by default.
```
def _extract_limit_from_outermost_layer(self, statement):
for pos, item in enumerate(statement.tokens):
if item.ttype in Keyword and item.value.lower() == 'limit':
return statement.tokens[pos + 2]
```
----------------------------------------------------------------
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]