flyrain commented on code in PR #1298:
URL: https://github.com/apache/polaris/pull/1298#discussion_r2036245026


##########
integration-tests/src/main/java/org/apache/polaris/service/it/env/GenericTableApi.java:
##########
@@ -0,0 +1,118 @@
+/*
+ * 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.polaris.service.it.env;
+
+import static jakarta.ws.rs.core.Response.Status.NO_CONTENT;
+import static org.assertj.core.api.Assertions.assertThat;
+
+import jakarta.ws.rs.client.Client;
+import jakarta.ws.rs.client.Entity;
+import jakarta.ws.rs.core.MultivaluedHashMap;
+import jakarta.ws.rs.core.Response;
+import java.net.URI;
+import java.util.List;
+import java.util.Map;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.rest.RESTUtil;
+import org.apache.iceberg.rest.responses.OAuthTokenResponse;
+import org.apache.polaris.service.types.CreateGenericTableRequest;
+import org.apache.polaris.service.types.GenericTable;
+import org.apache.polaris.service.types.ListGenericTablesResponse;
+import org.apache.polaris.service.types.LoadGenericTableResponse;
+
+/**
+ * A simple, non-exhaustive set of helper methods for accessing the generic 
tables REST API
+ *
+ * @see PolarisClient#genericTableApi(ClientCredentials)
+ */
+public class GenericTableApi extends RestApi {
+  GenericTableApi(Client client, PolarisApiEndpoints endpoints, String 
authToken, URI uri) {
+    super(client, endpoints, authToken, uri);
+  }
+
+  public String obtainToken(ClientCredentials credentials) {

Review Comment:
   Minor: do we need this method? I don't think we need to test the token 
endpoint here.



##########
integration-tests/src/main/java/org/apache/polaris/service/it/test/PolarisRestCatalogIntegrationTest.java:
##########
@@ -1194,4 +1203,143 @@ public void testLoadCredentials() {
       assertThat(response).returns(Response.Status.OK.getStatusCode(), 
Response::getStatus);
     }
   }
+
+  @Test
+  public void testCreateGenericTable() {
+    Namespace namespace = Namespace.of("ns1");
+    restCatalog.createNamespace(namespace);
+    TableIdentifier tableIdentifier = TableIdentifier.of(namespace, "tbl1");
+
+    GenericTable createResponse =
+        genericTableApi.createGenericTable(currentCatalogName, 
tableIdentifier, "format", Map.of());
+    Assertions.assertThat(createResponse.getFormat()).isEqualTo("format");
+
+    genericTableApi.purge(currentCatalogName, namespace);
+  }
+
+  @Test
+  public void testLoadGenericTable() {
+    Namespace namespace = Namespace.of("ns1");
+    restCatalog.createNamespace(namespace);
+    TableIdentifier tableIdentifier = TableIdentifier.of(namespace, "tbl1");
+
+    genericTableApi.createGenericTable(currentCatalogName, tableIdentifier, 
"format", Map.of());
+
+    GenericTable loadResponse =
+        genericTableApi.getGenericTable(currentCatalogName, tableIdentifier);
+    Assertions.assertThat(loadResponse.getFormat()).isEqualTo("format");
+
+    genericTableApi.purge(currentCatalogName, namespace);
+  }
+
+  @Test
+  public void testListGenericTables() {
+    Namespace namespace = Namespace.of("ns1");
+    restCatalog.createNamespace(namespace);
+    TableIdentifier tableIdentifier1 = TableIdentifier.of(namespace, "tbl1");
+    TableIdentifier tableIdentifier2 = TableIdentifier.of(namespace, "tbl2");
+
+    genericTableApi.createGenericTable(currentCatalogName, tableIdentifier1, 
"format", Map.of());
+    genericTableApi.createGenericTable(currentCatalogName, tableIdentifier2, 
"format", Map.of());
+
+    List<TableIdentifier> identifiers =
+        genericTableApi.listGenericTables(currentCatalogName, namespace);
+
+    Assertions.assertThat(identifiers).hasSize(2);
+    Assertions.assertThat(identifiers)
+        .containsExactlyInAnyOrder(tableIdentifier1, tableIdentifier2);
+
+    genericTableApi.purge(currentCatalogName, namespace);
+  }
+
+  @Test
+  public void testDropGenericTable() {
+    Namespace namespace = Namespace.of("ns1");
+    restCatalog.createNamespace(namespace);
+    TableIdentifier tableIdentifier = TableIdentifier.of(namespace, "tbl1");
+
+    genericTableApi.createGenericTable(currentCatalogName, tableIdentifier, 
"format", Map.of());
+
+    GenericTable loadResponse1 =

Review Comment:
   nit: probably just `loadResponse`



##########
service/common/src/main/java/org/apache/polaris/service/catalog/common/CatalogAdapter.java:
##########
@@ -0,0 +1,34 @@
+/*
+ * 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.polaris.service.catalog.common;
+
+import java.net.URLEncoder;
+import java.nio.charset.Charset;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.rest.RESTUtil;
+
+/**
+ * A common interface for adapters between the REST interface and {@link 
CatalogHandler}
+ * implementations
+ */
+public interface CatalogAdapter {

Review Comment:
   I'd also prefer to move it to a common util class,
   1. It isn't needed in the inheritance chain. 
   2. GenericTable, iceberg, policy all need it as a common static method. 



##########
service/common/src/main/java/org/apache/polaris/service/catalog/common/CatalogHandler.java:
##########
@@ -366,4 +349,24 @@ protected void authorizeRenameTableLikeOperationOrThrow(
 
     initializeCatalog();
   }
+
+  /**
+   * Helper function for when a TABLE_LIKE entity is not found so we want to 
throw the appropriate
+   * exception. Used in Iceberg APIs, so the Iceberg messages cannot be 
changed.
+   *
+   * @param subType The subtype of the entity that the exception should report 
doesn't exist
+   */
+  public static void throwNotFoundExceptionForTableLikeEntity(
+      TableIdentifier identifier, PolarisEntitySubType subType) {
+    switch (subType) {
+      case ICEBERG_TABLE:
+        throw new NoSuchTableException("Table does not exist: %s", identifier);
+      case ICEBERG_VIEW:
+        throw new NoSuchViewException("View does not exist: %s", identifier);
+      case GENERIC_TABLE:
+        throw new NoSuchTableException("Generic table does not exist: %s", 
identifier);
+      default:
+        throw new NoSuchTableException("Table does not exist: %s", subType);

Review Comment:
   Minor: something like this?
   ```suggestion
           throw new NoSuchTableException("Table with type %s does not exist: 
%s", subType, identifier);
   ```



##########
service/common/src/main/java/org/apache/polaris/service/admin/PolarisAdminService.java:
##########
@@ -468,22 +467,30 @@ private void authorizeGrantOnTableLikeOperationOrThrow(
         catalogRoleName);
     ResolverStatus status = resolutionManifest.resolveAll();
 
+    final PolarisEntitySubType searchSubtype;
+    if (subTypes.size() > 1) {
+      searchSubtype = PolarisEntitySubType.ANY_SUBTYPE;
+    } else {
+      searchSubtype = subTypes.getFirst();
+    }
+
     if (status.getStatus() == 
ResolverStatus.StatusEnum.ENTITY_COULD_NOT_BE_RESOLVED) {
       throw new NotFoundException("Catalog not found: %s", catalogName);
     } else if (status.getStatus() == 
ResolverStatus.StatusEnum.PATH_COULD_NOT_BE_FULLY_RESOLVED) {
       if (status.getFailedToResolvePath().getLastEntityType() == 
PolarisEntityType.TABLE_LIKE) {
-        if (subType == PolarisEntitySubType.ICEBERG_TABLE) {
-          throw new NoSuchTableException("Table does not exist: %s", 
identifier);
-        } else {
-          throw new NoSuchViewException("View does not exist: %s", identifier);
-        }
+        CatalogHandler.throwNotFoundExceptionForTableLikeEntity(identifier, 
searchSubtype);
       } else {
         throw new NotFoundException("CatalogRole not found: %s.%s", 
catalogName, catalogRoleName);
       }
     }
 
     PolarisResolvedPathWrapper tableLikeWrapper =
-        resolutionManifest.getResolvedPath(identifier, 
PolarisEntityType.TABLE_LIKE, subType, true);
+        resolutionManifest.getResolvedPath(
+            identifier, PolarisEntityType.TABLE_LIKE, searchSubtype, true);

Review Comment:
   I'd recommend to add a override of `resolutionManifest.getResolvedPath()` to 
take a list of subtypes.



##########
service/common/src/main/java/org/apache/polaris/service/catalog/generic/GenericTableCatalogAdapter.java:
##########
@@ -19,22 +19,81 @@
 package org.apache.polaris.service.catalog.generic;
 
 import jakarta.enterprise.context.RequestScoped;
+import jakarta.inject.Inject;
 import jakarta.ws.rs.core.Response;
 import jakarta.ws.rs.core.SecurityContext;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.exceptions.NotAuthorizedException;
+import org.apache.polaris.core.auth.AuthenticatedPolarisPrincipal;
+import org.apache.polaris.core.auth.PolarisAuthorizer;
+import org.apache.polaris.core.context.CallContext;
 import org.apache.polaris.core.context.RealmContext;
+import org.apache.polaris.core.persistence.PolarisEntityManager;
+import org.apache.polaris.core.persistence.PolarisMetaStoreManager;
 import 
org.apache.polaris.service.catalog.api.PolarisCatalogGenericTableApiService;
+import org.apache.polaris.service.catalog.common.CatalogAdapter;
 import org.apache.polaris.service.types.CreateGenericTableRequest;
+import org.apache.polaris.service.types.ListGenericTablesResponse;
+import org.apache.polaris.service.types.LoadGenericTableResponse;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 @RequestScoped
-public class GenericTableCatalogAdapter implements 
PolarisCatalogGenericTableApiService {
+public class GenericTableCatalogAdapter
+    implements PolarisCatalogGenericTableApiService, CatalogAdapter {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(GenericTableCatalogAdapter.class);
+
+  private final CallContext callContext;
+  private final PolarisEntityManager entityManager;
+  private final PolarisMetaStoreManager metaStoreManager;
+  private final PolarisAuthorizer polarisAuthorizer;
+
+  @Inject
+  public GenericTableCatalogAdapter(
+      CallContext callContext,
+      PolarisEntityManager entityManager,
+      PolarisMetaStoreManager metaStoreManager,
+      PolarisAuthorizer polarisAuthorizer) {
+    this.callContext = callContext;
+    this.entityManager = entityManager;
+    this.metaStoreManager = metaStoreManager;
+    this.polarisAuthorizer = polarisAuthorizer;
+  }
+
+  private GenericTableCatalogHandler newHandlerWrapper(
+      SecurityContext securityContext, String catalogName) {
+    AuthenticatedPolarisPrincipal authenticatedPrincipal =
+        (AuthenticatedPolarisPrincipal) securityContext.getUserPrincipal();

Review Comment:
   nit: we could keep them in one line if we use `var`



-- 
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: issues-unsubscr...@polaris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to