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

Patch for dbt show for a model with a struct column containing a json datatype #974

Merged
merged 7 commits into from
Oct 23, 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-20231023-082312.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Patch for json inline --show
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
body: Patch for json inline --show
body: Patch for `dbt show` for `json` within a `struct`

time: 2023-10-23T08:23:12.245223-06:00
custom:
Author: matt-winkler
Issue: "972"
12 changes: 11 additions & 1 deletion dbt/adapters/bigquery/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import google.auth
import google.auth.exceptions
import google.cloud.bigquery
import google.cloud.bigquery as bigquery
import google.cloud.exceptions
from google.api_core import retry, client_info
from google.auth import impersonated_credentials
Expand Down Expand Up @@ -63,6 +63,16 @@
)


# Override broken json deserializer for dbt show --inline
# can remove once this is fixed: https://github.com/googleapis/python-bigquery/issues/1500
def _json_from_json(value, _):
matt-winkler marked this conversation as resolved.
Show resolved Hide resolved
"""NOOP string -> string coercion"""
return json.loads(value)


bigquery._helpers._CELLDATA_FROM_JSON["JSON"] = _json_from_json


@lru_cache()
def get_bigquery_defaults(scopes=None) -> Tuple[Any, Optional[str]]:
"""
Expand Down
37 changes: 37 additions & 0 deletions tests/functional/adapter/dbt_show/test_dbt_show.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,46 @@
import pytest
from dbt.tests.adapter.dbt_show.test_dbt_show import BaseShowSqlHeader, BaseShowLimit

from dbt.tests.util import run_dbt

model_with_json_struct = """
select *
from (
select
struct<
k array<
struct<c1 int64, c2 json>
>
>(
[
struct(
1 as c1,
to_json(struct(1 as a)) as c2
)
]
)
as v
) as model_limit_subq
limit 5
"""


class TestBigQueryShowLimit(BaseShowLimit):
pass


class TestBigQueryShowSqlHeader(BaseShowSqlHeader):
pass


# Added to check if dbt show works with JSON struct
# Addresses: https://github.com/dbt-labs/dbt-bigquery/issues/972
class TestBigQueryShowSqlWorksWithJSONStruct:
dbeatty10 marked this conversation as resolved.
Show resolved Hide resolved
@pytest.fixture(scope="class")
def models(self):
return {
"json_struct_model.sql": model_with_json_struct,
}

def test_sql_header(self, project):
run_dbt(["show", "--select", "json_struct_model"])