1818 from dbt .adapters .base import BaseRelation
1919 from dbt .adapters .base .column import Column
2020 from dbt .adapters .base .impl import AdapterResponse
21+ from sqlmesh .core .engine_adapter .base import DataObject
2122 from sqlmesh .dbt .relation import Policy
2223
2324
@@ -256,10 +257,9 @@ def get_relation(
256257
257258 def load_relation (self , relation : BaseRelation ) -> t .Optional [BaseRelation ]:
258259 mapped_table = self ._map_table_name (self ._normalize (self ._relation_to_table (relation )))
259- if not self .engine_adapter .table_exists (mapped_table ):
260- return None
261260
262- return self ._table_to_relation (mapped_table )
261+ data_object = self .engine_adapter .get_data_object (mapped_table )
262+ return self ._data_object_to_relation (data_object ) if data_object is not None else None
263263
264264 def list_relations (self , database : t .Optional [str ], schema : str ) -> t .List [BaseRelation ]:
265265 target_schema = schema_ (schema , catalog = database )
@@ -269,24 +269,10 @@ def list_relations(self, database: t.Optional[str], schema: str) -> t.List[BaseR
269269 return self .list_relations_without_caching (self ._table_to_relation (target_schema ))
270270
271271 def list_relations_without_caching (self , schema_relation : BaseRelation ) -> t .List [BaseRelation ]:
272- from sqlmesh .dbt .relation import RelationType
273-
274272 schema = self ._normalize (self ._schema (schema_relation ))
275273
276274 relations = [
277- self .relation_type .create (
278- database = do .catalog ,
279- schema = do .schema_name ,
280- identifier = do .name ,
281- quote_policy = self .quote_policy ,
282- # DBT relation types aren't snake case and instead just one word without spaces so we remove underscores
283- type = (
284- RelationType .External
285- if do .type .is_unknown
286- else RelationType (do .type .lower ().replace ("_" , "" ))
287- ),
288- )
289- for do in self .engine_adapter .get_data_objects (schema )
275+ self ._data_object_to_relation (do ) for do in self .engine_adapter .get_data_objects (schema )
290276 ]
291277 return relations
292278
@@ -401,6 +387,24 @@ def _map_table_name(self, table: exp.Table) -> exp.Table:
401387 def _relation_to_table (self , relation : BaseRelation ) -> exp .Table :
402388 return exp .to_table (relation .render (), dialect = self .project_dialect )
403389
390+ def _data_object_to_relation (self , data_object : DataObject ) -> BaseRelation :
391+ from sqlmesh .dbt .relation import RelationType
392+
393+ if data_object .type .is_unknown :
394+ dbt_relation_type = RelationType .External
395+ elif data_object .type .is_managed_table :
396+ dbt_relation_type = RelationType .Table
397+ else :
398+ dbt_relation_type = RelationType (data_object .type .lower ())
399+
400+ return self .relation_type .create (
401+ database = data_object .catalog ,
402+ schema = data_object .schema_name ,
403+ identifier = data_object .name ,
404+ quote_policy = self .quote_policy ,
405+ type = dbt_relation_type ,
406+ )
407+
404408 def _table_to_relation (self , table : exp .Table ) -> BaseRelation :
405409 return self .relation_type .create (
406410 database = table .catalog or None ,
0 commit comments