1717
1818use std:: collections:: HashMap ;
1919use std:: fmt:: Debug ;
20+ use std:: str:: FromStr ;
2021use std:: sync:: Arc ;
2122
2223use anyhow:: anyhow;
@@ -338,6 +339,13 @@ impl Catalog for GlueCatalog {
338339 namespace : & NamespaceIdent ,
339340 properties : HashMap < String , String > ,
340341 ) -> Result < Namespace > {
342+ if self . namespace_exists ( namespace) . await ? {
343+ return Err ( Error :: new (
344+ ErrorKind :: NamespaceAlreadyExists ,
345+ format ! ( "Namespace {namespace:?} already exists" ) ,
346+ ) ) ;
347+ }
348+
341349 let db_input = convert_to_database ( namespace, & properties) ?;
342350
343351 let builder = self . client . 0 . create_database ( ) . database_input ( db_input) ;
@@ -364,15 +372,27 @@ impl Catalog for GlueCatalog {
364372 let builder = self . client . 0 . get_database ( ) . name ( & db_name) ;
365373 let builder = with_catalog_id ! ( builder, self . config) ;
366374
367- let resp = builder. send ( ) . await . map_err ( from_aws_sdk_error) ?;
375+ let resp = builder. send ( ) . await . map_err ( |err| {
376+ if err
377+ . as_service_error ( )
378+ . map ( |e| e. is_entity_not_found_exception ( ) )
379+ == Some ( true )
380+ {
381+ return Error :: new (
382+ ErrorKind :: NamespaceNotFound ,
383+ format ! ( "Namespace {namespace:?} does not exist" ) ,
384+ ) ;
385+ }
386+ from_aws_sdk_error ( err)
387+ } ) ?;
368388
369389 match resp. database ( ) {
370390 Some ( db) => {
371391 let namespace = convert_to_namespace ( db) ;
372392 Ok ( namespace)
373393 }
374394 None => Err ( Error :: new (
375- ErrorKind :: DataInvalid ,
395+ ErrorKind :: NamespaceNotFound ,
376396 format ! ( "Database with name: {db_name} does not exist" ) ,
377397 ) ) ,
378398 }
@@ -428,6 +448,13 @@ impl Catalog for GlueCatalog {
428448 namespace : & NamespaceIdent ,
429449 properties : HashMap < String , String > ,
430450 ) -> Result < ( ) > {
451+ if !self . namespace_exists ( namespace) . await ? {
452+ return Err ( Error :: new (
453+ ErrorKind :: NamespaceNotFound ,
454+ format ! ( "Namespace {namespace:?} does not exist" ) ,
455+ ) ) ;
456+ }
457+
431458 let db_name = validate_namespace ( namespace) ?;
432459 let db_input = convert_to_database ( namespace, & properties) ?;
433460
@@ -455,6 +482,13 @@ impl Catalog for GlueCatalog {
455482 /// - `Err(...)` signifies failure to drop the namespace due to validation
456483 /// errors, connectivity issues, or Glue Catalog constraints.
457484 async fn drop_namespace ( & self , namespace : & NamespaceIdent ) -> Result < ( ) > {
485+ if !self . namespace_exists ( namespace) . await ? {
486+ return Err ( Error :: new (
487+ ErrorKind :: NamespaceNotFound ,
488+ format ! ( "Namespace {namespace:?} does not exist" ) ,
489+ ) ) ;
490+ }
491+
458492 let db_name = validate_namespace ( namespace) ?;
459493 let table_list = self . list_tables ( namespace) . await ?;
460494
@@ -550,14 +584,14 @@ impl Catalog for GlueCatalog {
550584 let metadata = TableMetadataBuilder :: from_table_creation ( creation) ?
551585 . build ( ) ?
552586 . metadata ;
553- let metadata_location =
554- MetadataLocation :: new_with_table_location ( location. clone ( ) ) . to_string ( ) ;
587+ let metadata_location = MetadataLocation :: new_with_metadata ( location. clone ( ) , & metadata) ;
555588
556589 metadata. write_to ( & self . file_io , & metadata_location) . await ?;
557590
591+ let metadata_location_str = metadata_location. to_string ( ) ;
558592 let glue_table = convert_to_glue_table (
559593 & table_name,
560- metadata_location . clone ( ) ,
594+ metadata_location_str . clone ( ) ,
561595 & metadata,
562596 metadata. properties ( ) ,
563597 None ,
@@ -575,7 +609,7 @@ impl Catalog for GlueCatalog {
575609
576610 Table :: builder ( )
577611 . file_io ( self . file_io ( ) )
578- . metadata_location ( metadata_location )
612+ . metadata_location ( metadata_location_str )
579613 . metadata ( metadata)
580614 . identifier ( TableIdent :: new ( NamespaceIdent :: new ( db_name) , table_name) )
581615 . build ( )
@@ -813,12 +847,13 @@ impl Catalog for GlueCatalog {
813847 let current_metadata_location = current_table. metadata_location_result ( ) ?. to_string ( ) ;
814848
815849 let staged_table = commit. apply ( current_table) ?;
816- let staged_metadata_location = staged_table. metadata_location_result ( ) ?;
850+ let staged_metadata_location_str = staged_table. metadata_location_result ( ) ?;
851+ let staged_metadata_location = MetadataLocation :: from_str ( staged_metadata_location_str) ?;
817852
818853 // Write new metadata
819854 staged_table
820855 . metadata ( )
821- . write_to ( staged_table. file_io ( ) , staged_metadata_location)
856+ . write_to ( staged_table. file_io ( ) , & staged_metadata_location)
822857 . await ?;
823858
824859 // Persist staged table to Glue with optimistic locking
0 commit comments