Skip to content

Commit c02a04e

Browse files
committed
Handle schema pattern on BQ
1 parent 6a9aa6f commit c02a04e

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

macros/sql/get_tables_by_pattern_sql.sql

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
{% macro default__get_tables_by_pattern_sql(schema_pattern, table_pattern, exclude='', database=target.database) %}
77

8-
select distinct
8+
select distinct
99
table_schema as "table_schema", table_name as "table_name"
1010
from {{database}}.information_schema.tables
1111
where table_schema ilike '{{ schema_pattern }}'
@@ -16,13 +16,51 @@
1616

1717

1818
{% macro bigquery__get_tables_by_pattern_sql(schema_pattern, table_pattern, exclude='', database=target.database) %}
19-
20-
select distinct
21-
table_schema, table_name
2219

23-
from {{adapter.quote(database)}}.{{schema}}.INFORMATION_SCHEMA.TABLES
24-
where table_schema = '{{schema_pattern}}'
25-
and lower(table_name) like lower ('{{table_pattern}}')
26-
and lower(table_name) not like lower ('{{exclude}}')
20+
{% if '%' in schema_pattern %}
21+
{% set schemata=dbt_utils._bigquery__get_matching_schemata(schema_pattern, database) %}
22+
{% else %}
23+
{% set schemata=[schema_pattern] %}
24+
{% endif %}
25+
26+
{% set sql %}
27+
{% for schema in schemata %}
28+
select distinct
29+
table_schema, table_name
30+
31+
from {{ adapter.quote(database) }}.{{ schema }}.INFORMATION_SCHEMA.TABLES
32+
where lower(table_name) like lower ('{{ table_pattern }}')
33+
and lower(table_name) not like lower ('{{ exclude }}')
34+
35+
{% if not loop.last %} union all {% endif %}
36+
37+
{% endfor %}
38+
{% endset %}
39+
40+
{{ return(sql) }}
41+
42+
{% endmacro %}
43+
44+
45+
{% macro _bigquery__get_matching_schemata(schema_pattern, database) %}
46+
{% if execute %}
47+
48+
{% set sql %}
49+
select schema_name from {{ adapter.quote(database) }}.INFORMATION_SCHEMA.SCHEMATA
50+
where lower(schema_name) like lower('{{ schema_pattern }}')
51+
{% endset %}
52+
53+
{% set results=run_query(sql) %}
54+
55+
{% set schemata=results.columns['schema_name'].values() %}
56+
57+
{{ return(schemata) }}
58+
59+
{% else %}
60+
61+
{{ return([]) }}
62+
63+
{% endif %}
64+
2765

2866
{% endmacro %}

0 commit comments

Comments
 (0)