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,8 +257,10 @@ 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
260+
261+ data_object = self .engine_adapter .get_data_object (mapped_table )
262+ if data_object is not None :
263+ return self ._data_object_to_relation (data_object )
261264
262265 return self ._table_to_relation (mapped_table )
263266
@@ -269,24 +272,10 @@ def list_relations(self, database: t.Optional[str], schema: str) -> t.List[BaseR
269272 return self .list_relations_without_caching (self ._table_to_relation (target_schema ))
270273
271274 def list_relations_without_caching (self , schema_relation : BaseRelation ) -> t .List [BaseRelation ]:
272- from sqlmesh .dbt .relation import RelationType
273-
274275 schema = self ._normalize (self ._schema (schema_relation ))
275276
276277 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 )
278+ self ._data_object_to_relation (do ) for do in self .engine_adapter .get_data_objects (schema )
290279 ]
291280 return relations
292281
@@ -401,6 +390,24 @@ def _map_table_name(self, table: exp.Table) -> exp.Table:
401390 def _relation_to_table (self , relation : BaseRelation ) -> exp .Table :
402391 return exp .to_table (relation .render (), dialect = self .project_dialect )
403392
393+ def _data_object_to_relation (self , data_object : DataObject ) -> BaseRelation :
394+ from sqlmesh .dbt .relation import RelationType
395+
396+ if data_object .type .is_unknown :
397+ dbt_relation_type = RelationType .External
398+ elif data_object .type .is_managed_table :
399+ dbt_relation_type = RelationType .Table
400+ else :
401+ dbt_relation_type = RelationType (data_object .type .lower ())
402+
403+ return self .relation_type .create (
404+ database = data_object .catalog ,
405+ schema = data_object .schema_name ,
406+ identifier = data_object .name ,
407+ quote_policy = self .quote_policy ,
408+ type = dbt_relation_type ,
409+ )
410+
404411 def _table_to_relation (self , table : exp .Table ) -> BaseRelation :
405412 return self .relation_type .create (
406413 database = table .catalog or None ,
0 commit comments