Skip to content

Commit

Permalink
dbt-materialize: exclude subsources from introspection (#20501)
Browse files Browse the repository at this point in the history
Fixes #20483.
  • Loading branch information
morsapaes committed Jul 13, 2023
1 parent a8dba2c commit 5d7efdf
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 8 deletions.
11 changes: 11 additions & 0 deletions misc/dbt-materialize/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# dbt-materialize Changelog

## Unreleased

* Fix a bug in the `materialize__list_relations_without_caching` macro which
could cause the adapter to break for multi-output sources ([#20483](https://github.com/MaterializeInc/materialize/issues/20483)).

* **Breaking change.** Expose `owner` in the dbt documentation, now that
Materialize supports [role-based access control (RBAC)](https://materialize.com/docs/manage/access-control/).

This change requires [Materialize >=0.48.0](https://materialize.com/docs/releases/v0.48/).
**Users of older versions should pin `dbt-materialize` to `v1.4.1`.**

## 1.4.1 - 2023-04-28

* Let Materialize automatically run introspection queries in the
Expand Down
10 changes: 8 additions & 2 deletions misc/dbt-materialize/dbt/include/materialize/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,17 @@
d.name as database,
s.name as schema,
o.name,
case when o.type = 'materialized-view' then 'materializedview' else o.type end as type
case when o.type = 'materialized-view' then 'materializedview'
else o.type
end as type
from mz_objects o
left join mz_sources so on o.id = so.id
join mz_schemas s on o.schema_id = s.id and s.name = '{{ schema_relation.schema }}'
join mz_databases d on s.database_id = d.id and d.name = '{{ schema_relation.database }}'
where type in ('table', 'source', 'view', 'materialized-view', 'index', 'sink')
where o.type in ('table', 'source', 'view', 'materialized-view', 'index', 'sink')
--Exclude subsources and progress subsources, which aren't relevant in this
--context and can bork the adapter (see #20483)
and coalesce(so.type, '') not in ('subsource', 'progress')
{% endcall %}
{{ return(load_result('list_relations_without_caching').table) }}
{% endmacro %}
11 changes: 9 additions & 2 deletions misc/dbt-materialize/dbt/include/materialize/macros/catalog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,21 @@
d.name as table_database,
s.name as table_schema,
o.name as table_name,
case when o.type = 'materialized-view' then 'materializedview' else o.type end as table_type,
case when o.type = 'materialized-view' then 'materializedview'
--This macro is used for the dbt documentation. We use
--the source type in mz_sources here instead of that
--in mz_objects to correctly report subsources.
when o.type = 'source' then so.type
else o.type end as table_type,
'' as table_comment,
c.name as column_name,
c.position as column_index,
c.type as column_type,
'' as column_comment,
'' as table_owner
r.name as table_owner
from mz_objects o
left join mz_sources so on o.id = so.id
join mz_roles r on o.owner_id = r.id
join mz_schemas s on o.schema_id = s.id
join mz_databases d on s.database_id = d.id and d.name = '{{ database }}'
join mz_columns c on c.id = o.id
Expand Down
14 changes: 13 additions & 1 deletion misc/dbt-materialize/tests/adapter/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
{{ config(
materialized='source',
database='materialize',
pre_hook="CREATE CONNECTION kafka_connection TO KAFKA (BROKER '{{ env_var('KAFKA_ADDR', 'localhost:9092') }}')"
pre_hook="CREATE CONNECTION IF NOT EXISTS kafka_connection TO KAFKA (BROKER '{{ env_var('KAFKA_ADDR', 'localhost:9092') }}')"
)
}}
Expand All @@ -63,6 +63,18 @@
FORMAT BYTES
"""

test_subsources = """
{{ config(
materialized='source',
database='materialize'
)
}}
CREATE SOURCE {{ this }}
FROM LOAD GENERATOR AUCTION
FOR ALL TABLES;
"""

test_sink = """
{{ config(materialized='sink') }}
CREATE SINK {{ this }}
Expand Down
4 changes: 2 additions & 2 deletions misc/dbt-materialize/tests/adapter/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class TestDocsGenerateMaterialize(BaseDocsGenerate):
def expected_catalog(self, project, profile_user):
return base_expected_catalog(
project,
role=None,
role="materialize",
id_type="integer",
text_type="text",
time_type="timestamp",
Expand All @@ -183,7 +183,7 @@ class TestDocsGenReferencesMaterialize(BaseDocsGenReferences):
def expected_catalog(self, project, profile_user):
return expected_references_catalog(
project,
role=None,
role="materialize",
id_type="integer",
text_type="text",
time_type="timestamp",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
test_sink,
test_source,
test_source_index,
test_subsources,
test_view_index,
)

Expand All @@ -48,6 +49,7 @@ def models(self):
"test_relation_name_loooooooooooooooooonger_than_postgres_63_limit.sql": test_relation_name_length,
"test_source.sql": test_source,
"test_source_index.sql": test_source_index,
"test_subsources.sql": test_subsources,
"test_sink.sql": test_sink,
"actual_indexes.sql": actual_indexes,
}
Expand All @@ -60,7 +62,12 @@ def test_custom_materializations(self, project):
# run models
results = run_dbt(["run"])
# run result length
assert len(results) == 8
assert len(results) == 9
# re-run models to ensure there are no lingering errors in recreating
# the materializations
results = run_dbt(["run"])
# re-run result length
assert len(results) == 9
# relations_equal
check_relations_equal(
project.adapter, ["test_materialized_view", "test_view_index"]
Expand Down

0 comments on commit 5d7efdf

Please sign in to comment.