Skip to content

Commit

Permalink
fix: downgrade sqlparse and add unit test (apache#10165)
Browse files Browse the repository at this point in the history
* Downgrade sqlparse and add unit test

* Explain why sqlparse is pinned

Co-authored-by: bogdan kyryliuk <[email protected]>
  • Loading branch information
bkyryliuk and bogdan-dbx authored Jul 6, 2020
1 parent 9a5195a commit b7c45fe
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ six==1.14.0 # via bleach, cryptography, flask-jwt-extended, flask-
slackclient==2.6.2 # via apache-superset (setup.py)
sqlalchemy-utils==0.36.6 # via apache-superset (setup.py), flask-appbuilder
sqlalchemy==1.3.16 # via alembic, apache-superset (setup.py), flask-sqlalchemy, marshmallow-sqlalchemy, sqlalchemy-utils
sqlparse==0.3.1 # via apache-superset (setup.py)
sqlparse==0.3.0 # via apache-superset (setup.py)
typing-extensions==3.7.4.2 # via aiohttp
urllib3==1.25.9 # via selenium
vine==1.3.0 # via amqp, celery
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def get_git_sha():
"slackclient>=2.6.2",
"sqlalchemy>=1.3.16, <2.0",
"sqlalchemy-utils>=0.36.6,<0.37",
"sqlparse>=0.3.0, <0.4",
"sqlparse==0.3.0", # PINNED! see https://github.com/andialbrecht/sqlparse/issues/562
"wtforms-json",
],
extras_require={
Expand Down
21 changes: 20 additions & 1 deletion tests/sql_parse_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# under the License.
import unittest

import sqlparse

from superset.sql_parse import ParsedQuery, Table


Expand Down Expand Up @@ -187,7 +189,11 @@ def test_select_if(self):
# SHOW TABLES ((FROM | IN) qualifiedName)? (LIKE pattern=STRING)?
def test_show_tables(self):
query = "SHOW TABLES FROM s1 like '%order%'"
self.assertEqual(set(), self.extract_tables(query))
# TODO: figure out what should code do here
self.assertEqual({Table("s1")}, self.extract_tables(query))
# Expected behavior is below, it is fixed in sqlparse>=3.1
# However sqlparse==3.1 breaks some sql formatting.
# self.assertEqual(set(), self.extract_tables(query))

# SHOW COLUMNS (FROM | IN) qualifiedName
def test_show_columns(self):
Expand Down Expand Up @@ -560,3 +566,16 @@ def test_identifier_list_with_keyword_as_alias(self):
SELECT * FROM match
"""
self.assertEqual({Table("foo")}, self.extract_tables(query))

def test_sqlparse_formatting(self):
# sqlparse 0.3.1 has a bug and removes space between from and from_unixtime while formatting:
# SELECT extract(HOUR\n fromfrom_unixtime(hour_ts)
# AT TIME ZONE 'America/Los_Angeles')\nfrom table
self.assertEqual(
"SELECT extract(HOUR\n from from_unixtime(hour_ts) "
"AT TIME ZONE 'America/Los_Angeles')\nfrom table",
sqlparse.format(
"SELECT extract(HOUR from from_unixtime(hour_ts) AT TIME ZONE 'America/Los_Angeles') from table",
reindent=True,
),
)

0 comments on commit b7c45fe

Please sign in to comment.