1717 TypedefCatalogTypeReferenceEnum ,
1818)
1919from fenic_cloud .hasura_client .generated_graphql_client .input_types import (
20- CatalogNamespaceInsertInput ,
2120 CreateTableInput ,
2221 NestedFieldInput ,
2322 SchemaInput ,
3029)
3130
3231from fenic ._backends .cloud .manager import CloudSessionManager
33- from fenic ._backends .cloud .session_state import CloudSessionState
3432from fenic ._backends .local .catalog import (
3533 DEFAULT_CATALOG_NAME ,
3634 DEFAULT_DATABASE_NAME ,
@@ -70,16 +68,21 @@ class CloudCatalog(BaseCatalog):
7068 all table reads and writes should go through this class for unified table name canonicalization.
7169 """
7270
73- def __init__ (self , session_state : CloudSessionState , cloud_session_manager : CloudSessionManager ):
71+ def __init__ (self ,
72+ ephemeral_catalog_id : str ,
73+ asyncio_loop : asyncio .AbstractEventLoop ,
74+ cloud_session_manager : CloudSessionManager ):
7475 """Initialize the remote catalog."""
75- self .session_state = session_state
76+ self .cloud_session_manager = cloud_session_manager
7677 self .lock = threading .Lock ()
77- self .current_catalog_id : UUID = UUID (session_state .ephemeral_catalog_id )
78+ self .asyncio_loop = asyncio_loop
79+ self .ephemeral_catalog_id : UUID = UUID (ephemeral_catalog_id )
80+ self .current_catalog_id : UUID = self .ephemeral_catalog_id
7881 self .current_catalog_name : str = DEFAULT_CATALOG_NAME
7982 self .current_database_name : str = DEFAULT_DATABASE_NAME
80- self .user_id = cloud_session_manager .user_id
81- self .organization_id = cloud_session_manager .organization_id
82- self .user_client = cloud_session_manager .hasura_user_client
83+ self .user_id = self . cloud_session_manager ._client_id
84+ self .organization_id = self . cloud_session_manager ._organization_id
85+ self .user_client = self . cloud_session_manager .hasura_user_client
8386
8487 def does_catalog_exist (self , catalog_name : str ) -> bool :
8588 """Checks if a catalog with the specified name exists."""
@@ -129,7 +132,6 @@ def create_catalog(self, catalog_name: str, ignore_if_exists: bool = True) -> bo
129132 parent_organization_id = UUID (self .organization_id ),
130133 catalog_type = TypedefCatalogTypeReferenceEnum .INTERNAL_TYPEDEF ,
131134 catalog_warehouse = "" ,
132- catalog_description = None ,
133135 )
134136 )
135137 return True
@@ -342,14 +344,12 @@ def _create_database(
342344 raise DatabaseAlreadyExistsError (database_name )
343345
344346 self ._execute_catalog_command (
345- self .user_client .create_namespace (
346- namespace = CatalogNamespaceInsertInput (
347- name = db_identifier .db ,
348- canonical_name = db_identifier .db .casefold (),
349- parent_organization_id = self .organization_id ,
350- catalog_id = self .current_catalog_id ,
351- created_by_user_id = self .user_id ,
352- )
347+ self .user_client .sc_create_namespace (
348+ dispatch = self ._get_catalog_dispatch_input (self .current_catalog_id ),
349+ name = db_identifier .db ,
350+ canonical_name = db_identifier .db .casefold (),
351+ description = None ,
352+ properties = [],
353353 )
354354 )
355355 return True
@@ -365,8 +365,8 @@ def _does_table_exist(
365365 if not self ._does_database_exist (catalog_name , db_name ):
366366 return False
367367
368- tables = self ._get_tables_for_database (catalog_name , db_name )
369- return any ( compare_object_names ( table , table_name ) for table in tables )
368+ table = self ._get_table (catalog_name , db_name , table_name )
369+ return table is not None
370370
371371 def _set_current_catalog (self , catalog_name : str ) -> None :
372372 if not catalog_name :
@@ -376,7 +376,7 @@ def _set_current_catalog(self, catalog_name: str) -> None:
376376 return
377377
378378 if compare_object_names (catalog_name , DEFAULT_CATALOG_NAME ):
379- self .current_catalog_id = UUID ( self .session_state . ephemeral_catalog_id )
379+ self .current_catalog_id = self .ephemeral_catalog_id
380380 self .current_catalog_name = DEFAULT_CATALOG_NAME
381381 return
382382
@@ -421,7 +421,7 @@ def _does_database_exist(self, catalog_name: str, database_name: str) -> bool:
421421
422422 def _execute_catalog_command (self , command : Coroutine [Any , Any , Any ]) -> Any :
423423 return asyncio .run_coroutine_threadsafe (
424- command , self .session_state . asyncio_loop
424+ command , self .asyncio_loop
425425 ).result ()
426426
427427 def _get_catalog_by_name (self , catalog_name : str ) -> Optional [CatalogKey ]:
@@ -481,18 +481,35 @@ def _get_tables_for_database(
481481 )
482482 return [dataset .name for dataset in result .catalog_dataset ]
483483
484+ def _get_table (
485+ self ,
486+ catalog_name : str ,
487+ db_name : str ,
488+ table_name : str ,
489+ ignore_if_not_exists : bool = True ,
490+ ) -> LoadTableSimpleCatalogLoadTable :
491+ catalog_id = self ._get_catalog_id (catalog_name )
492+ try :
493+ result = self ._execute_catalog_command (
494+ self .user_client .load_table (
495+ dispatch = self ._get_catalog_dispatch_input (catalog_id ),
496+ namespace = db_name ,
497+ name = table_name ,
498+ )
499+ )
500+ return result .simple_catalog .load_table
501+ except Exception as e :
502+ if ignore_if_not_exists :
503+ return None
504+ logger .debug (f"Error getting table { table_name } from catalog { catalog_name } and database { db_name } : { e } " )
505+ raise e
506+
507+
484508 def _get_table_details (
485509 self , catalog_name : str , db_name : str , table_name : str
486510 ) -> Schema :
487- catalog_id = self ._get_catalog_id (catalog_name )
488- result = self ._execute_catalog_command (
489- self .user_client .load_table (
490- dispatch = self ._get_catalog_dispatch_input (catalog_id ),
491- namespace = db_name ,
492- name = table_name ,
493- )
494- )
495- return self ._get_table_schema (result .simple_catalog .load_table )
511+ load_table = self ._get_table (catalog_name , db_name , table_name , ignore_if_not_exists = False )
512+ return self ._get_table_schema (load_table )
496513
497514 def _get_catalog_dispatch_input (
498515 self ,
0 commit comments