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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.lang3.tuple.Pair;

public class KeyMapper {
private final CatalogRepository catalogRepository;
Expand Down Expand Up @@ -63,22 +64,37 @@ public Map<SecurableType, Object> mapResourceKeys(Map<SecurableType, Object> res
}

// If only TABLE is specified, assuming its value is a full table name (including catalog and
// schema)
// schema) or a table UUID.
if (!resourceKeys.containsKey(CATALOG)
&& !resourceKeys.containsKey(SCHEMA)
&& resourceKeys.containsKey(TABLE)) {
String fullName = (String) resourceKeys.get(TABLE);
// If the full name contains a dot, we assume it's a full name, otherwise we assume it's an id
TableInfo table =
fullName.contains(".")
? tableRepository.getTable(fullName)
: tableRepository.getTableById(fullName);
String fullSchemaName = table.getCatalogName() + "." + table.getSchemaName();
SchemaInfo schema = schemaRepository.getSchema(fullSchemaName);
CatalogInfo catalog = catalogRepository.getCatalog(table.getCatalogName());
resourceIds.put(TABLE, UUID.fromString(table.getTableId()));
resourceIds.put(SCHEMA, UUID.fromString(schema.getSchemaId()));
resourceIds.put(CATALOG, UUID.fromString(catalog.getId()));
boolean isTableName = fullName.contains(".");

UUID catalogId;
UUID schemaId;
UUID tableId;
if (isTableName) {
TableInfo table = tableRepository.getTable(fullName);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 72-78 is basically doing the same thing as old code line 67,69-74.

String fullSchemaName = table.getCatalogName() + "." + table.getSchemaName();
SchemaInfo schema = schemaRepository.getSchema(fullSchemaName);
CatalogInfo catalog = catalogRepository.getCatalog(table.getCatalogName());
catalogId = UUID.fromString(catalog.getId());
schemaId = UUID.fromString(schema.getSchemaId());
tableId = UUID.fromString(table.getTableId());
} else {
// It may be the ID of either a table or staging table.
tableId = UUID.fromString(fullName);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block goes to the new function to get catalog&schema ids if the fullName is considered a table ID.

Pair<UUID, UUID> catalogAndSchemaIds =
tableRepository.getCatalogSchemaIdsByTableOrStagingTableId(tableId);
catalogId = catalogAndSchemaIds.getLeft();
schemaId = catalogAndSchemaIds.getRight();
}

resourceIds.put(TABLE, tableId);
resourceIds.put(SCHEMA, schemaId);
resourceIds.put(CATALOG, catalogId);
}

if (resourceKeys.containsKey(CATALOG)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,6 @@ private void addNamespaceData(FunctionInfo functionInfo, String catalogName, Str
functionInfo.setFullName(catalogName + "." + schemaName + "." + functionInfo.getName());
}

public UUID getSchemaId(Session session, String catalogName, String schemaName) {
SchemaInfoDAO schemaInfo =
repositories.getSchemaRepository().getSchemaDAO(session, catalogName, schemaName);
if (schemaInfo == null) {
throw new BaseException(ErrorCode.NOT_FOUND, "Schema not found: " + schemaName);
}
return schemaInfo.getId();
}

/**
* Return the list of functions in ascending order of function name.
*
Expand All @@ -147,7 +138,8 @@ public ListFunctionsResponse listFunctions(
return TransactionManager.executeWithTransaction(
sessionFactory,
session -> {
UUID schemaId = getSchemaId(session, catalogName, schemaName);
UUID schemaId =
repositories.getSchemaRepository().getSchemaId(session, catalogName, schemaName);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: not in this PR, but this seems like a repeated code pattern in many repos that can deduped by having an interface like SchemaUtils that every repo extends to get a simple function getSchemaId(...).

can you make this a issue and mark it as a starter task label so that others can pick it up?

@yili-db yili-db Nov 6, 2025 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #1165

return listFunctions(session, schemaId, catalogName, schemaName, maxResults, pageToken);
},
"Failed to list functions",
Expand Down Expand Up @@ -205,7 +197,8 @@ public void addNamespaceInfo(FunctionInfo functionInfo, String catalogName, Stri

public FunctionInfoDAO getFunctionDAO(
Session session, String catalogName, String schemaName, String functionName) {
UUID schemaId = getSchemaId(session, catalogName, schemaName);
UUID schemaId =
repositories.getSchemaRepository().getSchemaId(session, catalogName, schemaName);
return getFunctionDAO(session, schemaId, functionName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ public RegisteredModelInfo getRegisteredModel(String fullName) {

private RegisteredModelInfoDAO findRegisteredModel(
Session session, String catalogName, String schemaName, String registeredModelName) {
UUID schemaId = getSchemaId(session, catalogName, schemaName);
UUID schemaId =
repositories.getSchemaRepository().getSchemaId(session, catalogName, schemaName);
return getRegisteredModelDao(session, schemaId, registeredModelName);
}

Expand Down Expand Up @@ -211,8 +212,9 @@ public RegisteredModelInfo createRegisteredModel(CreateRegisteredModel createReg
tx = session.beginTransaction();
String catalogName = registeredModelInfo.getCatalogName();
String schemaName = registeredModelInfo.getSchemaName();
UUID schemaId = getSchemaId(session, catalogName, schemaName);
UUID catalogId = getCatalogId(session, catalogName);
UUID schemaId =
repositories.getSchemaRepository().getSchemaId(session, catalogName, schemaName);
UUID catalogId = repositories.getCatalogRepository().getCatalogId(session, catalogName);
String storageLocation =
fileOperations.getModelStorageLocation(
catalogId.toString(), schemaId.toString(), modelId);
Expand Down Expand Up @@ -306,7 +308,10 @@ public ListRegisteredModelsResponse listRegisteredModels(
.nextPageToken(nextPageToken);
} else {
LOGGER.info("Listing registered models in {}.{}", catalogName.get(), schemaName.get());
UUID schemaId = getSchemaId(session, catalogName.get(), schemaName.get());
UUID schemaId =
repositories
.getSchemaRepository()
.getSchemaId(session, catalogName.get(), schemaName.get());
response =
listRegisteredModels(
session, schemaId, catalogName.get(), schemaName.get(), maxResults, pageToken);
Expand Down Expand Up @@ -431,7 +436,8 @@ public void deleteRegisteredModel(String fullName, boolean force) {
String schemaName = parts[1];
String registeredModelName = parts[2];
try {
UUID schemaId = getSchemaId(session, catalogName, schemaName);
UUID schemaId =
repositories.getSchemaRepository().getSchemaId(session, catalogName, schemaName);
deleteRegisteredModel(session, schemaId, registeredModelName, force);
tx.commit();
} catch (RuntimeException e) {
Expand Down Expand Up @@ -536,8 +542,9 @@ public ModelVersionInfo createModelVersion(CreateModelVersion createModelVersion
Transaction tx;
try (Session session = sessionFactory.openSession()) {
tx = session.beginTransaction();
UUID catalogId = getCatalogId(session, catalogName);
UUID schemaId = getSchemaId(session, catalogName, schemaName);
UUID catalogId = repositories.getCatalogRepository().getCatalogId(session, catalogName);
UUID schemaId =
repositories.getSchemaRepository().getSchemaId(session, catalogName, schemaName);
String storageLocation = "";
try {
// Check if registered model already exists
Expand Down Expand Up @@ -621,7 +628,8 @@ public ListModelVersionsResponse listModelVersions(
String catalogName = parts[0];
String schemaName = parts[1];
String registeredModelName = parts[2];
UUID schemaId = getSchemaId(session, catalogName, schemaName);
UUID schemaId =
repositories.getSchemaRepository().getSchemaId(session, catalogName, schemaName);
RegisteredModelInfoDAO existingRegisteredModel =
getRegisteredModelDaoOrThrow(session, schemaId, registeredModelName);
UUID registeredModelId = existingRegisteredModel.getId();
Expand Down Expand Up @@ -683,7 +691,8 @@ public ModelVersionInfo updateModelVersion(
tx = session.beginTransaction();
try {
// Get the registered model record from the database
UUID schemaId = getSchemaId(session, catalogName, schemaName);
UUID schemaId =
repositories.getSchemaRepository().getSchemaId(session, catalogName, schemaName);
// Get the model version record from the database
ModelVersionInfoDAO origModelVersionInfoDAO =
getModelVersionDaoOrThrow(session, schemaId, fullName, registeredModelName, version);
Expand Down Expand Up @@ -726,7 +735,8 @@ public void deleteModelVersion(String fullName, Long version) {
try (Session session = sessionFactory.openSession()) {
Transaction tx = session.beginTransaction();
try {
UUID schemaId = getSchemaId(session, catalogName, schemaName);
UUID schemaId =
repositories.getSchemaRepository().getSchemaId(session, catalogName, schemaName);
RegisteredModelInfoDAO existingRegisteredModel =
getRegisteredModelDaoOrThrow(session, schemaId, registeredModelName);
deleteModelVersion(session, existingRegisteredModel.getId(), fullName, version);
Expand Down Expand Up @@ -774,7 +784,8 @@ public ModelVersionInfo finalizeModelVersion(FinalizeModelVersion finalizeModelV
tx = session.beginTransaction();
try {
// Get the registered model record from the database
UUID schemaId = getSchemaId(session, catalogName, schemaName);
UUID schemaId =
repositories.getSchemaRepository().getSchemaId(session, catalogName, schemaName);
ModelVersionInfoDAO origModelVersionInfoDAO =
getModelVersionDaoOrThrow(session, schemaId, fullName, registeredModelName, version);

Expand Down Expand Up @@ -809,22 +820,4 @@ public ModelVersionInfo finalizeModelVersion(FinalizeModelVersion finalizeModelV
}
return modelVersionInfo;
}

public UUID getSchemaId(Session session, String catalogName, String schemaName) {
SchemaInfoDAO schemaInfo =
repositories.getSchemaRepository().getSchemaDAO(session, catalogName, schemaName);
if (schemaInfo == null) {
throw new BaseException(ErrorCode.NOT_FOUND, "Schema not found: " + schemaName);
}
return schemaInfo.getId();
}

public UUID getCatalogId(Session session, String catalogName) {
CatalogInfoDAO catalogInfo =
repositories.getCatalogRepository().getCatalogDAO(session, catalogName);
if (catalogInfo == null) {
throw new BaseException(ErrorCode.NOT_FOUND, "Catalog not found: " + catalogName);
}
return catalogInfo.getId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Repositories(SessionFactory sessionFactory, ServerProperties serverProper

this.catalogRepository = new CatalogRepository(this, sessionFactory);
this.schemaRepository = new SchemaRepository(this, sessionFactory);
this.tableRepository = new TableRepository(this, sessionFactory);
this.tableRepository = new TableRepository(this, sessionFactory, serverProperties);
this.stagingTableRepository =
new StagingTableRepository(this, sessionFactory, serverProperties);
this.volumeRepository = new VolumeRepository(this, sessionFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import io.unitycatalog.server.utils.IdentityUtils;
import io.unitycatalog.server.utils.ServerProperties;
import io.unitycatalog.server.utils.ValidationUtils;
import java.util.Date;
import java.util.Objects;
import java.util.UUID;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
Expand Down Expand Up @@ -125,4 +127,49 @@ public StagingTableInfo createStagingTable(CreateStagingTable createStagingTable
"Error creating table: " + getTableFullName(createStagingTable),
/* readOnly = */ false);
}

/**
* Commits a staging table by marking it as finalized and associating it with a permanent table.
*
* <p>This method is called by TableRepository.createTable when a table is created using a staging
* table location. It validates that the caller is the owner of the staging table and that the
* staging table has not been previously committed.
*
* @param session the Hibernate session for database operations
* @param callerId the identifier of the user attempting to commit the staging table
* @param storageLocation the storage location URI of the staging table to commit
* @return StagingTableDAO the committed staging table data access object
* @throws BaseException with ErrorCode.NOT_FOUND if no staging table exists at the specified
* location
* @throws BaseException with ErrorCode.PERMISSION_DENIED if the caller is not the owner of the
* staging table
* @throws BaseException with ErrorCode.FAILED_PRECONDITION if the staging table has already been
* committed
*/
public StagingTableDAO commitStagingTable(
Session session, String callerId, String storageLocation) {
serverProperties.checkManagedTableEnabled();
StagingTableDAO stagingTableDAO = findByStagingLocation(session, storageLocation);
if (stagingTableDAO == null) {
throw new BaseException(ErrorCode.NOT_FOUND, "Staging table not found: " + storageLocation);
}
if (!Objects.equals(stagingTableDAO.getCreatedBy(), callerId)) {
throw new BaseException(
ErrorCode.PERMISSION_DENIED,
"User attempts to create table on a staging location without ownership: "
+ storageLocation);
}
if (stagingTableDAO.isStageCommitted()) {
throw new BaseException(
ErrorCode.FAILED_PRECONDITION, "Staging table already committed: " + storageLocation);
}

// Commit the staging table
Date now = new Date();
stagingTableDAO.setStageCommitted(true);
stagingTableDAO.setStageCommittedAt(now);
stagingTableDAO.setAccessedAt(now);
session.merge(stagingTableDAO);
return stagingTableDAO;
}
}
Loading