@@ -1046,7 +1046,8 @@ def create_table_like(
10461046 target_table_name: The name of the table to create. Can be fully qualified or just table name.
10471047 source_table_name: The name of the table to base the new table on.
10481048 """
1049- self .create_table (target_table_name , self .columns (source_table_name ), exists = exists )
1049+ self ._create_table_like (target_table_name , source_table_name , exists = exists , ** kwargs )
1050+ self ._clear_data_object_cache (target_table_name )
10501051
10511052 def clone_table (
10521053 self ,
@@ -2271,24 +2272,34 @@ def rename_table(
22712272 "Tried to rename table across catalogs which is not supported"
22722273 )
22732274 self ._rename_table (old_table_name , new_table_name )
2275+ self ._clear_data_object_cache (old_table_name )
2276+ self ._clear_data_object_cache (new_table_name )
22742277
2275- def get_data_object (self , target_name : TableName ) -> t .Optional [DataObject ]:
2278+ def get_data_object (
2279+ self , target_name : TableName , safe_to_cache : bool = False
2280+ ) -> t .Optional [DataObject ]:
22762281 target_table = exp .to_table (target_name )
22772282 existing_data_objects = self .get_data_objects (
2278- schema_ (target_table .db , target_table .catalog ), {target_table .name }
2283+ schema_ (target_table .db , target_table .catalog ),
2284+ {target_table .name },
2285+ safe_to_cache = safe_to_cache ,
22792286 )
22802287 if existing_data_objects :
22812288 return existing_data_objects [0 ]
22822289 return None
22832290
22842291 def get_data_objects (
2285- self , schema_name : SchemaName , object_names : t .Optional [t .Set [str ]] = None
2292+ self ,
2293+ schema_name : SchemaName ,
2294+ object_names : t .Optional [t .Set [str ]] = None ,
2295+ safe_to_cache : bool = False ,
22862296 ) -> t .List [DataObject ]:
22872297 """Lists all data objects in the target schema.
22882298
22892299 Args:
22902300 schema_name: The name of the schema to list data objects from.
22912301 object_names: If provided, only return data objects with these names.
2302+ safe_to_cache: Whether it is safe to cache the results of this call.
22922303
22932304 Returns:
22942305 A list of data objects in the target schema.
@@ -2323,31 +2334,36 @@ def get_data_objects(
23232334 object_names_list [i : i + self .DATA_OBJECT_FILTER_BATCH_SIZE ]
23242335 for i in range (0 , len (object_names_list ), self .DATA_OBJECT_FILTER_BATCH_SIZE )
23252336 ]
2326- fetched_objects = [
2327- obj
2328- for batch in batches
2329- for obj in self ._get_data_objects (schema_name , set (batch ))
2330- ]
2331-
2332- for obj in fetched_objects :
2333- cache_key = _get_data_object_cache_key (obj .catalog , obj .schema_name , obj .name )
2334- self ._data_object_cache [cache_key ] = obj
23352337
2336- fetched_object_names = {obj .name for obj in fetched_objects }
2337- for missing_name in missing_names - fetched_object_names :
2338- cache_key = _get_data_object_cache_key (
2339- target_schema .catalog , target_schema .db , missing_name
2340- )
2341- self ._data_object_cache [cache_key ] = None
2338+ fetched_objects = []
2339+ fetched_object_names = set ()
2340+ for batch in batches :
2341+ objects = self ._get_data_objects (schema_name , set (batch ))
2342+ for obj in objects :
2343+ if safe_to_cache :
2344+ cache_key = _get_data_object_cache_key (
2345+ obj .catalog , obj .schema_name , obj .name
2346+ )
2347+ self ._data_object_cache [cache_key ] = obj
2348+ fetched_objects .append (obj )
2349+ fetched_object_names .add (obj .name )
2350+
2351+ if safe_to_cache :
2352+ for missing_name in missing_names - fetched_object_names :
2353+ cache_key = _get_data_object_cache_key (
2354+ target_schema .catalog , target_schema .db , missing_name
2355+ )
2356+ self ._data_object_cache [cache_key ] = None
23422357
23432358 return cached_objects + fetched_objects
23442359
23452360 return cached_objects
23462361
23472362 fetched_objects = self ._get_data_objects (schema_name )
2348- for obj in fetched_objects :
2349- cache_key = _get_data_object_cache_key (obj .catalog , obj .schema_name , obj .name )
2350- self ._data_object_cache [cache_key ] = obj
2363+ if safe_to_cache :
2364+ for obj in fetched_objects :
2365+ cache_key = _get_data_object_cache_key (obj .catalog , obj .schema_name , obj .name )
2366+ self ._data_object_cache [cache_key ] = obj
23512367 return fetched_objects
23522368
23532369 def fetchone (
@@ -2951,6 +2967,15 @@ def _create_column_comments(
29512967 exc_info = True ,
29522968 )
29532969
2970+ def _create_table_like (
2971+ self ,
2972+ target_table_name : TableName ,
2973+ source_table_name : TableName ,
2974+ exists : bool ,
2975+ ** kwargs : t .Any ,
2976+ ) -> None :
2977+ self .create_table (target_table_name , self .columns (source_table_name ), exists = exists )
2978+
29542979 def _rename_table (
29552980 self ,
29562981 old_table_name : TableName ,
@@ -3017,5 +3042,5 @@ def _decoded_str(value: t.Union[str, bytes]) -> str:
30173042
30183043def _get_data_object_cache_key (catalog : t .Optional [str ], schema_name : str , object_name : str ) -> str :
30193044 """Returns a cache key for a data object based on its fully qualified name."""
3020- catalog = catalog or ""
3021- return f"{ catalog } . { schema_name } .{ object_name } "
3045+ catalog = f" { catalog } ." if catalog else ""
3046+ return f"{ catalog } { schema_name } .{ object_name } "
0 commit comments