|
1 | 1 | {% macro fabric__get_catalog(information_schemas, schemas) -%} |
| 2 | + |
2 | 3 | {% set query_label = apply_label() %} |
3 | 4 | {%- call statement('catalog', fetch_result=True) -%} |
4 | 5 |
|
|
126 | 127 | {%- endmacro %} |
127 | 128 |
|
128 | 129 | {% macro fabric__get_catalog_relations(information_schema, relations) -%} |
| 130 | + |
129 | 131 | {% set query_label = apply_label() %} |
130 | | - {%- call statement('catalog', fetch_result=True) -%} |
| 132 | + {%- set distinct_databases = relations | map(attribute='database') | unique | list -%} |
131 | 133 |
|
132 | | - with |
133 | | - principals as ( |
134 | | - select |
135 | | - name as principal_name, |
136 | | - principal_id as principal_id |
137 | | - from |
138 | | - sys.database_principals {{ information_schema_hints() }} |
139 | | - ), |
| 134 | + {%- if distinct_databases | length == 1 -%} |
| 135 | + {%- call statement('catalog', fetch_result=True) -%} |
| 136 | + {{ get_use_database_sql(distinct_databases[0]) }} |
| 137 | + with |
| 138 | + principals as ( |
| 139 | + select |
| 140 | + name as principal_name, |
| 141 | + principal_id as principal_id |
| 142 | + from |
| 143 | + sys.database_principals {{ information_schema_hints() }} |
| 144 | + ), |
140 | 145 |
|
141 | | - schemas as ( |
142 | | - select |
143 | | - name as schema_name, |
144 | | - schema_id as schema_id, |
145 | | - principal_id as principal_id |
146 | | - from |
147 | | - sys.schemas {{ information_schema_hints() }} |
148 | | - ), |
| 146 | + schemas as ( |
| 147 | + select |
| 148 | + name as schema_name, |
| 149 | + schema_id as schema_id, |
| 150 | + principal_id as principal_id |
| 151 | + from |
| 152 | + sys.schemas {{ information_schema_hints() }} |
| 153 | + ), |
149 | 154 |
|
150 | | - tables as ( |
151 | | - select |
152 | | - object_id, |
153 | | - name as table_name, |
154 | | - schema_id as schema_id, |
155 | | - principal_id as principal_id, |
156 | | - 'BASE TABLE' as table_type |
157 | | - from |
158 | | - sys.tables {{ information_schema_hints() }} |
159 | | - ), |
| 155 | + tables as ( |
| 156 | + select |
| 157 | + object_id, |
| 158 | + name as table_name, |
| 159 | + schema_id as schema_id, |
| 160 | + principal_id as principal_id, |
| 161 | + 'BASE TABLE' as table_type |
| 162 | + from |
| 163 | + sys.tables {{ information_schema_hints() }} |
| 164 | + ), |
160 | 165 |
|
161 | | - tables_with_metadata as ( |
162 | | - select |
163 | | - object_id, |
164 | | - table_name, |
165 | | - schema_name, |
166 | | - coalesce(tables.principal_id, schemas.principal_id) as owner_principal_id, |
167 | | - table_type |
168 | | - from |
169 | | - tables |
170 | | - join schemas on tables.schema_id = schemas.schema_id |
171 | | - ), |
| 166 | + tables_with_metadata as ( |
| 167 | + select |
| 168 | + object_id, |
| 169 | + table_name, |
| 170 | + schema_name, |
| 171 | + coalesce(tables.principal_id, schemas.principal_id) as owner_principal_id, |
| 172 | + table_type |
| 173 | + from |
| 174 | + tables |
| 175 | + join schemas on tables.schema_id = schemas.schema_id |
| 176 | + ), |
172 | 177 |
|
173 | | - views as ( |
174 | | - select |
175 | | - object_id, |
176 | | - name as table_name, |
177 | | - schema_id as schema_id, |
178 | | - principal_id as principal_id, |
179 | | - 'VIEW' as table_type |
180 | | - from |
181 | | - sys.views {{ information_schema_hints() }} |
182 | | - ), |
| 178 | + views as ( |
| 179 | + select |
| 180 | + object_id, |
| 181 | + name as table_name, |
| 182 | + schema_id as schema_id, |
| 183 | + principal_id as principal_id, |
| 184 | + 'VIEW' as table_type |
| 185 | + from |
| 186 | + sys.views {{ information_schema_hints() }} |
| 187 | + ), |
183 | 188 |
|
184 | | - views_with_metadata as ( |
185 | | - select |
186 | | - object_id, |
187 | | - table_name, |
188 | | - schema_name, |
189 | | - coalesce(views.principal_id, schemas.principal_id) as owner_principal_id, |
190 | | - table_type |
191 | | - from |
192 | | - views |
193 | | - join schemas on views.schema_id = schemas.schema_id |
194 | | - ), |
| 189 | + views_with_metadata as ( |
| 190 | + select |
| 191 | + object_id, |
| 192 | + table_name, |
| 193 | + schema_name, |
| 194 | + coalesce(views.principal_id, schemas.principal_id) as owner_principal_id, |
| 195 | + table_type |
| 196 | + from |
| 197 | + views |
| 198 | + join schemas on views.schema_id = schemas.schema_id |
| 199 | + ), |
195 | 200 |
|
196 | | - tables_and_views as ( |
197 | | - select |
198 | | - object_id, |
199 | | - table_name, |
200 | | - schema_name, |
201 | | - principal_name, |
202 | | - table_type |
203 | | - from |
204 | | - tables_with_metadata |
205 | | - join principals on tables_with_metadata.owner_principal_id = principals.principal_id |
206 | | - union all |
207 | | - select |
208 | | - object_id, |
209 | | - table_name, |
210 | | - schema_name, |
211 | | - principal_name, |
212 | | - table_type |
213 | | - from |
214 | | - views_with_metadata |
215 | | - join principals on views_with_metadata.owner_principal_id = principals.principal_id |
216 | | - ), |
| 201 | + tables_and_views as ( |
| 202 | + select |
| 203 | + object_id, |
| 204 | + table_name, |
| 205 | + schema_name, |
| 206 | + principal_name, |
| 207 | + table_type |
| 208 | + from |
| 209 | + tables_with_metadata |
| 210 | + join principals on tables_with_metadata.owner_principal_id = principals.principal_id |
| 211 | + union all |
| 212 | + select |
| 213 | + object_id, |
| 214 | + table_name, |
| 215 | + schema_name, |
| 216 | + principal_name, |
| 217 | + table_type |
| 218 | + from |
| 219 | + views_with_metadata |
| 220 | + join principals on views_with_metadata.owner_principal_id = principals.principal_id |
| 221 | + ), |
217 | 222 |
|
218 | | - cols as ( |
| 223 | + cols as ( |
219 | 224 |
|
220 | | - select |
221 | | - c.object_id, |
222 | | - c.name as column_name, |
223 | | - c.column_id as column_index, |
224 | | - t.name as column_type |
225 | | - from sys.columns as c {{ information_schema_hints() }} |
226 | | - left join sys.types as t on c.system_type_id = t.system_type_id |
227 | | - ) |
| 225 | + select |
| 226 | + c.object_id, |
| 227 | + c.name as column_name, |
| 228 | + c.column_id as column_index, |
| 229 | + t.name as column_type |
| 230 | + from sys.columns as c {{ information_schema_hints() }} |
| 231 | + left join sys.types as t on c.system_type_id = t.system_type_id |
| 232 | + ) |
228 | 233 |
|
229 | | - select |
230 | | - DB_NAME() as table_database, |
231 | | - tv.schema_name as table_schema, |
232 | | - tv.table_name, |
233 | | - tv.table_type, |
234 | | - null as table_comment, |
235 | | - tv.principal_name as table_owner, |
236 | | - cols.column_name, |
237 | | - cols.column_index, |
238 | | - cols.column_type, |
239 | | - null as column_comment |
240 | | - from tables_and_views tv |
241 | | - join cols on tv.object_id = cols.object_id |
242 | | - where ( |
243 | | - {%- for relation in relations -%} |
244 | | - {% if relation.schema and relation.identifier %} |
245 | | - ( |
246 | | - upper(tv.schema_name) = upper('{{ relation.schema }}') |
247 | | - and upper(tv.table_name) = upper('{{ relation.identifier }}') |
248 | | - ) |
249 | | - {% elif relation.schema %} |
250 | | - ( |
251 | | - upper(tv.schema_name) = upper('{{ relation.schema }}') |
252 | | - ) |
253 | | - {% else %} |
254 | | - {% do exceptions.raise_compiler_error( |
255 | | - '`get_catalog_relations` requires a list of relations, each with a schema' |
256 | | - ) %} |
257 | | - {% endif %} |
| 234 | + select |
| 235 | + DB_NAME() as table_database, |
| 236 | + tv.schema_name as table_schema, |
| 237 | + tv.table_name, |
| 238 | + tv.table_type, |
| 239 | + null as table_comment, |
| 240 | + tv.principal_name as table_owner, |
| 241 | + cols.column_name, |
| 242 | + cols.column_index, |
| 243 | + cols.column_type, |
| 244 | + null as column_comment |
| 245 | + from tables_and_views tv |
| 246 | + join cols on tv.object_id = cols.object_id |
| 247 | + where ( |
| 248 | + {%- for relation in relations -%} |
| 249 | + {% if relation.schema and relation.identifier %} |
| 250 | + ( |
| 251 | + upper(tv.schema_name) = upper('{{ relation.schema }}') |
| 252 | + and upper(tv.table_name) = upper('{{ relation.identifier }}') |
| 253 | + ) |
| 254 | + {% elif relation.schema %} |
| 255 | + ( |
| 256 | + upper(tv.schema_name) = upper('{{ relation.schema }}') |
| 257 | + ) |
| 258 | + {% else %} |
| 259 | + {% do exceptions.raise_compiler_error( |
| 260 | + '`get_catalog_relations` requires a list of relations, each with a schema' |
| 261 | + ) %} |
| 262 | + {% endif %} |
258 | 263 |
|
259 | | - {%- if not loop.last %} or {% endif -%} |
260 | | - {%- endfor -%} |
261 | | - ) |
| 264 | + {%- if not loop.last %} or {% endif -%} |
| 265 | + {%- endfor -%} |
| 266 | + ) |
262 | 267 |
|
263 | | - order by column_index |
264 | | - {{ query_label }} |
265 | | - {%- endcall -%} |
| 268 | + order by column_index |
| 269 | + {{ query_label }} |
266 | 270 |
|
267 | | - {{ return(load_result('catalog').table) }} |
| 271 | + {%- endcall -%} |
| 272 | + {{ return(load_result('catalog').table) }} |
| 273 | + {% else %} |
| 274 | + {% do exceptions.raise_compiler_error( |
| 275 | + '`get_catalog_relations` can catalog one database at a time' |
| 276 | + ) %} |
| 277 | + {% endif %} |
268 | 278 |
|
269 | 279 | {%- endmacro %} |
0 commit comments