Skip to content

feat: expose dbt_task job_id, job_run_id, task_run_id in adapter_response - #1451

Merged
sd-db merged 4 commits into
mainfrom
sd-db/feat/dbt-job-context-in-adapter-response
May 20, 2026
Merged

feat: expose dbt_task job_id, job_run_id, task_run_id in adapter_response#1451
sd-db merged 4 commits into
mainfrom
sd-db/feat/dbt-job-context-in-adapter-response

Conversation

@sd-db

@sd-db sd-db commented May 11, 2026

Copy link
Copy Markdown
Collaborator

Summary

Exposes job_id, job_run_id, and task_run_id on adapter_response for dbt runs executed by a Databricks Jobs dbt_task, so users can join run_results.json to system.lakeflow.{jobs, job_run_timeline, job_task_run_timeline}.

Source: parses DBT_DATABRICKS_HTTP_SESSION_HEADERS, the env var the Jobs runtime sets on the dbt CLI subprocess. Fields are None outside a dbt_task (local CLI, notebook task, etc.) — fully backward-compatible.

Resolves #722
Refs #1376

Verification

  • Unit tests cover env-var absent, empty, valid, missing query-source, malformed JSON/base64. Full unit suite: 771 passed.
  • Ran dbt_task submissions on a live workspace to verify.
    • Single-task run: extracted the three IDs from the resulting run_results.json, then queried system.lakeflow.job_task_run_timeline and system.lakeflow.job_run_timeline for that job_id. The task table's run_id matched our task_run_id, its job_run_id matched ours, its job_id matched. The parent table's run_id matched our job_run_id. Names line up with the join columns.
    • Multi-task run: three sibling dbt_tasks in one submission, no depends_on. Two ran dbt run-operation dump_ids (a macro that just log()s the raw DBT_DATABRICKS_HTTP_SESSION_HEADERS); the third ran dbt run. All three tasks reported the same job_run_id and three different task_run_ids, each matching that task's own run_id in the jobs API response as well as the values in system tables.
  • Local dbt run: outside a dbt_task writes a run_results.json with the three fields stripped (dbt-core drops None dataclass fields) — no log pollution.

…onse

Adds three optional fields to the dbt-databricks adapter response so that
dbt runs executed by a Databricks Jobs `dbt_task` can be correlated with
their originating workflow execution via run_results.json.

Source: parse the DBT_DATABRICKS_HTTP_SESSION_HEADERS env var set by the
Jobs runtime on the dbt CLI subprocess. The header JSON carries the IDs
directly (X-Databricks-Dbsql-Job-Id, X-Databricks-Dbsql-Run-Id) plus a
base64-encoded query source containing job_run_id.

Fields join cleanly to system tables:
  - job_id      -> system.lakeflow.jobs.job_id
  - job_run_id  -> system.lakeflow.job_run_timeline.run_id
  - task_run_id -> system.lakeflow.job_task_run_timeline.run_id

All three are None when dbt runs outside a dbt_task (local CLI, notebook
task, other task types); the new response type stays a backward-compatible
subclass of AdapterResponse.

Resolves #722
Refs #1376
@github-actions

github-actions Bot commented May 11, 2026

Copy link
Copy Markdown

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  dbt/adapters/databricks
  connections.py 545-548
  handle.py
Project Total  

This report was generated by python-coverage-comment-action

@sd-db sd-db changed the title feat: expose dbt_task job_id, job_run_id, task_run_id in adapter_response feat: expose dbt_task job_id, job_run_id, task_run_id in adapter_response May 11, 2026
@sd-db

sd-db commented May 11, 2026

Copy link
Copy Markdown
Collaborator Author

/integration-test

@github-actions

Copy link
Copy Markdown

Integration tests dispatched for PR #1451 by @sd-db. Track progress in the Actions tab.

@sd-db sd-db self-assigned this May 11, 2026
sd-db added 2 commits May 11, 2026 19:12
- handle.py: trim _DBT_TASK_HEADERS_ENV comment to one line; tighten
  DatabricksAdapterResponse docstring (drop per-field system-table mapping).
- test_handle.py: drop assertion on internal _message field; remove three
  low-value tests (no_query_id duplicate, is_adapter_response_subclass,
  str_uses_base_message).
json.loads on the DBT_DATABRICKS_HTTP_SESSION_HEADERS env var (or on the
base64-decoded X-Databricks-Sql-Query-Source value) accepts any valid JSON
— including null, [], 123, "foo", true — and the previous code would
raise AttributeError when calling .get() on those non-dict results. Add
isinstance(..., dict) guards at both levels and parametrized regression
tests covering each non-object shape.
@github-actions

Copy link
Copy Markdown

Integration results for PR #1451 — UC cluster ✅ success · SQL warehouse ✅ success · All-purpose cluster ✅ success · Shard coverage ✅ success

Run details.

@sd-db
sd-db merged commit 7be7649 into main May 20, 2026
7 checks passed
@sd-db
sd-db deleted the sd-db/feat/dbt-job-context-in-adapter-response branch May 20, 2026 05:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Include job_id, run_id and task_id in adapter_response in the results on-run-end context variable

2 participants