This is an automated email from the ASF dual-hosted git repository.
potiuk 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 13b0930bf4 Fix databricks_sql hook query failing on empty result for
return_tuple (#36827)
13b0930bf4 is described below
commit 13b0930bf45faece076ef1b0c4e6fd14f2b17e16
Author: Jarek Potiuk <[email protected]>
AuthorDate: Wed Jan 17 00:26:36 2024 +0100
Fix databricks_sql hook query failing on empty result for return_tuple
(#36827)
* Fix databricks_sql hook query failing on empty result for return_tuple
The hook was failing with list_index_out_of_range when returned
results were empty list. Also it fixes the retunr_tuple test
that was returning wrong cursor response and checked wrong
hook response
Fixes: #36822
* Update airflow/providers/databricks/hooks/databricks_sql.py
Co-authored-by: Jed Cunningham
<[email protected]>
---------
Co-authored-by: Jed Cunningham
<[email protected]>
---
airflow/providers/databricks/hooks/databricks_sql.py | 2 ++
tests/providers/databricks/hooks/test_databricks_sql.py | 16 ++++++++++++++--
2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/airflow/providers/databricks/hooks/databricks_sql.py
b/airflow/providers/databricks/hooks/databricks_sql.py
index d18d804b04..5ffe404e31 100644
--- a/airflow/providers/databricks/hooks/databricks_sql.py
+++ b/airflow/providers/databricks/hooks/databricks_sql.py
@@ -280,6 +280,8 @@ class DatabricksSqlHook(BaseDatabricksHook, DbApiHook):
# instantiated namedtuple, and will never do:
https://github.com/python/mypy/issues/848
if isinstance(result, list):
rows: list[Row] = result
+ if not rows:
+ return []
rows_fields = rows[0].__fields__
rows_object = namedtuple("Row", rows_fields) # type: ignore[misc]
return cast(List[tuple], [rows_object(*row) for row in rows])
diff --git a/tests/providers/databricks/hooks/test_databricks_sql.py
b/tests/providers/databricks/hooks/test_databricks_sql.py
index a5d85880be..01392fb0a7 100644
--- a/tests/providers/databricks/hooks/test_databricks_sql.py
+++ b/tests/providers/databricks/hooks/test_databricks_sql.py
@@ -200,11 +200,23 @@ SerializableRow = namedtuple("Row", ["id", "value"]) #
type: ignore[name-match]
["select * from test.test"],
True,
[["id", "value"]],
- (Row(id=1, value=2),),
+ ([Row(id=1, value=2)],),
[[("id",), ("value",)]],
- SerializableRow(1, 2),
+ [SerializableRow(1, 2)],
id="The return_last set and no split statements set on single
query in string",
),
+ pytest.param(
+ True,
+ False,
+ "select * from test.test",
+ ["select * from test.test"],
+ True,
+ [["id", "value"]],
+ ([],),
+ [[("id",), ("value",)]],
+ [],
+ id="Empty list",
+ ),
],
)
def test_query(