@@ -216,11 +216,6 @@ impl SqlCatalog {
216
216
}
217
217
}
218
218
}
219
-
220
- /// Get the catalogs `FileIO`
221
- pub fn file_io ( & self ) -> FileIO {
222
- self . fileio . clone ( )
223
- }
224
219
}
225
220
226
221
#[ async_trait]
@@ -773,11 +768,11 @@ impl Catalog for SqlCatalog {
773
768
table_ident : & TableIdent ,
774
769
metadata_location : String ,
775
770
) -> Result < Table > {
776
- if ! self . table_exists ( table_ident) . await ? {
777
- return no_such_table_err ( table_ident) ;
771
+ if self . table_exists ( table_ident) . await ? {
772
+ return table_already_exists_err ( table_ident) ;
778
773
}
779
774
780
- let metadata = TableMetadata :: read_from ( & self . fileio , metadata_location. clone ( ) ) . await ?;
775
+ let metadata = TableMetadata :: read_from ( & self . fileio , & metadata_location) . await ?;
781
776
782
777
let namespace = table_ident. namespace ( ) ;
783
778
let tbl_name = table_ident. name ( ) . to_string ( ) ;
@@ -792,7 +787,7 @@ impl Catalog for SqlCatalog {
792
787
. identifier ( table_ident. clone ( ) )
793
788
. metadata_location ( metadata_location)
794
789
. metadata ( metadata)
795
- . file_io ( self . file_io ( ) )
790
+ . file_io ( self . fileio . clone ( ) )
796
791
. build ( ) ?)
797
792
}
798
793
@@ -1930,4 +1925,58 @@ mod tests {
1930
1925
"Unexpected => No such table: TableIdent { namespace: NamespaceIdent([\" a\" ]), name: \" tbl1\" }"
1931
1926
) ;
1932
1927
}
1928
+
1929
+ #[ tokio:: test]
1930
+ async fn test_register_table_throws_error_if_table_with_same_name_already_exists ( ) {
1931
+ let warehouse_loc = temp_path ( ) ;
1932
+ let catalog = new_sql_catalog ( warehouse_loc. clone ( ) ) . await ;
1933
+ let namespace_ident = NamespaceIdent :: new ( "a" . into ( ) ) ;
1934
+ create_namespace ( & catalog, & namespace_ident) . await ;
1935
+ let table_name = "tbl1" ;
1936
+ let table_ident = TableIdent :: new ( namespace_ident. clone ( ) , table_name. into ( ) ) ;
1937
+ create_table ( & catalog, & table_ident) . await ;
1938
+
1939
+ assert_eq ! (
1940
+ catalog
1941
+ . register_table( & table_ident, warehouse_loc)
1942
+ . await
1943
+ . unwrap_err( )
1944
+ . to_string( ) ,
1945
+ format!( "Unexpected => Table {:?} already exists." , & table_ident)
1946
+ ) ;
1947
+ }
1948
+
1949
+ #[ tokio:: test]
1950
+ async fn test_register_table ( ) {
1951
+ let warehouse_loc = temp_path ( ) ;
1952
+ let catalog = new_sql_catalog ( warehouse_loc. clone ( ) ) . await ;
1953
+ let namespace_ident = NamespaceIdent :: new ( "a" . into ( ) ) ;
1954
+ create_namespace ( & catalog, & namespace_ident) . await ;
1955
+
1956
+ let table_name = "abc" ;
1957
+ let location = warehouse_loc. clone ( ) ;
1958
+ let table_creation = TableCreation :: builder ( )
1959
+ . name ( table_name. into ( ) )
1960
+ . location ( location. clone ( ) )
1961
+ . schema ( simple_table_schema ( ) )
1962
+ . build ( ) ;
1963
+
1964
+ let table_ident = TableIdent :: new ( namespace_ident. clone ( ) , table_name. into ( ) ) ;
1965
+ let expected_table = catalog
1966
+ . create_table ( & namespace_ident, table_creation)
1967
+ . await
1968
+ . unwrap ( ) ;
1969
+
1970
+ assert_table_eq ( & expected_table, & table_ident, & simple_table_schema ( ) ) ;
1971
+
1972
+ let _ = catalog. drop_table ( & table_ident) . await ;
1973
+
1974
+ let table = catalog
1975
+ . register_table ( & table_ident, location. clone ( ) )
1976
+ . await
1977
+ . unwrap ( ) ;
1978
+
1979
+ assert_eq ! ( table. identifier( ) , expected_table. identifier( ) ) ;
1980
+ assert_eq ! ( table. metadata_location( ) , Some ( location. as_str( ) ) ) ;
1981
+ }
1933
1982
}
0 commit comments