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 error when SnowflakeHook take empty list in sql param #23767

Merged
merged 1 commit into from
May 20, 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
14 changes: 9 additions & 5 deletions airflow/providers/snowflake/hooks/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,18 @@ def run(
"""
self.query_ids = []

if isinstance(sql, str):
split_statements_tuple = split_statements(StringIO(sql))
sql = [sql_string for sql_string, _ in split_statements_tuple if sql_string]

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

with closing(self.get_conn()) as conn:
self.set_autocommit(conn, autocommit)

if isinstance(sql, str):
split_statements_tuple = split_statements(StringIO(sql))
sql = [sql_string for sql_string, _ in split_statements_tuple if sql_string]

self.log.debug("Executing %d statements against Snowflake DB", len(sql))
# SnowflakeCursor does not extend ContextManager, so we have to ignore mypy error here
with closing(conn.cursor(DictCursor)) as cur: # type: ignore[type-var]

Expand Down
8 changes: 8 additions & 0 deletions tests/providers/snowflake/hooks/test_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,11 @@ def test_connection_failure(self, mock_run):
assert status is False
assert msg == 'Connection Errors'
mock_run.assert_called_once_with(sql='select 1')

def test_empty_sql_parameter(self):
hook = SnowflakeHook()

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