|
1 | 1 | {% macro fabric__get_catalog(information_schemas, schemas) -%}
|
| 2 | + {% set query_label = apply_label() %} |
| 3 | + {%- call statement('catalog', fetch_result=True) -%} |
2 | 4 |
|
3 |
| - {%- call statement('catalog', fetch_result=True) -%} |
4 |
| - |
5 |
| - with |
6 |
| - principals as ( |
7 |
| - select |
8 |
| - name as principal_name, |
9 |
| - principal_id as principal_id |
10 |
| - from |
11 |
| - sys.database_principals {{ information_schema_hints() }} |
12 |
| - ), |
13 |
| - |
14 |
| - schemas as ( |
15 |
| - select |
16 |
| - name as schema_name, |
17 |
| - schema_id as schema_id, |
18 |
| - principal_id as principal_id |
19 |
| - from |
20 |
| - sys.schemas {{ information_schema_hints() }} |
21 |
| - ), |
22 |
| - |
23 |
| - tables as ( |
24 |
| - select |
25 |
| - object_id, |
26 |
| - name as table_name, |
27 |
| - schema_id as schema_id, |
28 |
| - principal_id as principal_id, |
29 |
| - 'BASE TABLE' as table_type |
30 |
| - from |
31 |
| - sys.tables {{ information_schema_hints() }} |
32 |
| - ), |
33 |
| - |
34 |
| - tables_with_metadata as ( |
35 |
| - select |
36 |
| - object_id, |
37 |
| - table_name, |
38 |
| - schema_name, |
39 |
| - coalesce(tables.principal_id, schemas.principal_id) as owner_principal_id, |
40 |
| - table_type |
41 |
| - from |
42 |
| - tables |
43 |
| - join schemas on tables.schema_id = schemas.schema_id |
44 |
| - ), |
45 |
| - |
46 |
| - views as ( |
47 |
| - select |
48 |
| - object_id, |
49 |
| - name as table_name, |
50 |
| - schema_id as schema_id, |
51 |
| - principal_id as principal_id, |
52 |
| - 'VIEW' as table_type |
53 |
| - from |
54 |
| - sys.views {{ information_schema_hints() }} |
55 |
| - ), |
56 |
| - |
57 |
| - views_with_metadata as ( |
58 |
| - select |
59 |
| - object_id, |
60 |
| - table_name, |
61 |
| - schema_name, |
62 |
| - coalesce(views.principal_id, schemas.principal_id) as owner_principal_id, |
63 |
| - table_type |
64 |
| - from |
65 |
| - views |
66 |
| - join schemas on views.schema_id = schemas.schema_id |
67 |
| - ), |
68 |
| - |
69 |
| - tables_and_views as ( |
70 |
| - select |
71 |
| - object_id, |
72 |
| - table_name, |
73 |
| - schema_name, |
74 |
| - principal_name, |
75 |
| - table_type |
76 |
| - from |
77 |
| - tables_with_metadata |
78 |
| - join principals on tables_with_metadata.owner_principal_id = principals.principal_id |
79 |
| - union all |
80 |
| - select |
81 |
| - object_id, |
82 |
| - table_name, |
83 |
| - schema_name, |
84 |
| - principal_name, |
85 |
| - table_type |
86 |
| - from |
87 |
| - views_with_metadata |
88 |
| - join principals on views_with_metadata.owner_principal_id = principals.principal_id |
89 |
| - ), |
90 |
| - |
91 |
| - cols as ( |
| 5 | + with |
| 6 | + principals as ( |
| 7 | + select |
| 8 | + name as principal_name, |
| 9 | + principal_id as principal_id |
| 10 | + from |
| 11 | + sys.database_principals {{ information_schema_hints() }} |
| 12 | + ), |
| 13 | + |
| 14 | + schemas as ( |
| 15 | + select |
| 16 | + name as schema_name, |
| 17 | + schema_id as schema_id, |
| 18 | + principal_id as principal_id |
| 19 | + from |
| 20 | + sys.schemas {{ information_schema_hints() }} |
| 21 | + ), |
| 22 | + |
| 23 | + tables as ( |
| 24 | + select |
| 25 | + object_id, |
| 26 | + name as table_name, |
| 27 | + schema_id as schema_id, |
| 28 | + principal_id as principal_id, |
| 29 | + 'BASE TABLE' as table_type |
| 30 | + from |
| 31 | + sys.tables {{ information_schema_hints() }} |
| 32 | + ), |
| 33 | + |
| 34 | + tables_with_metadata as ( |
| 35 | + select |
| 36 | + object_id, |
| 37 | + table_name, |
| 38 | + schema_name, |
| 39 | + coalesce(tables.principal_id, schemas.principal_id) as owner_principal_id, |
| 40 | + table_type |
| 41 | + from |
| 42 | + tables |
| 43 | + join schemas on tables.schema_id = schemas.schema_id |
| 44 | + ), |
| 45 | + |
| 46 | + views as ( |
| 47 | + select |
| 48 | + object_id, |
| 49 | + name as table_name, |
| 50 | + schema_id as schema_id, |
| 51 | + principal_id as principal_id, |
| 52 | + 'VIEW' as table_type |
| 53 | + from |
| 54 | + sys.views {{ information_schema_hints() }} |
| 55 | + ), |
| 56 | + |
| 57 | + views_with_metadata as ( |
| 58 | + select |
| 59 | + object_id, |
| 60 | + table_name, |
| 61 | + schema_name, |
| 62 | + coalesce(views.principal_id, schemas.principal_id) as owner_principal_id, |
| 63 | + table_type |
| 64 | + from |
| 65 | + views |
| 66 | + join schemas on views.schema_id = schemas.schema_id |
| 67 | + ), |
| 68 | + |
| 69 | + tables_and_views as ( |
| 70 | + select |
| 71 | + object_id, |
| 72 | + table_name, |
| 73 | + schema_name, |
| 74 | + principal_name, |
| 75 | + table_type |
| 76 | + from |
| 77 | + tables_with_metadata |
| 78 | + join principals on tables_with_metadata.owner_principal_id = principals.principal_id |
| 79 | + union all |
| 80 | + select |
| 81 | + object_id, |
| 82 | + table_name, |
| 83 | + schema_name, |
| 84 | + principal_name, |
| 85 | + table_type |
| 86 | + from |
| 87 | + views_with_metadata |
| 88 | + join principals on views_with_metadata.owner_principal_id = principals.principal_id |
| 89 | + ), |
| 90 | + |
| 91 | + cols as ( |
| 92 | + |
| 93 | + select |
| 94 | + c.object_id, |
| 95 | + c.name as column_name, |
| 96 | + c.column_id as column_index, |
| 97 | + t.name as column_type |
| 98 | + from sys.columns as c {{ information_schema_hints() }} |
| 99 | + left join sys.types as t on c.system_type_id = t.system_type_id {{ information_schema_hints() }} |
| 100 | + ) |
92 | 101 |
|
93 | 102 | select
|
94 |
| - c.object_id, |
95 |
| - c.name as column_name, |
96 |
| - c.column_id as column_index, |
97 |
| - t.name as column_type |
98 |
| - from sys.columns as c {{ information_schema_hints() }} |
99 |
| - left join sys.types as t on c.system_type_id = t.system_type_id {{ information_schema_hints() }} |
100 |
| - ) |
101 |
| - |
102 |
| - select |
103 |
| - DB_NAME() as table_database, |
104 |
| - tv.schema_name as table_schema, |
105 |
| - tv.table_name, |
106 |
| - tv.table_type, |
107 |
| - null as table_comment, |
108 |
| - tv.principal_name as table_owner, |
109 |
| - cols.column_name, |
110 |
| - cols.column_index, |
111 |
| - cols.column_type, |
112 |
| - null as column_comment |
113 |
| - from tables_and_views tv |
114 |
| - join cols on tv.object_id = cols.object_id |
115 |
| - where ({%- for schema in schemas -%} |
116 |
| - upper(tv.schema_name) = upper('{{ schema }}'){%- if not loop.last %} or {% endif -%} |
117 |
| - {%- endfor -%}) |
118 |
| - |
119 |
| - order by column_index |
120 |
| - |
121 |
| - {%- endcall -%} |
122 |
| - |
123 |
| - {{ return(load_result('catalog').table) }} |
| 103 | + DB_NAME() as table_database, |
| 104 | + tv.schema_name as table_schema, |
| 105 | + tv.table_name, |
| 106 | + tv.table_type, |
| 107 | + null as table_comment, |
| 108 | + tv.principal_name as table_owner, |
| 109 | + cols.column_name, |
| 110 | + cols.column_index, |
| 111 | + cols.column_type, |
| 112 | + null as column_comment |
| 113 | + from tables_and_views tv |
| 114 | + join cols on tv.object_id = cols.object_id |
| 115 | + where ({%- for schema in schemas -%} |
| 116 | + upper(tv.schema_name) = upper('{{ schema }}'){%- if not loop.last %} or {% endif -%} |
| 117 | + {%- endfor -%}) |
| 118 | + |
| 119 | + order by column_index |
| 120 | + {{ query_label }} |
| 121 | + |
| 122 | + {%- endcall -%} |
| 123 | + |
| 124 | + {{ return(load_result('catalog').table) }} |
124 | 125 |
|
125 | 126 | {%- endmacro %}
|
126 | 127 |
|
127 | 128 | {% macro fabric__get_catalog_relations(information_schema, relations) -%}
|
128 |
| - |
| 129 | + {% set query_label = apply_label() %} |
129 | 130 | {%- call statement('catalog', fetch_result=True) -%}
|
130 | 131 |
|
131 | 132 | with
|
|
260 | 261 | )
|
261 | 262 |
|
262 | 263 | order by column_index
|
263 |
| - |
| 264 | + {{ query_label }} |
264 | 265 | {%- endcall -%}
|
265 | 266 |
|
266 | 267 | {{ return(load_result('catalog').table) }}
|
|
0 commit comments