From e3629df11f3195f0c467f17764ae70008a86daaf Mon Sep 17 00:00:00 2001 From: GuyEshdat Date: Tue, 31 Mar 2026 12:12:28 +0300 Subject: [PATCH 1/2] feat: Add invocation_id to default query comment Add invocation_id to the DATABRICKS_QUERY_COMMENT template so that all queries from a single dbt invocation can be correlated in Databricks SQL Query History. The invocation_id is a UUID unique to each dbt command invocation and is already available in the query-comment compilation context. Resolves #1377 --- dbt/adapters/databricks/connections.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dbt/adapters/databricks/connections.py b/dbt/adapters/databricks/connections.py index 291ac7677..3cee9b521 100644 --- a/dbt/adapters/databricks/connections.py +++ b/dbt/adapters/databricks/connections.py @@ -64,6 +64,7 @@ databricks_sql_connector_version='{dbsql_version}', profile_name=target.get('profile_name'), target_name=target.get('target_name'), + invocation_id=invocation_id, ) -%}} {{%- if node is not none -%}} {{%- do comment_dict.update( From 4c433b33e4198b7a2e1f2ff2fc638bd9269b86d8 Mon Sep 17 00:00:00 2001 From: Shubham Dhal Date: Sun, 26 Apr 2026 19:44:06 +0530 Subject: [PATCH 2/2] test: cover invocation_id in default query comment + CHANGELOG entry Co-authored-by: Isaac --- CHANGELOG.md | 4 ++++ tests/unit/test_query_comment.py | 40 ++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 tests/unit/test_query_comment.py diff --git a/CHANGELOG.md b/CHANGELOG.md index aaba349e0..1a4ce80a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## dbt-databricks 1.11.8 (TBD) +### Features + +- Add `invocation_id` to the default query comment ([#1377](https://github.com/databricks/dbt-databricks/issues/1377)) + ### Fixes - Validate relation identifier length at creation time and raise a clear error when it exceeds Databricks' 255-character limit ([#1309](https://github.com/databricks/dbt-databricks/issues/1309)) diff --git a/tests/unit/test_query_comment.py b/tests/unit/test_query_comment.py new file mode 100644 index 000000000..dd75bd40f --- /dev/null +++ b/tests/unit/test_query_comment.py @@ -0,0 +1,40 @@ +import json + +import jinja2 + +from dbt.adapters.databricks.connections import DATABRICKS_QUERY_COMMENT + + +def _render_default_query_comment(*, node, connection_name="connection"): + captured = {} + + def _return(value): + captured["value"] = value + return "" + + env = jinja2.Environment(extensions=["jinja2.ext.do"]) + env.globals["tojson"] = json.dumps + template = env.from_string(DATABRICKS_QUERY_COMMENT) + template.render( + dbt_version="1.11.0", + target={"profile_name": "my_profile", "target_name": "dev"}, + invocation_id="abc-123-uuid", + node=node, + connection_name=connection_name, + **{"return": _return}, + ) + return json.loads(captured["value"]) + + +class TestDatabricksQueryComment: + def test_includes_invocation_id_with_node(self): + node = type("Node", (), {"unique_id": "model.proj.my_model"})() + comment = _render_default_query_comment(node=node) + assert comment["invocation_id"] == "abc-123-uuid" + assert comment["node_id"] == "model.proj.my_model" + + def test_includes_invocation_id_without_node(self): + comment = _render_default_query_comment(node=None, connection_name="setup") + assert comment["invocation_id"] == "abc-123-uuid" + assert "node_id" not in comment + assert comment["connection_name"] == "setup"