Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport] PRs 955 and 957 to 1.6.latest #958

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20231005-235950.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Fix issue where job labels are not rendered when using macro for query comment
time: 2023-10-05T23:59:50.077842+02:00
custom:
Author: kodaho mikealfare
Issue: "863"
22 changes: 13 additions & 9 deletions dbt/adapters/bigquery/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,18 @@ def get_table_from_response(cls, resp):
column_names = [field.name for field in resp.schema]
return agate_helper.table_from_data_flat(resp, column_names)

def get_labels_from_query_comment(cls):
if (
hasattr(cls.profile, "query_comment")
and cls.profile.query_comment
and cls.profile.query_comment.job_label
and cls.query_header
):
query_comment = cls.query_header.comment.query_comment
return cls._labels_from_query_comment(query_comment)

return {}

def raw_execute(
self,
sql,
Expand All @@ -440,15 +452,7 @@ def raw_execute(

fire_event(SQLQuery(conn_name=conn.name, sql=sql, node_info=get_node_info()))

if (
hasattr(self.profile, "query_comment")
and self.profile.query_comment
and self.profile.query_comment.job_label
):
query_comment = self.profile.query_comment
labels = self._labels_from_query_comment(query_comment.comment)
else:
labels = {}
labels = self.get_labels_from_query_comment()

if active_user:
labels["dbt_invocation_id"] = active_user.invocation_id
Expand Down
52 changes: 52 additions & 0 deletions tests/functional/adapter/query_comment_test/test_job_label.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pytest

from google.cloud.bigquery.client import Client

from dbt.tests.util import run_dbt


_MACRO__BQ_LABELS = """
{% macro bq_labels() %}{
"system": "{{ env_var('LABEL_SYSTEM', 'my_system') }}",
"env_type": "{{ env_var('LABEL_ENV', 'dev') }}"
}{% endmacro %}
"""
_MODEL__MY_TABLE = """
{{ config(materialized= "table") }}
select 1 as id
"""


class TestQueryCommentJobLabel:
@pytest.fixture(scope="class")
def models(self):
return {"my_table.sql": _MODEL__MY_TABLE}

@pytest.fixture(scope="class")
def macros(self):
return {"bq_labels.sql": _MACRO__BQ_LABELS}

@pytest.fixture(scope="class")
def project_config_update(self):
return {
"query-comment": {
"comment": "{{ bq_labels() }}",
"job-label": True,
"append": True,
}
}

def test_query_comments_displays_as_job_labels(self, project):
"""
Addresses this regression in dbt-bigquery 1.6:
https://github.com/dbt-labs/dbt-bigquery/issues/863
"""
results = run_dbt(["run"])
job_id = results.results[0].adapter_response.get("job_id")
with project.adapter.connection_named("_test"):
client: Client = project.adapter.connections.get_thread_connection().handle
job = client.get_job(job_id=job_id)

# this is what should happen
assert job.labels.get("system") == "my_system"
assert job.labels.get("env_type") == "dev"