Skip to content

Commit

Permalink
Core: Use HEAD request to check if view exists (#11760)
Browse files Browse the repository at this point in the history
  • Loading branch information
nastra authored Dec 12, 2024
1 parent 6c05f35 commit 3053540
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,12 @@ private static LoadViewResponse viewResponse(View view) {
.build();
}

public static void viewExists(ViewCatalog catalog, TableIdentifier viewIdentifier) {
if (!catalog.viewExists(viewIdentifier)) {
throw new NoSuchViewException("View does not exist: %s", viewIdentifier);
}
}

public static LoadViewResponse loadView(ViewCatalog catalog, TableIdentifier viewIdentifier) {
View view = catalog.loadView(viewIdentifier);
return viewResponse(view);
Expand Down
12 changes: 12 additions & 0 deletions core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,18 @@ public List<TableIdentifier> listViews(SessionContext context, Namespace namespa
return views.build();
}

@Override
public boolean viewExists(SessionContext context, TableIdentifier identifier) {
checkViewIdentifierIsValid(identifier);

try {
client.head(paths.view(identifier), headers(context), ErrorHandlers.viewErrorHandler());
return true;
} catch (NoSuchViewException e) {
return false;
}
}

@Override
public View loadView(SessionContext context, TableIdentifier identifier) {
Endpoint.check(
Expand Down
10 changes: 10 additions & 0 deletions core/src/test/java/org/apache/iceberg/rest/RESTCatalogAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ enum Route {
CommitTransactionRequest.class,
null),
LIST_VIEWS(HTTPMethod.GET, ResourcePaths.V1_VIEWS, null, ListTablesResponse.class),
VIEW_EXISTS(HTTPMethod.HEAD, ResourcePaths.V1_VIEW),
LOAD_VIEW(HTTPMethod.GET, ResourcePaths.V1_VIEW, null, LoadViewResponse.class),
CREATE_VIEW(
HTTPMethod.POST, ResourcePaths.V1_VIEWS, CreateViewRequest.class, LoadViewResponse.class),
Expand Down Expand Up @@ -471,6 +472,15 @@ public <T extends RESTResponse> T handleRequest(
break;
}

case VIEW_EXISTS:
{
if (null != asViewCatalog) {
CatalogHandlers.viewExists(asViewCatalog, viewIdentFromPathVars(vars));
return null;
}
break;
}

case LOAD_VIEW:
{
if (null != asViewCatalog) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.eclipse.jetty.servlet.ServletHolder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand Down Expand Up @@ -226,6 +227,37 @@ public void testPaginationForListViews(int numberOfItems) {
eq(ListTablesResponse.class));
}

@Test
public void viewExistsViaHEADRequest() {
RESTCatalogAdapter adapter = Mockito.spy(new RESTCatalogAdapter(backendCatalog));
RESTCatalog catalog =
new RESTCatalog(SessionCatalog.SessionContext.createEmpty(), (config) -> adapter);
catalog.initialize("test", ImmutableMap.of());

catalog.createNamespace(Namespace.of("ns"));

assertThat(catalog.viewExists(TableIdentifier.of("ns", "view"))).isFalse();

Mockito.verify(adapter)
.execute(
eq(HTTPMethod.GET),
eq("v1/config"),
any(),
any(),
eq(ConfigResponse.class),
any(),
any());
Mockito.verify(adapter)
.execute(
eq(HTTPMethod.HEAD),
eq("v1/namespaces/ns/views/view"),
any(),
any(),
any(),
any(),
any());
}

@Override
protected RESTCatalog catalog() {
return restCatalog;
Expand Down

0 comments on commit 3053540

Please sign in to comment.