Skip to content

Commit

Permalink
Fix UnboundLocalError when sql is empty list in DatabricksSqlHook (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
kazanzhy authored May 22, 2022
1 parent 4b5a101 commit cf5a78e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion airflow/providers/databricks/hooks/databricks_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ def run(self, sql: Union[str, List[str]], autocommit=True, parameters=None, hand
"""
if isinstance(sql, str):
sql = self.maybe_split_sql_string(sql)
self.log.debug("Executing %d statements", len(sql))

if sql:
self.log.debug("Executing %d statements", len(sql))
else:
raise ValueError("List of SQL statements is empty")

conn = None
for sql_statement in sql:
Expand Down
8 changes: 8 additions & 0 deletions tests/providers/databricks/hooks/test_databricks_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import unittest
from unittest import mock

import pytest

from airflow.models import Connection
from airflow.providers.databricks.hooks.databricks_sql import DatabricksSqlHook
from airflow.utils.session import provide_session
Expand Down Expand Up @@ -82,3 +84,9 @@ def test_query(self, mock_requests, mock_conn):

cur.execute.assert_has_calls([mock.call(q) for q in [query]])
cur.close.assert_called()

def test_no_query(self):
for empty_statement in ([], '', '\n'):
with pytest.raises(ValueError) as err:
self.hook.run(sql=empty_statement)
assert err.value.args[0] == "List of SQL statements is empty"

0 comments on commit cf5a78e

Please sign in to comment.