CTTY commented on code in PR #1521:
URL: https://github.com/apache/iceberg-rust/pull/1521#discussion_r2223848333


##########
crates/catalog/rest/src/catalog.rs:
##########
@@ -745,10 +749,86 @@ impl Catalog for RestCatalog {
         _table_ident: &TableIdent,
         _metadata_location: String,
     ) -> Result<Table> {
-        Err(Error::new(
-            ErrorKind::FeatureUnsupported,
-            "Registering a table is not supported yet",
-        ))
+        let context = self.context().await?;
+
+        let request = context
+            .client
+            .request(
+                Method::POST,
+                context
+                    .config
+                    .register_table_endpoint(_table_ident.namespace()),
+            )
+            .json(&RegisterTableRequest {
+                name: _table_ident.name.clone(),
+                metadata_location: _metadata_location.clone(),
+            })
+            .build()?;
+
+        let http_response = context.client.query_catalog(request).await?;
+
+        let response: LoadTableResponse = match http_response.status() {
+            StatusCode::OK => {
+                
deserialize_catalog_response::<LoadTableResponse>(http_response).await?
+            }
+            StatusCode::BAD_REQUEST => {
+                return Err(Error::new(
+                    ErrorKind::Unexpected,
+                    "Unexpected error while registering table.",
+                ));
+            }
+            StatusCode::UNAUTHORIZED => {
+                return Err(Error::new(
+                    ErrorKind::Unexpected,
+                    "Authenticated user does not have the necessary 
permissions.",
+                ));
+            }
+            StatusCode::FORBIDDEN => {
+                return Err(Error::new(
+                    ErrorKind::Unexpected,
+                    "Authenticated user does not have the necessary 
permissions.",
+                ));
+            }
+            StatusCode::NOT_FOUND => {
+                return Err(Error::new(
+                    ErrorKind::NamespaceNotFound,
+                    "The namespace specified does not exist.",
+                ));
+            }
+            StatusCode::CONFLICT => {
+                return Err(Error::new(
+                    ErrorKind::TableAlreadyExists,

Review Comment:
   This should use `ErrorKind::CatalogCommitConflicts` and should be explicitly 
set to retryable using `with_retryable(true)`



##########
crates/catalog/rest/src/catalog.rs:
##########
@@ -745,10 +749,86 @@ impl Catalog for RestCatalog {
         _table_ident: &TableIdent,
         _metadata_location: String,

Review Comment:
   Same here with the `_` prefix



##########
crates/catalog/rest/src/catalog.rs:
##########
@@ -745,10 +749,86 @@ impl Catalog for RestCatalog {
         _table_ident: &TableIdent,
         _metadata_location: String,
     ) -> Result<Table> {
-        Err(Error::new(
-            ErrorKind::FeatureUnsupported,
-            "Registering a table is not supported yet",
-        ))
+        let context = self.context().await?;
+
+        let request = context
+            .client
+            .request(
+                Method::POST,
+                context
+                    .config
+                    .register_table_endpoint(_table_ident.namespace()),
+            )
+            .json(&RegisterTableRequest {
+                name: _table_ident.name.clone(),
+                metadata_location: _metadata_location.clone(),
+            })
+            .build()?;
+
+        let http_response = context.client.query_catalog(request).await?;
+
+        let response: LoadTableResponse = match http_response.status() {
+            StatusCode::OK => {
+                
deserialize_catalog_response::<LoadTableResponse>(http_response).await?
+            }
+            StatusCode::BAD_REQUEST => {
+                return Err(Error::new(
+                    ErrorKind::Unexpected,
+                    "Unexpected error while registering table.",
+                ));
+            }
+            StatusCode::UNAUTHORIZED => {
+                return Err(Error::new(
+                    ErrorKind::Unexpected,
+                    "Authenticated user does not have the necessary 
permissions.",
+                ));
+            }
+            StatusCode::FORBIDDEN => {
+                return Err(Error::new(
+                    ErrorKind::Unexpected,
+                    "Authenticated user does not have the necessary 
permissions.",
+                ));
+            }

Review Comment:
   Following iceberg-java's [error handling 
logic](https://github.com/apache/iceberg/blob/5845e1e02bf1078c485ff7fe917046e164ec64a3/core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java#L519),
 I think it's necessary to handle all of these error codes



##########
crates/catalog/rest/src/catalog.rs:
##########
@@ -745,10 +749,86 @@ impl Catalog for RestCatalog {
         _table_ident: &TableIdent,

Review Comment:
   the `_` prefix is for unused variables, we should remove it since now it's 
implemented



##########
crates/catalog/rest/src/catalog.rs:
##########
@@ -745,10 +749,86 @@ impl Catalog for RestCatalog {
         _table_ident: &TableIdent,
         _metadata_location: String,
     ) -> Result<Table> {
-        Err(Error::new(
-            ErrorKind::FeatureUnsupported,
-            "Registering a table is not supported yet",
-        ))
+        let context = self.context().await?;
+
+        let request = context
+            .client
+            .request(
+                Method::POST,
+                context
+                    .config
+                    .register_table_endpoint(_table_ident.namespace()),
+            )
+            .json(&RegisterTableRequest {
+                name: _table_ident.name.clone(),
+                metadata_location: _metadata_location.clone(),
+            })
+            .build()?;
+
+        let http_response = context.client.query_catalog(request).await?;
+
+        let response: LoadTableResponse = match http_response.status() {
+            StatusCode::OK => {
+                
deserialize_catalog_response::<LoadTableResponse>(http_response).await?
+            }
+            StatusCode::BAD_REQUEST => {
+                return Err(Error::new(
+                    ErrorKind::Unexpected,
+                    "Unexpected error while registering table.",
+                ));
+            }
+            StatusCode::UNAUTHORIZED => {
+                return Err(Error::new(
+                    ErrorKind::Unexpected,
+                    "Authenticated user does not have the necessary 
permissions.",
+                ));
+            }
+            StatusCode::FORBIDDEN => {
+                return Err(Error::new(
+                    ErrorKind::Unexpected,
+                    "Authenticated user does not have the necessary 
permissions.",
+                ));
+            }
+            StatusCode::NOT_FOUND => {
+                return Err(Error::new(
+                    ErrorKind::NamespaceNotFound,
+                    "The namespace specified does not exist.",
+                ));
+            }
+            StatusCode::CONFLICT => {
+                return Err(Error::new(
+                    ErrorKind::TableAlreadyExists,
+                    "The given table already exists.",
+                ));
+            }
+            StatusCode::SERVICE_UNAVAILABLE => {
+                return Err(Error::new(
+                    ErrorKind::Unexpected,
+                    "The service is not ready to handle the request.",
+                ));
+            }
+            StatusCode::INTERNAL_SERVER_ERROR => {
+                return Err(Error::new(
+                    ErrorKind::Unexpected,
+                    "An unknown server-side problem occurred; the commit state 
is unknown.",
+                ));
+            }
+            _ => return 
Err(deserialize_unexpected_catalog_error(http_response).await),
+        };
+
+        let metadata_location = 
response.metadata_location.as_ref().ok_or(Error::new(
+            ErrorKind::DataInvalid,
+            "Metadata location missing in `register_table` response!",
+        ))?;

Review Comment:
   nit: this can be slightly simplified using some syntax sugar
   ```
   let Some(metadata_location) = response.metadata_location else { return 
Err(Error::new(...)) }
   
   let file_io = self.load_file_io(metadata_location, None).await?;
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to