chore(sqllab): Do not strip comments when executing SQL statements (#27725)

(cherry picked from commit 5ed48760fb)
This commit is contained in:
John Bodley 2024-04-03 19:24:39 -07:00 committed by Michael S. Molina
parent 300669f1e7
commit 899cd0eda8
2 changed files with 21 additions and 17 deletions

View File

@ -412,7 +412,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:

View File

@ -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
@ -639,12 +640,13 @@ class TestSqlLab(SupersetTestCase):
mock_get_query,
mock_db,
):
sql = """
sql = dedent(
"""
-- comment
SET @value = 42;
SELECT @value AS foo;
-- comment
SELECT /*+ hint */ @value AS foo;
"""
)
mock_db = mock.MagicMock()
mock_query = mock.MagicMock()
mock_query.database.allow_run_async = False
@ -667,14 +669,14 @@ class TestSqlLab(SupersetTestCase):
mock_execute_sql_statement.assert_has_calls(
[
mock.call(
"SET @value = 42",
"-- comment\nSET @value = 42",
mock_query,
mock_cursor,
None,
False,
),
mock.call(
"SELECT @value AS foo",
"SELECT /*+ hint */ @value AS foo",
mock_query,
mock_cursor,
None,
@ -689,12 +691,13 @@ class TestSqlLab(SupersetTestCase):
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_query = mock.MagicMock()
mock_query.database.allow_run_async = True
mock_cursor = mock.MagicMock()
@ -741,12 +744,13 @@ class TestSqlLab(SupersetTestCase):
mock_get_query,
mock_db,
):
sql = """
sql = dedent(
"""
-- comment
SET @value = 42;
SELECT @value AS foo;
-- comment
SELECT /*+ hint */ @value AS foo;
"""
)
mock_db = mock.MagicMock()
mock_query = mock.MagicMock()
mock_query.database.allow_run_async = False
@ -773,14 +777,14 @@ class TestSqlLab(SupersetTestCase):
mock_execute_sql_statement.assert_has_calls(
[
mock.call(
"SET @value = 42",
"-- comment\nSET @value = 42",
mock_query,
mock_cursor,
None,
False,
),
mock.call(
"SELECT @value AS foo",
"SELECT /*+ hint */ @value AS foo",
mock_query,
mock_cursor,
None,
@ -817,12 +821,13 @@ class TestSqlLab(SupersetTestCase):
# 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,