nastra commented on code in PR #6512:
URL: https://github.com/apache/iceberg/pull/6512#discussion_r1230705685


##########
core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java:
##########
@@ -400,7 +402,37 @@ public void invalidateTable(SessionContext context, 
TableIdentifier ident) {}
   @Override
   public Table registerTable(
       SessionContext context, TableIdentifier ident, String 
metadataFileLocation) {
-    throw new UnsupportedOperationException("Register table is not supported");
+    checkIdentifierIsValid(ident);
+
+    Preconditions.checkArgument(
+        metadataFileLocation != null && !metadataFileLocation.isEmpty(),
+        "Invalid metadata file location: %s",
+        metadataFileLocation);
+
+    if (tableExists(context, ident)) {
+      throw new AlreadyExistsException("Table already exists: %s", ident);
+    }
+
+    RegisterTableRequest request = new RegisterTableRequest(ident.name(), 
metadataFileLocation);
+
+    LoadTableResponse response =
+        client.post(
+            paths.register(ident.namespace()),
+            request,
+            LoadTableResponse.class,
+            headers(context),
+            ErrorHandlers.tableErrorHandler());
+
+    AuthSession session = tableSession(response.config(), session(context));
+    RESTTableOperations ops =
+        new RESTTableOperations(
+            client,
+            paths.table(ident),
+            session::headers,
+            tableFileIO(context, response.config()),
+            response.tableMetadata());
+
+    return new BaseTable(ops, fullTableName(ident));

Review Comment:
   just an FYI that we'd need to call `trackFileIO(ops);` just before the 
return here



##########
core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java:
##########
@@ -400,7 +402,37 @@ public void invalidateTable(SessionContext context, 
TableIdentifier ident) {}
   @Override
   public Table registerTable(
       SessionContext context, TableIdentifier ident, String 
metadataFileLocation) {
-    throw new UnsupportedOperationException("Register table is not supported");
+    checkIdentifierIsValid(ident);
+
+    Preconditions.checkArgument(
+        metadataFileLocation != null && !metadataFileLocation.isEmpty(),
+        "Invalid metadata file location: %s",
+        metadataFileLocation);
+
+    if (tableExists(context, ident)) {
+      throw new AlreadyExistsException("Table already exists: %s", ident);
+    }
+
+    RegisterTableRequest request = new RegisterTableRequest(ident.name(), 
metadataFileLocation);

Review Comment:
   I think it's better to track the `TableIdentifier` instead of a plain string



##########
core/src/main/java/org/apache/iceberg/rest/requests/RegisterTableRequestParser.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.rest.requests;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.util.JsonUtil;
+
+public class RegisterTableRequestParser {
+
+  private static final String TABLE_NAME = "table-name";
+  private static final String METADATA_FILE_LOCATION = 
"metadata-file-location";
+
+  private RegisterTableRequestParser() {}
+
+  public static String toJson(RegisterTableRequest request) {
+    return toJson(request, false);
+  }
+
+  public static String toJson(RegisterTableRequest request, boolean pretty) {
+    return JsonUtil.generate(gen -> toJson(request, gen), pretty);
+  }
+
+  public static void toJson(RegisterTableRequest request, JsonGenerator gen) 
throws IOException {
+    Preconditions.checkArgument(null != request, "Invalid register table 
request: null");
+
+    gen.writeStartObject();
+
+    gen.writeStringField(TABLE_NAME, request.tableName());

Review Comment:
   once we track the `TableIdentifier` in the request, you can change this to 
   ```
   gen.writeFieldName(IDENTIFIER);
   TableIdentifierParser.toJson(request.identifier(), gen);
   ```



##########
core/src/main/java/org/apache/iceberg/rest/requests/RegisterTableRequestParser.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.rest.requests;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.util.JsonUtil;
+
+public class RegisterTableRequestParser {
+
+  private static final String TABLE_NAME = "table-name";
+  private static final String METADATA_FILE_LOCATION = 
"metadata-file-location";
+
+  private RegisterTableRequestParser() {}
+
+  public static String toJson(RegisterTableRequest request) {
+    return toJson(request, false);
+  }
+
+  public static String toJson(RegisterTableRequest request, boolean pretty) {
+    return JsonUtil.generate(gen -> toJson(request, gen), pretty);
+  }
+
+  public static void toJson(RegisterTableRequest request, JsonGenerator gen) 
throws IOException {
+    Preconditions.checkArgument(null != request, "Invalid register table 
request: null");
+
+    gen.writeStartObject();
+
+    gen.writeStringField(TABLE_NAME, request.tableName());
+    gen.writeStringField(METADATA_FILE_LOCATION, 
request.metadataFileLocation());
+
+    gen.writeEndObject();
+  }
+
+  public static RegisterTableRequest fromJson(String json) {
+    return JsonUtil.parse(json, RegisterTableRequestParser::fromJson);
+  }
+
+  public static RegisterTableRequest fromJson(JsonNode json) {
+    Preconditions.checkArgument(
+        null != json, "Cannot parse register table request from null object");
+
+    String name = JsonUtil.getString(TABLE_NAME, json);

Review Comment:
   and this would be `TableIdentifierParser.fromJson(JsonUtil.get(IDENTIFIER, 
node))`



-- 
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