This is an automated email from the ASF dual-hosted git repository.

uranusjr pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 86cfd1244a Fix error when SnowflakeHook take empty list in `sql` param 
(#23767)
86cfd1244a is described below

commit 86cfd1244a641a8f17c9b33a34399d9be264f556
Author: Dmytro Kazanzhy <dkazan...@gmail.com>
AuthorDate: Fri May 20 06:59:25 2022 +0300

    Fix error when SnowflakeHook take empty list in `sql` param (#23767)
---
 airflow/providers/snowflake/hooks/snowflake.py    | 14 +++++++++-----
 tests/providers/snowflake/hooks/test_snowflake.py |  8 ++++++++
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/airflow/providers/snowflake/hooks/snowflake.py 
b/airflow/providers/snowflake/hooks/snowflake.py
index 75ddaa59de..29a4b63156 100644
--- a/airflow/providers/snowflake/hooks/snowflake.py
+++ b/airflow/providers/snowflake/hooks/snowflake.py
@@ -308,14 +308,18 @@ class SnowflakeHook(DbApiHook):
         """
         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]
 
diff --git a/tests/providers/snowflake/hooks/test_snowflake.py 
b/tests/providers/snowflake/hooks/test_snowflake.py
index 74c6fb03e1..97935e66f5 100644
--- a/tests/providers/snowflake/hooks/test_snowflake.py
+++ b/tests/providers/snowflake/hooks/test_snowflake.py
@@ -515,3 +515,11 @@ class TestPytestSnowflakeHook:
             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"

Reply via email to