Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit f6e26d0

Browse files
authored
ADAP-972: Fix issue where materialized views were being mapped as tables in catalog queries (#996)
* changelog * add test demonstrating issue * update catalog query to correctly identify materialized views
1 parent 2367be7 commit f6e26d0

File tree

4 files changed

+109
-14
lines changed

4 files changed

+109
-14
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: Fixes
2+
body: Assign the correct relation type to materialized views in catalog queries
3+
time: 2023-10-30T22:21:34.401675-04:00
4+
custom:
5+
Author: mikealfare
6+
Issue: "995"

dbt/include/bigquery/macros/catalog.sql

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,42 @@
77
{%- else -%}
88

99
{%- set query -%}
10-
with tables as (
10+
with materialized_views as (
1111
select
12-
project_id as table_database,
13-
dataset_id as table_schema,
14-
table_id as original_table_name,
12+
table_catalog as project_id,
13+
table_schema as dataset_id,
14+
table_name as table_id
15+
from {{ information_schema.replace(information_schema_view='MATERIALIZED_VIEWS') }}
16+
),
17+
tables as (
18+
select
19+
tables.project_id as table_database,
20+
tables.dataset_id as table_schema,
21+
tables.table_id as original_table_name,
1522

16-
concat(project_id, '.', dataset_id, '.', table_id) as relation_id,
23+
concat(tables.project_id, '.', tables.dataset_id, '.', tables.table_id) as relation_id,
1724

18-
row_count,
19-
size_bytes as size_bytes,
25+
tables.row_count,
26+
tables.size_bytes as size_bytes,
2027
case
21-
when type = 1 then 'table'
22-
when type = 2 then 'view'
28+
when materialized_views.table_id is not null then 'materialized view'
29+
when tables.type = 1 then 'table'
30+
when tables.type = 2 then 'view'
2331
else 'external'
2432
end as table_type,
2533

26-
REGEXP_CONTAINS(table_id, '^.+[0-9]{8}$') and coalesce(type, 0) = 1 as is_date_shard,
27-
REGEXP_EXTRACT(table_id, '^(.+)[0-9]{8}$') as shard_base_name,
28-
REGEXP_EXTRACT(table_id, '^.+([0-9]{8})$') as shard_name
34+
REGEXP_CONTAINS(tables.table_id, '^.+[0-9]{8}$') and coalesce(type, 0) = 1 as is_date_shard,
35+
REGEXP_EXTRACT(tables.table_id, '^(.+)[0-9]{8}$') as shard_base_name,
36+
REGEXP_EXTRACT(tables.table_id, '^.+([0-9]{8})$') as shard_name
2937

30-
from {{ information_schema.replace(information_schema_view='__TABLES__') }}
38+
from {{ information_schema.replace(information_schema_view='__TABLES__') }} tables
39+
left join materialized_views
40+
on materialized_views.project_id = tables.project_id
41+
and materialized_views.dataset_id = tables.dataset_id
42+
and materialized_views.table_id = tables.table_id
3143
where (
3244
{%- for schema in schemas -%}
33-
upper(dataset_id) = upper('{{ schema }}'){%- if not loop.last %} or {% endif -%}
45+
upper(tables.dataset_id) = upper('{{ schema }}'){%- if not loop.last %} or {% endif -%}
3446
{%- endfor -%}
3547
)
3648
),
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
MY_SEED = """
2+
id,value,record_valid_date
3+
1,100,2023-01-01 00:00:00
4+
2,200,2023-01-02 00:00:00
5+
3,300,2023-01-02 00:00:00
6+
""".strip()
7+
8+
9+
MY_TABLE = """
10+
{{ config(
11+
materialized='table',
12+
) }}
13+
select *
14+
from {{ ref('my_seed') }}
15+
"""
16+
17+
18+
MY_VIEW = """
19+
{{ config(
20+
materialized='view',
21+
) }}
22+
select *
23+
from {{ ref('my_seed') }}
24+
"""
25+
26+
27+
MY_MATERIALIZED_VIEW = """
28+
{{ config(
29+
materialized='materialized_view',
30+
) }}
31+
select *
32+
from {{ ref('my_table') }}
33+
"""
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from dbt.contracts.results import CatalogArtifact
2+
from dbt.tests.util import run_dbt
3+
import pytest
4+
5+
from tests.functional.adapter.catalog_tests import files
6+
7+
8+
class TestCatalogRelationTypes:
9+
@pytest.fixture(scope="class", autouse=True)
10+
def seeds(self):
11+
return {"my_seed.csv": files.MY_SEED}
12+
13+
@pytest.fixture(scope="class", autouse=True)
14+
def models(self):
15+
yield {
16+
"my_table.sql": files.MY_TABLE,
17+
"my_view.sql": files.MY_VIEW,
18+
"my_materialized_view.sql": files.MY_MATERIALIZED_VIEW,
19+
}
20+
21+
@pytest.fixture(scope="class", autouse=True)
22+
def docs(self, project):
23+
run_dbt(["seed"])
24+
run_dbt(["run"])
25+
yield run_dbt(["docs", "generate"])
26+
27+
@pytest.mark.parametrize(
28+
"node_name,relation_type",
29+
[
30+
("seed.test.my_seed", "table"),
31+
("model.test.my_table", "table"),
32+
("model.test.my_view", "view"),
33+
("model.test.my_materialized_view", "materialized view"),
34+
],
35+
)
36+
def test_relation_types_populate_correctly(
37+
self, docs: CatalogArtifact, node_name: str, relation_type: str
38+
):
39+
"""
40+
This test addresses: https://github.com/dbt-labs/dbt-bigquery/issues/995
41+
"""
42+
assert node_name in docs.nodes
43+
node = docs.nodes[node_name]
44+
assert node.metadata.type == relation_type

0 commit comments

Comments
 (0)