-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[Storage] Implement getCommits in UC Delta token-based client #6814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
153b88a
473ef06
9b9b931
d6167e4
ee014c8
d246102
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
|
|
||
| import io.delta.storage.commit.Commit; | ||
| import io.delta.storage.commit.CommitFailedException; | ||
| import io.delta.storage.commit.CoordinatedCommitsUtils; | ||
| import io.delta.storage.commit.GetCommitsResponse; | ||
| import io.delta.storage.commit.TableIdentifier; | ||
| import io.delta.storage.commit.actions.AbstractMetadata; | ||
|
|
@@ -60,6 +61,7 @@ | |
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
|
|
@@ -70,6 +72,8 @@ | |
| import java.util.function.Supplier; | ||
|
|
||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.fs.FileStatus; | ||
| import org.apache.hadoop.fs.Path; | ||
|
|
||
| /** | ||
| * A REST client implementation of {@link UCDeltaClient} that uses the UC Delta REST API for | ||
|
|
@@ -233,7 +237,7 @@ public void commit( | |
| throws IOException, CommitFailedException, UCCommitCoordinatorException { | ||
| ensureOpen(); | ||
| Objects.requireNonNull(tableId, "tableId must not be null"); | ||
| Objects.requireNonNull(tableIdentifier, "tableIdentifier must not be null"); | ||
| ResolvedTableName name = requireThreePartName(tableIdentifier); | ||
|
|
||
| UpdateTableRequest request = new UpdateTableRequest(); | ||
| request.addRequirementsItem(new AssertTableUUID() | ||
|
|
@@ -266,14 +270,10 @@ public void commit( | |
| .protocol(toSDKDeltaProtocol(newProtocol.get()))); | ||
| } | ||
|
|
||
| String catalog = tableIdentifier.getNamespace()[0]; | ||
| String schema = tableIdentifier.getNamespace()[1]; | ||
| String table = tableIdentifier.getName(); | ||
|
|
||
| try { | ||
| deltaTablesApi.updateTable(catalog, schema, table, request); | ||
| deltaTablesApi.updateTable(name.catalog, name.schema, name.table, request); | ||
| } catch (ApiException e) { | ||
| handleUpdateTableException(e, catalog, schema, table); | ||
| handleUpdateTableException(e, name.catalog, name.schema, name.table); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -284,9 +284,75 @@ public GetCommitsResponse getCommits( | |
| TableIdentifier tableIdentifier, | ||
| Optional<Long> startVersion, | ||
| Optional<Long> endVersion) throws IOException, UCCommitCoordinatorException { | ||
| throw new UnsupportedOperationException( | ||
| "getCommits is not yet supported by UCDeltaTokenBasedRestClient. " + | ||
| "A separate PR will add this once the tableIdentifier mapping is available."); | ||
| ensureOpen(); | ||
| Objects.requireNonNull(tableId, "tableId must not be null"); | ||
| Objects.requireNonNull(tableUri, "tableUri must not be null"); | ||
| Objects.requireNonNull(startVersion, "startVersion must not be null"); | ||
| Objects.requireNonNull(endVersion, "endVersion must not be null"); | ||
|
|
||
| ResolvedTableName name = requireThreePartName(tableIdentifier); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto. |
||
|
|
||
| // The UC loadTable endpoint does not support server-side filtering by version range, so | ||
| // we fetch the full unbackfilled commit window and filter client-side below. The server | ||
| // bounds the window size, so this list is not unbounded in practice. | ||
| LoadTableResponse response; | ||
| try { | ||
| response = deltaTablesApi.loadTable(name.catalog, name.schema, name.table); | ||
| } catch (ApiException e) { | ||
| if (e.getCode() == HTTP_NOT_FOUND) { | ||
| throw new NoSuchTableException( | ||
| String.format("Table %s not found in Unity Catalog", name.fullName), e); | ||
| } | ||
| throw new IOException( | ||
| String.format("Failed to load commits for table %s (HTTP %s): %s", | ||
| name.fullName, e.getCode(), e.getResponseBody()), | ||
| e); | ||
| } | ||
|
|
||
| TableMetadata metadata = response.getMetadata(); | ||
| String actualTableId = metadata != null && metadata.getTableUuid() != null | ||
| ? metadata.getTableUuid().toString() | ||
| : null; | ||
| if (!tableId.equals(actualTableId)) { | ||
| throw new InvalidTargetTableException( | ||
| String.format( | ||
| "Table UUID mismatch for %s: expected %s but got %s", | ||
| name.fullName, | ||
| tableId, | ||
| actualTableId)); | ||
| } | ||
|
|
||
| Path basePath = CoordinatedCommitsUtils.commitDirPath( | ||
| CoordinatedCommitsUtils.logDirPath(new Path(tableUri))); | ||
| List<Commit> commits = new ArrayList<>(); | ||
| if (response.getCommits() != null) { | ||
| for (DeltaCommit deltaCommit : response.getCommits()) { | ||
| long version = deltaCommit.getVersion(); | ||
| if (startVersion.isPresent() && version < startVersion.get()) { | ||
| continue; | ||
| } | ||
| if (endVersion.isPresent() && version > endVersion.get()) { | ||
| continue; | ||
| } | ||
| commits.add(fromDeltaCommit(deltaCommit, basePath)); | ||
| } | ||
| } | ||
|
|
||
| long latestTableVersion = response.getLatestTableVersion() != null | ||
| ? response.getLatestTableVersion() : -1L; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will it be the case if the table does not even have a single table version ? |
||
| return new GetCommitsResponse(commits, latestTableVersion); | ||
| } | ||
|
|
||
| /** Converts a UC SDK {@link DeltaCommit} to a Delta {@link Commit}. */ | ||
| private Commit fromDeltaCommit(DeltaCommit deltaCommit, Path basePath) { | ||
| FileStatus fileStatus = new FileStatus( | ||
| deltaCommit.getFileSize(), | ||
| false /* isdir */, | ||
| 0 /* block_replication */, | ||
| 0 /* blocksize */, | ||
| deltaCommit.getFileModificationTimestamp(), | ||
| new Path(basePath, deltaCommit.getFileName())); | ||
| return new Commit(deltaCommit.getVersion(), fileStatus, deltaCommit.getTimestamp()); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -339,38 +405,30 @@ public void close() throws IOException { | |
| @Override | ||
| public TableInfo loadTable(TableIdentifier tableIdentifier) throws IOException { | ||
| ensureOpen(); | ||
| Objects.requireNonNull(tableIdentifier, "tableIdentifier must not be null"); | ||
| String[] namespace = tableIdentifier.getNamespace(); | ||
| if (namespace == null || namespace.length != 2) { | ||
| throw new IllegalArgumentException( | ||
| "UC tableIdentifier must have a 2-component namespace [catalog, schema]; got " + | ||
| (namespace == null ? "null" : java.util.Arrays.toString(namespace))); | ||
| } | ||
| String catalog = namespace[0]; | ||
| String schema = namespace[1]; | ||
| String table = tableIdentifier.getName(); | ||
| ResolvedTableName name = requireThreePartName(tableIdentifier); | ||
|
|
||
| try { | ||
| return toTableInfo( | ||
| deltaTablesApi.loadTable(catalog, schema, table), catalog, schema, table); | ||
| deltaTablesApi.loadTable(name.catalog, name.schema, name.table), | ||
| name.catalog, name.schema, name.table); | ||
| } catch (ApiException e) { | ||
| if (e.getCode() == HTTP_NOT_FOUND) { | ||
| throw new NoSuchTableException( | ||
| String.format("Table %s.%s.%s not found in Unity Catalog", catalog, schema, table), e); | ||
| String.format("Table %s not found in Unity Catalog", name.fullName), e); | ||
| } | ||
| // UC encodes non-Delta-format errors as HTTP 400 with error.type = | ||
| // "UnsupportedTableFormatException"; substring-match the body to avoid coupling to an | ||
| // ErrorResponse parser. | ||
| String body = e.getResponseBody(); | ||
| if (body != null && body.contains("UnsupportedTableFormatException")) { | ||
| throw new UnsupportedTableFormatException( | ||
| String.format("Table %s.%s.%s is not in Delta format; the Delta REST API cannot " | ||
| + "serve it. Body: %s", catalog, schema, table, body), | ||
| String.format("Table %s is not in Delta format; the Delta REST API cannot " | ||
| + "serve it. Body: %s", name.fullName, body), | ||
| e); | ||
| } | ||
| throw new IOException( | ||
| String.format("Failed to load table %s.%s.%s (HTTP %s): %s", | ||
| catalog, schema, table, e.getCode(), e.getResponseBody()), e); | ||
| String.format("Failed to load table %s (HTTP %s): %s", | ||
| name.fullName, e.getCode(), e.getResponseBody()), e); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -640,6 +698,25 @@ private void addMetadataUpdates( | |
| } | ||
| } | ||
|
|
||
| // =========================== | ||
| // Table Identifier Helpers | ||
| // =========================== | ||
|
|
||
| /** | ||
| * Validates that the given {@code tableIdentifier} is a Unity Catalog three-part | ||
| * (catalog.schema.table) name and returns its resolved parts. | ||
| */ | ||
| private static ResolvedTableName requireThreePartName(TableIdentifier tableIdentifier) { | ||
| Objects.requireNonNull(tableIdentifier, "tableIdentifier must not be null"); | ||
| String[] namespace = tableIdentifier.getNamespace(); | ||
| if (namespace == null || namespace.length != 2) { | ||
| throw new IllegalArgumentException( | ||
| "UC tableIdentifier must have a 2-component namespace [catalog, schema]; got " + | ||
| (namespace == null ? "null" : Arrays.toString(namespace))); | ||
| } | ||
| return new ResolvedTableName(namespace[0], namespace[1], tableIdentifier.getName()); | ||
| } | ||
|
|
||
| // =========================== | ||
| // Exception Handling | ||
| // =========================== | ||
|
|
@@ -673,6 +750,21 @@ private void handleUpdateTableException( | |
| // Inner Classes | ||
| // =========================== | ||
|
|
||
| /** A Unity Catalog three-part table name resolved from a {@link TableIdentifier}. */ | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is intentional kept inner - its a private impl detail |
||
| private static final class ResolvedTableName { | ||
| final String catalog; | ||
| final String schema; | ||
| final String table; | ||
| final String fullName; | ||
|
|
||
| ResolvedTableName(String catalog, String schema, String table) { | ||
| this.catalog = catalog; | ||
| this.schema = schema; | ||
| this.table = table; | ||
| this.fullName = catalog + "." + schema + "." + table; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Adapts a UC SDK {@link TableMetadata} to {@link AbstractMetadata}. | ||
| */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
name->resolvedTable.