Skip to content

Commit

Permalink
Merge branch 'main' into feature/materialized-tests/adap-892
Browse files Browse the repository at this point in the history
  • Loading branch information
mikealfare authored Oct 10, 2023
2 parents ccf856f + 5396aa0 commit 412fbc8
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 25 deletions.
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"
6 changes: 6 additions & 0 deletions .changes/unreleased/Under the Hood-20231005-115800.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Under the Hood
body: Support for use of type aliases in contract column data_type
time: 2023-10-05T11:58:00.719136-04:00
custom:
Author: gshank
Issue: "953"
4 changes: 2 additions & 2 deletions .github/workflows/nightly-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defaults:
shell: bash

env:
RELEASE_BRANCH: "1.5.latest" # must test against most recent .latest branch to have parity for dependency with core
RELEASE_BRANCH: "1.6.latest" # must test against most recent .latest branch to have parity for dependency with core

jobs:
aggregate-release-data:
Expand Down Expand Up @@ -75,7 +75,7 @@ jobs:
- name: "Generate Nightly Release Version Number"
id: nightly-release-version
run: |
number="${{ steps.semver.outputs.major }}.${{ steps.semver.outputs.minor }}.${{ steps.bump_patch.outputs.patch }}.dev${{ steps.current-date.outputs.date }}"
number="${{ steps.semver.outputs.major }}.${{ steps.semver.outputs.minor }}.${{ steps.bump_patch.outputs.patch }}+dev${{ steps.current-date.outputs.date }}"
echo "number=$number" >> $GITHUB_OUTPUT
- name: "Audit Nightly Release Version And Parse Into Parts"
Expand Down
5 changes: 1 addition & 4 deletions dbt/adapters/bigquery/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@
@dataclass(init=False)
class BigQueryColumn(Column):
TYPE_LABELS = {
"STRING": "STRING",
"TIMESTAMP": "TIMESTAMP",
"TEXT": "STRING",
"FLOAT": "FLOAT64",
"INTEGER": "INT64",
"BOOLEAN": "BOOLEAN",
"RECORD": "RECORD",
}
fields: List[Self] # type: ignore
mode: str # type: ignore
Expand Down
22 changes: 13 additions & 9 deletions dbt/adapters/bigquery/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,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 @@ -447,15 +459,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
17 changes: 7 additions & 10 deletions tests/functional/adapter/constraints/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

_expected_sql_bigquery = """
create or replace table <model_identifier> (
id integer not null primary key not enforced references <foreign_key_model_identifier> (id) not enforced,
id INT64 not null primary key not enforced references <foreign_key_model_identifier> (id) not enforced,
color string,
date_day string
)
Expand Down Expand Up @@ -79,12 +79,9 @@
"""

# Different on BigQuery:
# - does not support a data type named 'text' (TODO handle this via type translation/aliasing!)
constraints_yml = model_schema_yml.replace("text", "string")
model_constraints_yml = constrained_model_schema_yml.replace("text", "string")
model_contract_header_schema_yml = model_contract_header_schema_yml.replace("text", "string")
model_fk_constraint_schema_yml = model_fk_constraint_schema_yml.replace("text", "string")
constrained_model_schema_yml = constrained_model_schema_yml.replace("text", "string")
# Switch from text to string handled by aliasing
constraints_yml = model_schema_yml
model_constraints_yml = constrained_model_schema_yml

my_model_contract_sql_header_sql = """
{{
Expand Down Expand Up @@ -334,7 +331,7 @@ def models(self):
def expected_sql(self):
return """
create or replace table <model_identifier> (
id integer not null,
id INT64 not null,
color string,
date_day string,
primary key (id) not enforced,
Expand All @@ -361,14 +358,14 @@ class TestBigQueryConstraintQuotedColumn(BaseConstraintQuotedColumn):
def models(self):
return {
"my_model.sql": my_model_with_quoted_column_name_sql,
"constraints_schema.yml": model_quoted_column_schema_yml.replace("text", "string"),
"constraints_schema.yml": model_quoted_column_schema_yml,
}

@pytest.fixture(scope="class")
def expected_sql(self):
return """
create or replace table <model_identifier> (
id integer not null,
id INT64 not null,
`from` string not null,
date_day string
)
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"

0 comments on commit 412fbc8

Please sign in to comment.