Skip to content

Commit

Permalink
Break line before LIMIT statement to prevent trailing comment issue (a…
Browse files Browse the repository at this point in the history
…pache#7485)

* Break line before LIMIT statement to prevent trailing comment issue

This may not be a perfect solution but it addresses the issue in 7483

closes apache#7483

* fix tests
  • Loading branch information
mistercrunch authored May 13, 2019
1 parent 4377328 commit d8be0a7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion superset/sql_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ 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)
return f'{self.sql}\nLIMIT {new_limit}'
limit_pos = None
tokens = self._parsed[0].tokens
# Add all items to before_str until there is a limit
Expand Down
3 changes: 2 additions & 1 deletion tests/celery_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ def test_run_async_query(self):
self.assertEqual(
'CREATE TABLE tmp_async_1 AS \n'
'SELECT name FROM ab_role '
"WHERE name='Admin' LIMIT 666", query.executed_sql)
"WHERE name='Admin'\n"
'LIMIT 666', query.executed_sql)
self.assertEqual(sql_where, query.sql)
self.assertEqual(0, query.rows)
self.assertEqual(False, query.limit_used)
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 @@ -190,7 +190,7 @@ def test_wrapped_semi_tabs(self):
def test_simple_limit_query(self):
self.sql_limit_regex(
'SELECT * FROM a',
'SELECT * FROM a LIMIT 1000',
'SELECT * FROM a\nLIMIT 1000',
)

def test_modify_limit_query(self):
Expand Down Expand Up @@ -288,7 +288,7 @@ def test_limit_with_non_token_limit(self):
'LIMIT 777'""",
"""
SELECT
'LIMIT 777' LIMIT 1000""",
'LIMIT 777'\nLIMIT 1000""",
)

def test_time_grain_blacklist(self):
Expand Down
19 changes: 19 additions & 0 deletions tests/sql_parse_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,25 @@ def test_complex_cte_with_prefix(self):
"""
self.assertEquals({'SalesOrderHeader'}, self.extract_tables(query))

def test_get_query_with_new_limit_comment(self):
sql = 'SELECT * FROM ab_user --SOME COMMENT'
parsed = sql_parse.ParsedQuery(sql)
newsql = parsed.get_query_with_new_limit(1000)
self.assertEquals(newsql, sql + '\nLIMIT 1000')

def test_get_query_with_new_limit_comment_with_limit(self):
sql = 'SELECT * FROM ab_user --SOME COMMENT WITH LIMIT 555'
parsed = sql_parse.ParsedQuery(sql)
newsql = parsed.get_query_with_new_limit(1000)
self.assertEquals(newsql, sql + '\nLIMIT 1000')

def test_get_query_with_new_limit(self):
sql = 'SELECT * FROM ab_user LIMIT 555'
parsed = sql_parse.ParsedQuery(sql)
newsql = parsed.get_query_with_new_limit(1000)
expected = 'SELECT * FROM ab_user LIMIT 1000'
self.assertEquals(newsql, expected)

def test_basic_breakdown_statements(self):
multi_sql = """
SELECT * FROM ab_user;
Expand Down

0 comments on commit d8be0a7

Please sign in to comment.