Skip to content
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

[sqllab] Fix limit parsing bug when using limit-offset comma notation #7912

Merged
merged 3 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion superset/sql_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ def _extract_limit_from_query(self, statement):
_, token = statement.token_next(idx=idx)
if token:
if isinstance(token, IdentifierList):
_, token = token.token_next(idx=-1)
# In case of "LIMIT <offset>, <limit>", find comma and extract
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@villebro this seems good. I just checked the source code to verify that the IdentifierList contains the , punctuation and thus idx will not be None.

# first succeeding non-whitespace token
idx, _ = token.token_next_by(m=(sqlparse.tokens.Punctuation, ","))
_, token = token.token_next(idx=idx)
if token and token.ttype == sqlparse.tokens.Literal.Number.Integer:
return int(token.value)

Expand Down
4 changes: 2 additions & 2 deletions tests/db_engine_specs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ def test_extract_limit_from_query(self, engine_spec_class=MySQLEngineSpec):
q2 = "select * from (select * from my_subquery limit 10) where col=1 limit 20"
q3 = "select * from (select * from my_subquery limit 10);"
q4 = "select * from (select * from my_subquery limit 10) where col=1 limit 20;"
q5 = "select * from mytable limit 10, 20"
q5 = "select * from mytable limit 20, 10"
q6 = "select * from mytable limit 10 offset 20"
q7 = "select * from mytable limit"
q8 = "select * from mytable limit 10.0"
q9 = "select * from mytable limit x"
q10 = "select * from mytable limit x, 20"
q10 = "select * from mytable limit 20, x"
q11 = "select * from mytable limit x offset 20"

self.assertEqual(engine_spec_class.get_limit_from_sql(q0), None)
Expand Down