Skip to content

Commit

Permalink
[bugfix] Correctly quote table and schema in select_star (#8181)
Browse files Browse the repository at this point in the history
* Fix select_star table quoting bug

* Add unit test for fully qualified names in select_star

* Rename main_db to db

* Rename test function
  • Loading branch information
villebro authored Sep 5, 2019
1 parent 8f071e8 commit 4e1e54b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def select_star(self):
# show_cols and latest_partition set to false to avoid
# the expensive cost of inspecting the DB
return self.database.select_star(
self.name, show_cols=False, latest_partition=False
self.table_name, schema=self.schema, show_cols=False, latest_partition=False
)

def get_col(self, col_name):
Expand Down
28 changes: 25 additions & 3 deletions tests/model_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ def test_database_impersonate_user(self):
self.assertNotEquals(example_user, user_name)

def test_select_star(self):
main_db = get_example_database()
db = get_example_database()
table_name = "energy_usage"
sql = main_db.select_star(table_name, show_cols=False, latest_partition=False)
sql = db.select_star(table_name, show_cols=False, latest_partition=False)
expected = textwrap.dedent(
f"""\
SELECT *
Expand All @@ -115,7 +115,7 @@ def test_select_star(self):
)
assert sql.startswith(expected)

sql = main_db.select_star(table_name, show_cols=True, latest_partition=False)
sql = db.select_star(table_name, show_cols=True, latest_partition=False)
expected = textwrap.dedent(
f"""\
SELECT source,
Expand All @@ -126,6 +126,28 @@ def test_select_star(self):
)
assert sql.startswith(expected)

def test_select_star_fully_qualified_names(self):
db = get_example_database()
schema = "schema.name"
table_name = "table/name"
sql = db.select_star(
table_name, schema=schema, show_cols=False, latest_partition=False
)
fully_qualified_names = {
"sqlite": '"schema.name"."table/name"',
"mysql": "`schema.name`.`table/name`",
"postgres": '"schema.name"."table/name"',
}
fully_qualified_name = fully_qualified_names.get(db.db_engine_spec.engine)
if fully_qualified_name:
expected = textwrap.dedent(
f"""\
SELECT *
FROM {fully_qualified_name}
LIMIT 100"""
)
assert sql.startswith(expected)

def test_single_statement(self):
main_db = get_main_database()

Expand Down

0 comments on commit 4e1e54b

Please sign in to comment.