-
Notifications
You must be signed in to change notification settings - Fork 497
/
get_tables_by_pattern_sql.sql
73 lines (50 loc) · 2.21 KB
/
get_tables_by_pattern_sql.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
{% macro get_tables_by_pattern_sql(schema_pattern, table_pattern, exclude='', database=target.database) %}
{{ return(adapter.dispatch('get_tables_by_pattern_sql', 'dbt_utils')
(schema_pattern, table_pattern, exclude, database)) }}
{% endmacro %}
{% macro default__get_tables_by_pattern_sql(schema_pattern, table_pattern, exclude='', database=target.database) %}
select distinct
table_schema as "table_schema",
table_name as "table_name",
{{ dbt_utils.get_table_types_sql() }}
from {{ database }}.information_schema.tables
where table_schema ilike '{{ schema_pattern }}'
and table_name ilike '{{ table_pattern }}'
and table_name not ilike '{{ exclude }}'
{% endmacro %}
{% macro bigquery__get_tables_by_pattern_sql(schema_pattern, table_pattern, exclude='', database=target.database) %}
{% if '%' in schema_pattern %}
{% set schemata=dbt_utils._bigquery__get_matching_schemata(schema_pattern, database) %}
{% else %}
{% set schemata=[schema_pattern] %}
{% endif %}
{% set sql %}
{% for schema in schemata %}
select distinct
table_schema,
table_name,
case table_type
when 'BASE TABLE' then 'table'
else lower(table_type)
end as table_type
from {{ adapter.quote(database) }}.{{ schema }}.INFORMATION_SCHEMA.TABLES
where lower(table_name) like lower ('{{ table_pattern }}')
and lower(table_name) not like lower ('{{ exclude }}')
{% if not loop.last %} union all {% endif %}
{% endfor %}
{% endset %}
{{ return(sql) }}
{% endmacro %}
{% macro _bigquery__get_matching_schemata(schema_pattern, database) %}
{% if execute %}
{% set sql %}
select schema_name from {{ adapter.quote(database) }}.INFORMATION_SCHEMA.SCHEMATA
where lower(schema_name) like lower('{{ schema_pattern }}')
{% endset %}
{% set results=run_query(sql) %}
{% set schemata=results.columns['schema_name'].values() %}
{{ return(schemata) }}
{% else %}
{{ return([]) }}
{% endif %}
{% endmacro %}