Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UnboundLocalError when sql is empty list in DatabricksSqlHook #23815

Merged
merged 1 commit into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"