Skip to content

Commit ff9423e

Browse files
committed
Fix cancellation. (#173)
### Description `DatabricksSQLConnectionWrapper` should have `cancel` method and handle it properly. Otherwise, the following error will be seen when cancelling the session due to e.g., `dbt test --fail-fast`: ``` AttributeError: 'DatabricksSQLConnectionWrapper' object has no attribute 'cancel' ```
1 parent 2465591 commit ff9423e

5 files changed

Lines changed: 69 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## dbt-databricks 1.1.5 (Release TBD)
22

3+
### Fixes
4+
- Fix cancellation ([#173](https://github.com/databricks/dbt-databricks/pull/173))
5+
36
## dbt-databricks 1.1.4 (September 8, 2022)
47

58
### Fixes

dbt/adapters/databricks/connections.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,22 @@ def __init__(self, conn: DatabricksSQLConnection):
124124
def cursor(self) -> "DatabricksSQLCursorWrapper":
125125
return DatabricksSQLCursorWrapper(self._conn.cursor())
126126

127+
def cancel(self) -> None:
128+
cursors: List[DatabricksSQLCursor] = self._conn._cursors
129+
130+
for cursor in cursors:
131+
try:
132+
cursor.cancel()
133+
except DBSQLError as exc:
134+
logger.debug("Exception while cancelling query: {}".format(exc))
135+
_log_dbsql_errors(exc)
136+
127137
def close(self) -> None:
128-
self._conn.close()
138+
try:
139+
self._conn.close()
140+
except DBSQLError as exc:
141+
logger.debug("Exception while closing connection: {}".format(exc))
142+
_log_dbsql_errors(exc)
129143

130144
def rollback(self, *args: Any, **kwargs: Any) -> None:
131145
logger.debug("NotImplemented: rollback")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: 2
2+
3+
models:
4+
- name: view_model
5+
columns:
6+
- name: id
7+
tests:
8+
- unique
9+
- not_null
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
select 1 as id
2+
union all
3+
select 1 as id
4+
union all
5+
select null as id
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from dbt.exceptions import FailFastException
2+
3+
from tests.integration.base import DBTIntegrationTest, use_profile
4+
5+
6+
class TesFailFast(DBTIntegrationTest):
7+
@property
8+
def schema(self):
9+
return "fail_fast"
10+
11+
@property
12+
def models(self):
13+
return "models"
14+
15+
def test_fail_fast(self):
16+
self.run_dbt(["run"])
17+
18+
with self.assertRaisesRegex(
19+
FailFastException, "Failing early due to test failure or runtime error"
20+
):
21+
self.run_dbt(["test", "--fail-fast"])
22+
23+
@use_profile("databricks_cluster")
24+
def test_fail_fast_databricks_cluster(self):
25+
self.test_fail_fast()
26+
27+
@use_profile("databricks_uc_cluster")
28+
def test_fail_fast_databricks_uc_cluster(self):
29+
self.test_fail_fast()
30+
31+
@use_profile("databricks_sql_endpoint")
32+
def test_fail_fast_databricks_sql_endpoint(self):
33+
self.test_fail_fast()
34+
35+
@use_profile("databricks_uc_sql_endpoint")
36+
def test_fail_fast_databricks_uc_sql_endpoint(self):
37+
self.test_fail_fast()

0 commit comments

Comments
 (0)