Skip to content

Commit

Permalink
chore(sqllab): Do not strip comments when executing SQL statements (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
john-bodley authored and michael-s-molina committed Apr 4, 2024
1 parent 863461d commit 51d8863
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
1 change: 0 additions & 1 deletion superset/sql_lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,6 @@ def execute_sql_statements(
# Breaking down into multiple statements
parsed_query = ParsedQuery(
rendered_query,
strip_comments=True,
engine=db_engine_spec.engine,
)
if not db_engine_spec.run_multiple_statements_as_one:
Expand Down
14 changes: 7 additions & 7 deletions tests/integration_tests/databases/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ def test_get_invalid_table_table_metadata(self):
"indexes": [],
"name": "wrong_table",
"primaryKey": {"constrained_columns": None, "name": None},
"selectStar": "SELECT\n *\nFROM wrong_table\nLIMIT 100\nOFFSET 0",
"selectStar": "SELECT *\nFROM wrong_table\nLIMIT 100\nOFFSET 0",
},
)
elif example_db.backend == "mysql":
Expand Down Expand Up @@ -2293,9 +2293,9 @@ def test_import_database_masked_password(self):
uri = "api/v1/database/import/"

masked_database_config = database_config.copy()
masked_database_config[
"sqlalchemy_uri"
] = "postgresql://username:XXXXXXXXXX@host:12345/db"
masked_database_config["sqlalchemy_uri"] = (
"postgresql://username:XXXXXXXXXX@host:12345/db"
)

buf = BytesIO()
with ZipFile(buf, "w") as bundle:
Expand Down Expand Up @@ -2350,9 +2350,9 @@ def test_import_database_masked_password_provided(self):
uri = "api/v1/database/import/"

masked_database_config = database_config.copy()
masked_database_config[
"sqlalchemy_uri"
] = "vertica+vertica_python://hackathon:XXXXXXXXXX@host:5433/dbname?ssl=1"
masked_database_config["sqlalchemy_uri"] = (
"vertica+vertica_python://hackathon:XXXXXXXXXX@host:5433/dbname?ssl=1"
)

buf = BytesIO()
with ZipFile(buf, "w") as bundle:
Expand Down
37 changes: 21 additions & 16 deletions tests/integration_tests/sqllab_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""Unit tests for Sql Lab"""
import json
from datetime import datetime
from textwrap import dedent

import pytest
from celery.exceptions import SoftTimeLimitExceeded
Expand Down Expand Up @@ -578,12 +579,13 @@ def test_sql_json_parameter_forbidden(self):
@mock.patch("superset.sql_lab.get_query")
@mock.patch("superset.sql_lab.execute_sql_statement")
def test_execute_sql_statements(self, mock_execute_sql_statement, mock_get_query):
sql = """
sql = dedent(
"""
-- comment
SET @value = 42;
SELECT @value AS foo;
-- comment
SELECT /*+ hint */ @value AS foo;
"""
)
mock_session = mock.MagicMock()
mock_query = mock.MagicMock()
mock_query.database.allow_run_async = False
Expand All @@ -607,15 +609,15 @@ def test_execute_sql_statements(self, mock_execute_sql_statement, mock_get_query
mock_execute_sql_statement.assert_has_calls(
[
mock.call(
"SET @value = 42",
"-- comment\nSET @value = 42",
mock_query,
mock_session,
mock_cursor,
None,
False,
),
mock.call(
"SELECT @value AS foo",
"SELECT /*+ hint */ @value AS foo",
mock_query,
mock_session,
mock_cursor,
Expand All @@ -631,12 +633,13 @@ def test_execute_sql_statements(self, mock_execute_sql_statement, mock_get_query
def test_execute_sql_statements_no_results_backend(
self, mock_execute_sql_statement, mock_get_query
):
sql = """
sql = dedent(
"""
-- comment
SET @value = 42;
SELECT @value AS foo;
-- comment
SELECT /*+ hint */ @value AS foo;
"""
)
mock_session = mock.MagicMock()
mock_query = mock.MagicMock()
mock_query.database.allow_run_async = True
Expand Down Expand Up @@ -681,12 +684,13 @@ def test_execute_sql_statements_no_results_backend(
def test_execute_sql_statements_ctas(
self, mock_execute_sql_statement, mock_get_query
):
sql = """
sql = dedent(
"""
-- comment
SET @value = 42;
SELECT @value AS foo;
-- comment
SELECT /*+ hint */ @value AS foo;
"""
)
mock_session = mock.MagicMock()
mock_query = mock.MagicMock()
mock_query.database.allow_run_async = False
Expand Down Expand Up @@ -714,15 +718,15 @@ def test_execute_sql_statements_ctas(
mock_execute_sql_statement.assert_has_calls(
[
mock.call(
"SET @value = 42",
"-- comment\nSET @value = 42",
mock_query,
mock_session,
mock_cursor,
None,
False,
),
mock.call(
"SELECT @value AS foo",
"SELECT /*+ hint */ @value AS foo",
mock_query,
mock_session,
mock_cursor,
Expand Down Expand Up @@ -761,12 +765,13 @@ def test_execute_sql_statements_ctas(

# try invalid CVAS
mock_query.ctas_method = CtasMethod.VIEW
sql = """
sql = dedent(
"""
-- comment
SET @value = 42;
SELECT @value AS foo;
-- comment
SELECT /*+ hint */ @value AS foo;
"""
)
with pytest.raises(SupersetErrorException) as excinfo:
execute_sql_statements(
query_id=1,
Expand Down

0 comments on commit 51d8863

Please sign in to comment.