Skip to content

Commit

Permalink
fix(mssql): avoid trying to return a resultset for DML queries with n…
Browse files Browse the repository at this point in the history
…ot resultset (#24999)

(cherry picked from commit 66eabc2)
  • Loading branch information
Yuval-Moshe authored and michael-s-molina committed Aug 21, 2023
1 parent 994fd23 commit 34bc86a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions superset/db_engine_specs/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ def convert_dttm(
def fetch_data(
cls, cursor: Any, limit: Optional[int] = None
) -> list[tuple[Any, ...]]:
if not cursor.description:
return []
data = super().fetch_data(cursor, limit)
# Lists of `pyodbc.Row` need to be unpacked further
return cls.pyodbc_rows_to_tuples(data)
Expand Down
1 change: 1 addition & 0 deletions superset/sql_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ def tables(self) -> set[Table]:
def limit(self) -> Optional[int]:
return self._limit

# pylint: disable=no-self-use
def _get_cte_tables(self, parsed: dict[str, Any]) -> list[dict[str, Any]]:
if "with" not in parsed:
return []
Expand Down
11 changes: 10 additions & 1 deletion tests/unit_tests/db_engine_specs/test_mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ def test_extract_error_message() -> None:
assert expected_message == error_message


def test_fetch_data_no_description() -> None:
from superset.db_engine_specs.mssql import MssqlEngineSpec

cursor = mock.MagicMock()
cursor.description = []
assert MssqlEngineSpec.fetch_data(cursor) == []


def test_fetch_data() -> None:
from superset.db_engine_specs.base import BaseEngineSpec
from superset.db_engine_specs.mssql import MssqlEngineSpec
Expand All @@ -166,9 +174,10 @@ def test_fetch_data() -> None:
"pyodbc_rows_to_tuples",
return_value="converted",
) as mock_pyodbc_rows_to_tuples:
cursor = mock.MagicMock()
data = [(1, "foo")]
with mock.patch.object(BaseEngineSpec, "fetch_data", return_value=data):
result = MssqlEngineSpec.fetch_data(None, 0)
result = MssqlEngineSpec.fetch_data(cursor, 0)
mock_pyodbc_rows_to_tuples.assert_called_once_with(data)
assert result == "converted"

Expand Down

0 comments on commit 34bc86a

Please sign in to comment.