Break line before LIMIT statement to prevent trailing comment issue (#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 https://github.com/apache/incubator-superset/issues/7483

* fix tests
This commit is contained in:
Maxime Beauchemin 2019-05-13 00:34:34 -05:00 committed by GitHub
parent 4377328e39
commit d8be0a7dd5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 4 deletions

View File

@ -168,7 +168,7 @@ class ParsedQuery(object):
"""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

View File

@ -201,7 +201,8 @@ class CeleryTestCase(SupersetTestCase):
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)

View File

@ -190,7 +190,7 @@ class DbEngineSpecsTestCase(SupersetTestCase):
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):
@ -288,7 +288,7 @@ class DbEngineSpecsTestCase(SupersetTestCase):
'LIMIT 777'""",
"""
SELECT
'LIMIT 777' LIMIT 1000""",
'LIMIT 777'\nLIMIT 1000""",
)
def test_time_grain_blacklist(self):

View File

@ -431,6 +431,25 @@ class SupersetTestCase(unittest.TestCase):
"""
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;