Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/Models/DeltaGetCommitsResponse.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

| Name | Type | Description | Notes |
|------------ | ------------- | ------------- | -------------|
| **commits** | [**List**](DeltaCommitInfo.md) | The list of unbackfilled Delta table commits. Can be in arbitrary order. | [optional] [default to null] |
| **latest\_table\_version** | **Long** | Represents the latest version of the table tracked by UC. If no commits have occurred via UC yet, then UC cannot determine the latest version and returns -1. Use this field to manage pagination — if the returned commits don't cover the range up to latest_table_version or end_version (whichever is smaller), it indicates that more unbackfilled commits may be available. | [optional] [default to null] |
| **commits** | [**List**](DeltaCommitInfo.md) | The list of unbackfilled Delta table commits. Can be in arbitrary order. | [default to null] |
| **latest\_table\_version** | **Long** | Represents the latest version of the table tracked by UC. If no commits have occurred via UC yet, then UC cannot determine the latest version and returns -1. Use this field to manage pagination — if the returned commits don't cover the range up to latest_table_version or end_version (whichever is smaller), it indicates that more unbackfilled commits may be available. | [default to null] |

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

3 changes: 3 additions & 0 deletions api/all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3025,6 +3025,9 @@ components:
then UC cannot determine the latest version and returns -1. Use this field to manage pagination —
if the returned commits don't cover the range up to latest_table_version or end_version (whichever is smaller),
it indicates that more unbackfilled commits may be available.
required:
- commits
- latest_table_version
info:
title: Unity Catalog API
version: '0.1'
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import io.unitycatalog.server.model.DeltaCommit;
import io.unitycatalog.server.model.DeltaCommitInfo;
import io.unitycatalog.server.model.DeltaCommitMetadataProperties;
import io.unitycatalog.server.model.DeltaGetCommits;
import io.unitycatalog.server.model.DeltaGetCommitsResponse;
import io.unitycatalog.server.model.DeltaMetadata;
import io.unitycatalog.server.model.TableType;
import io.unitycatalog.server.persist.dao.ColumnInfoDAO;
Expand All @@ -26,6 +28,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.NativeQuery;
Expand All @@ -48,8 +51,8 @@
* <li>commit(v1). Then database has [v1]
* <li>commit(v2). Then database has [v1, v2]
* <li>commit(v3). Then database has [v1, v2, v3]
* <li>backfill(v1). Then database has [v2, v3]. Any version <= v1 is removed.
* <li>commit(v4) & backfill(v3). Then database has [v4]. Any version <= v3 is removed.
* <li>backfill(v1). Then database has [v2, v3]. Any version &lt;= v1 is removed.
* <li>commit(v4) and backfill(v3). Then database has [v4]. Any version &lt;= v3 is removed.
* <li>commit (v5). Then database has [v4, v5]
* <li>backfill(v5). Then database has [v5(is_backfilled_latest_commit=true)]. v4 is removed. But
* v5 has to be kept as the last record in database.
Expand Down Expand Up @@ -87,6 +90,117 @@ public DeltaCommitRepository(SessionFactory sessionFactory, ServerProperties ser
this.serverProperties = serverProperties;
}

/**
* Retrieves all commits for a table in descending order by version up to NUM_COMMITS_PER_BATCH.
*/
private List<DeltaCommitDAO> getAllCommitDAOsDesc(UUID tableId) {
return TransactionManager.executeWithTransaction(
sessionFactory,
session -> {
TableInfoDAO tableInfoDAO = session.get(TableInfoDAO.class, tableId);
if (tableInfoDAO == null) {
throw new BaseException(ErrorCode.NOT_FOUND, "Table not found: " + tableId);
}
validateTable(tableInfoDAO);

Query<DeltaCommitDAO> query =
session.createQuery(
"FROM DeltaCommitDAO WHERE tableId = :tableId ORDER BY commitVersion DESC",
DeltaCommitDAO.class);
query.setParameter("tableId", tableId);
query.setMaxResults(NUM_COMMITS_PER_BATCH);
return query.list();
},
"Failed to get latest commits",
/* readOnly= */ true);
}

/**
* Retrieves commits for a managed Delta table within a specified version range.
*
* <p>This method returns commits that fall within the requested version range [startVersion,
* endVersion]. The response includes both the list of commits and the latest table version.
*
* <p><b>Pagination:</b> Results may be further reduced to MAX_NUM_COMMITS_PER_TABLE commits per
* request if they exceed. Earlier (lower version) commits in the version range of [startVersion,
* endVersion] will be kept and later (higher version) commits in the version range will be
* trimmed in order to keep the result within the limit.
*
* <p><b>Backfilled commits:</b> When commits are backfilled (persisted to file storage), they are
* removed from the database. If the latest commit is marked as backfilled, this method returns an
* empty commit list but still returns the correct latestTableVersion.
*
* <p><b>Empty table behavior:</b> If the table has no commits yet, returns latestTableVersion=-1
* with an empty commit list.
*
* <p>The returned commits are ordered by version in descending order (newest first).
*/
public DeltaGetCommitsResponse getCommits(DeltaGetCommits rpc) {
serverProperties.checkManagedTableEnabled();

ValidationUtils.checkArgument(rpc.getTableId() != null, "Field can not be null: table_id");
ValidationUtils.checkArgument(
rpc.getStartVersion() != null, "Field can not be null: start_version");
UUID tableId = UUID.fromString(rpc.getTableId());
long startVersion = rpc.getStartVersion();
Optional<Long> endVersion = Optional.ofNullable(rpc.getEndVersion());
ValidationUtils.checkArgument(startVersion >= 0, "Field must be >=0: start_version");
ValidationUtils.checkArgument(
endVersion.filter(x -> x < startVersion).isEmpty(),
"end_version must be >=start_version if set");

List<DeltaCommitDAO> allCommitDAOsDesc = getAllCommitDAOsDesc(tableId);
int commitCount = allCommitDAOsDesc.size();
if (commitCount > MAX_NUM_COMMITS_PER_TABLE) {
// This should never occur. But this is recoverable and not fatal.
LOGGER.error(
"Table {} has {} commits, which exceeds the limit of {}.",
tableId,
commitCount,
MAX_NUM_COMMITS_PER_TABLE);
}
if (commitCount == 0) {
// No commit exist yet. Not even any backfilled one. That means the table has never sent any
// commit coordinated by UC.
return new DeltaGetCommitsResponse().latestTableVersion(-1L);
}

// In case there's only one commit in database, firstCommitDAO and lastCommitDAO will be the
// same object.
DeltaCommitDAO firstCommitDAO = allCommitDAOsDesc.get(allCommitDAOsDesc.size() - 1);
DeltaCommitDAO lastCommitDAO = allCommitDAOsDesc.get(0);
assert firstCommitDAO.getCommitVersion() <= lastCommitDAO.getCommitVersion();
if (lastCommitDAO.isBackfilledLatestCommit()) {
// The last commit is already backfilled. Just return an empty list. No need to return any
// actual commits.
return new DeltaGetCommitsResponse().latestTableVersion(lastCommitDAO.getCommitVersion());
}

// The last version to return if endVersion is not set. It's limited by pagination limit.
// In normal cases the pagination limitation should not happen at all since the limit is the
// same limit that a table can have as many unbackfilled commits as possible. But it is
// implemented this way just in case.
long paginatedEndVersionInclusive =
Math.max(startVersion, firstCommitDAO.getCommitVersion()) + MAX_NUM_COMMITS_PER_TABLE - 1;
// The actual last version to return
long effectiveEndVersionInclusive =
Math.min(endVersion.orElse(Long.MAX_VALUE), paginatedEndVersionInclusive);

// Filter result and return
List<DeltaCommitInfo> commits =
allCommitDAOsDesc.stream()
.filter(
c ->
!c.isBackfilledLatestCommit()
&& c.getCommitVersion() >= startVersion
&& c.getCommitVersion() <= effectiveEndVersionInclusive)
.map(DeltaCommitDAO::toCommitInfo)
.collect(Collectors.toList());
return new DeltaGetCommitsResponse()
.commits(commits)
.latestTableVersion(lastCommitDAO.getCommitVersion());
}

/**
* Commits a new version to a managed Delta table with coordinated commit semantics.
*
Expand Down Expand Up @@ -681,23 +795,25 @@ private static void validateCommit(DeltaCommit commit) {
ErrorCode.INVALID_ARGUMENT,
"At least one of description, properties, or schema must be set in commit.metadata");
}
Optional<String> propertiesTableIdOpt = propertiesOpt.map(p -> p.get("ucTableId"));
if (propertiesTableIdOpt.isEmpty()) {
throw new BaseException(
ErrorCode.INVALID_ARGUMENT, "commit does not contain ucTableId in the properties.");
}
if (!propertiesTableIdOpt.get().equals(commit.getTableId())) {
// This is to ensure that the Delta table's log on the file system has the table id stored
// as a property. An extra check to ensure that the filesystem-based information and the
// catalog-based information is in sync. for example, if some buggy connector accidentally
// updated table A on the file system, but send the commit to table B in UC, then this
// check will catch it as table A's properties in the log will have a different id than
// the table B's id in UC.
throw new BaseException(
ErrorCode.INVALID_ARGUMENT,
String.format(
"the table being committed (%s) does not match the properties ucTableId(%s).",
commit.getTableId(), propertiesTableIdOpt.get()));
if (propertiesOpt.isPresent()) {
Optional<String> propertiesTableIdOpt = propertiesOpt.map(p -> p.get("ucTableId"));
if (propertiesTableIdOpt.isEmpty()) {
throw new BaseException(
ErrorCode.INVALID_ARGUMENT, "commit does not contain ucTableId in the properties.");
}
if (!propertiesTableIdOpt.get().equals(commit.getTableId())) {
// This is to ensure that the Delta table's log on the file system has the table id
// stored as a property. An extra check to ensure that the filesystem-based information
// and the catalog-based information is in sync. for example, if some buggy connector
// accidentally updated table A on the file system, but send the commit to table B in
// UC, then this check will catch it as table A's properties in the log will have a
// different id than the table B's id in UC.
throw new BaseException(
ErrorCode.INVALID_ARGUMENT,
String.format(
"the table being committed (%s) does not match the properties ucTableId(%s).",
commit.getTableId(), propertiesTableIdOpt.get()));
}
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ public class DeltaCommitDAO {
@Column(name = "is_backfilled_latest_commit", nullable = false)
private boolean isBackfilledLatestCommit;

public DeltaCommitInfo toCommitInfo() {
return new DeltaCommitInfo()
.version(commitVersion)
.fileName(commitFilename)
.fileSize(commitFilesize)
.fileModificationTimestamp(commitFileModificationTimestamp.getTime())
.timestamp(commitTimestamp.getTime());
}

public static DeltaCommitDAO from(UUID tableId, DeltaCommitInfo commitInfo) {
return DeltaCommitDAO.builder()
.tableId(tableId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.HttpStatus;
import com.linecorp.armeria.server.annotation.ExceptionHandler;
import com.linecorp.armeria.server.annotation.Get;
import com.linecorp.armeria.server.annotation.Post;
import io.unitycatalog.server.auth.UnityCatalogAuthorizer;
import io.unitycatalog.server.auth.annotation.AuthorizeExpression;
import io.unitycatalog.server.auth.annotation.AuthorizeKey;
import io.unitycatalog.server.auth.annotation.AuthorizeKeys;
import io.unitycatalog.server.exception.GlobalExceptionHandler;
import io.unitycatalog.server.model.DeltaCommit;
import io.unitycatalog.server.model.DeltaGetCommits;
import io.unitycatalog.server.persist.DeltaCommitRepository;
import io.unitycatalog.server.persist.Repositories;
import lombok.SneakyThrows;
Expand Down Expand Up @@ -45,5 +47,16 @@ public HttpResponse postCommit(
return HttpResponse.of(HttpStatus.OK);
}

// TODO: implement GET
@Get("")
@AuthorizeExpression("""
#authorizeAny(#principal, #schema, OWNER, USE_SCHEMA) &&
#authorizeAny(#principal, #catalog, OWNER, USE_CATALOG) &&
#authorizeAny(#principal, #table, OWNER, SELECT)
""")
@AuthorizeKey(METASTORE)
public HttpResponse getCommits(
@AuthorizeKeys({@AuthorizeKey(value = TABLE, key = "table_id")})
DeltaGetCommits rpc) {
return HttpResponse.ofJson(deltaCommitRepository.getCommits(rpc));
}
}
Loading