rdblue commented on code in PR #7913:
URL: https://github.com/apache/iceberg/pull/7913#discussion_r1351025602


##########
core/src/main/java/org/apache/iceberg/rest/CatalogHandlers.java:
##########
@@ -374,4 +385,107 @@ static TableMetadata commit(TableOperations ops, 
UpdateTableRequest request) {
 
     return ops.current();
   }
+
+  public static BaseView baseView(View view) {
+    Preconditions.checkArgument(view instanceof BaseView, "View must be a 
BaseView");
+    return (BaseView) view;
+  }
+
+  public static ListTablesResponse listViews(ViewCatalog catalog, Namespace 
namespace) {
+    return 
ListTablesResponse.builder().addAll(catalog.listViews(namespace)).build();
+  }
+
+  public static LoadViewResponse createView(
+      ViewCatalog catalog, Namespace namespace, CreateViewRequest request) {
+    request.validate();
+
+    ViewMetadata viewMetadata = request.metadata();
+    ViewBuilder viewBuilder =
+        catalog
+            .buildView(TableIdentifier.of(namespace, request.name()))
+            .withSchema(viewMetadata.schema())
+            .withProperties(viewMetadata.properties())
+            
.withDefaultNamespace(viewMetadata.currentVersion().defaultNamespace())
+            
.withDefaultCatalog(viewMetadata.currentVersion().defaultCatalog());
+    viewMetadata.currentVersion().representations().stream()
+        .filter(r -> r instanceof SQLViewRepresentation)
+        .map(r -> (SQLViewRepresentation) r)
+        .forEach(r -> viewBuilder.withQuery(r.dialect(), r.sql()));
+    View view = viewBuilder.create();
+
+    return viewResponse(view);
+  }
+
+  private static LoadViewResponse viewResponse(View view) {
+    ViewMetadata metadata = baseView(view).operations().current();
+    return ImmutableLoadViewResponse.builder()
+        .metadata(metadata)
+        .metadataLocation(metadata.metadataFileLocation())
+        .build();
+  }
+
+  public static LoadViewResponse loadView(ViewCatalog catalog, TableIdentifier 
viewIdentifier) {
+    View view = catalog.loadView(viewIdentifier);
+    return viewResponse(view);
+  }
+
+  public static LoadViewResponse updateView(
+      ViewCatalog catalog, TableIdentifier ident, UpdateTableRequest request) {
+    View view = catalog.loadView(ident);
+    ViewMetadata metadata = commit(baseView(view).operations(), request);
+
+    return ImmutableLoadViewResponse.builder()
+        .metadata(metadata)
+        .metadataLocation(metadata.metadataFileLocation())
+        .build();
+  }
+
+  public static void renameView(ViewCatalog catalog, RenameTableRequest 
request) {
+    catalog.renameView(request.source(), request.destination());
+  }
+
+  public static void dropView(ViewCatalog catalog, TableIdentifier 
viewIdentifier) {
+    boolean dropped = catalog.dropView(viewIdentifier);
+    if (!dropped) {
+      throw new NoSuchViewException("View does not exist: %s", viewIdentifier);
+    }
+  }
+
+  static ViewMetadata commit(ViewOperations ops, UpdateTableRequest request) {
+    AtomicBoolean isRetry = new AtomicBoolean(false);
+    try {
+      Tasks.foreach(ops)
+          .retry(COMMIT_NUM_RETRIES_DEFAULT)
+          .exponentialBackoff(
+              COMMIT_MIN_RETRY_WAIT_MS_DEFAULT,
+              COMMIT_MAX_RETRY_WAIT_MS_DEFAULT,
+              COMMIT_TOTAL_RETRY_TIME_MS_DEFAULT,
+              2.0 /* exponential */)
+          .onlyRetryOn(CommitFailedException.class)
+          .run(
+              taskOps -> {
+                ViewMetadata base = isRetry.get() ? taskOps.refresh() : 
taskOps.current();
+                isRetry.set(true);
+
+                // apply changes
+                ViewMetadata.Builder metadataBuilder = 
ViewMetadata.buildFrom(base);
+                request.updates().forEach(update -> 
update.applyTo(metadataBuilder));

Review Comment:
   I think we will need to support the AssertUUID requirement right? It also 
seems good to run them even if we expect there to be none. Otherwise, when we 
add a requirement we'll have to go back and find out why it doesn't actually 
work. And similarly, if someone sends a requirement that is not allowed for 
views (but can be parsed for tables) we need to attempt to run it so that we 
will throw the exception that it isn't supported.



-- 
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...@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to