Repository navigation
Add staging table creation support #1134
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
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 |
|---|---|---|
|
|
@@ -311,6 +311,8 @@ lazy val server = (project in file("server")) | |
| ) ++ javacRelease17, | ||
| libraryDependencies ++= Seq( | ||
| "com.linecorp.armeria" % "armeria" % "1.28.4", | ||
| "org.apache.commons" % "commons-lang3" % "3.19.0", | ||
|
Contributor
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. what is using this?
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 not new usage. But existing test is failing on main branch. I had another PR fixing it and this comment describes the failure point. |
||
|
|
||
| // Netty dependencies | ||
| "io.netty" % "netty-all" % "4.1.111.Final", | ||
| "jakarta.annotation" % "jakarta.annotation-api" % "3.0.0" % Provided, | ||
|
Contributor
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. there is no test for the new functionality in this PR?
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. The majority changes of this PR is in StagingTableDAO.java and StagingTableRepository.java. They are essentially dead code until the next PR is in and until a test actually turns them on with server.managed-table.enabled=true.
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. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,11 @@ server.redirect-port= | |
| # D-Days H-Hours M-Minutes S-Seconds (P5D = 5 days,PT5H = 5 hours, PT5M = 5 minutes, PT5S = 5 seconds) | ||
| server.cookie-timeout=P5D | ||
|
|
||
| ## Experimental Feature Flags | ||
| # Enable MANAGED table (experimental feature) | ||
| # Default: false (disabled) | ||
| server.managed-table.enabled=false | ||
|
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. if I remember correctly, for the catalog owned table seems like we are using the word
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. catalogOwned means using UC as commit coordinator.
Contributor
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. yes. exactly.
Contributor
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. actually... slight nuance.
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. So here can I assume that this type of table, using UC assigned storage path and using catalog as commit coordinator, will be advertised as MANAGED table as its brand name?
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. Okay, then Then here we defintely should use
Contributor
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. Absolutely right. "managed table" is user-facing UC-specific product concept, and catalogOwned is a internal implementation detail of how managed table implemented using delta's catalogOwned protocol. So for a UC server configuration, we should use "managed table" |
||
|
|
||
| # Define the model storage root. Cloud storage or file based allowed. | ||
| # If no root specified, the current working directory of the server is used. | ||
|
|
||
|
|
@@ -23,6 +28,15 @@ server.cookie-timeout=P5D | |
| #storage-root.models=gs://my-gc-bucket/root | ||
| storage-root.models=file:/tmp/ucroot | ||
|
|
||
| # Define the managed table storage root. Cloud storage or file based allowed. | ||
| # If no root specified, defaults to file:///tmp/ucroot | ||
| # Managed tables will be stored under this root in the path: {storage-root.tables}/tables/{tableId} | ||
|
|
||
| #storage-root.tables=s3://my-s3-bucket/tables | ||
| #storage-root.tables=abfs://file_system@account_name.dfs.core.windows.net/tables | ||
| #storage-root.tables=gs://my-gc-bucket/tables | ||
| storage-root.tables=file:///tmp/ucroot | ||
|
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. the
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. Or for the single uc server, we can only configure one path for the process ?
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. storage-root.tables specify a storage root for all managed tables. Each table will be placed at
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. Make sense, thanks for the explanation ! |
||
|
|
||
| ## S3 Storage Config (Multiple configs can be added by incrementing the index) | ||
| s3.bucketPath.0= | ||
| s3.region.0= | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| import java.util.Date; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
| import org.hibernate.Session; | ||
| import org.hibernate.SessionFactory; | ||
| import org.hibernate.query.Query; | ||
|
|
@@ -122,6 +123,14 @@ public CatalogInfoDAO getCatalogDAO(Session session, String name) { | |
| return query.uniqueResult(); | ||
| } | ||
|
|
||
| public UUID getCatalogId(Session session, String catalogName) { | ||
| CatalogInfoDAO catalogInfo = getCatalogDAO(session, catalogName); | ||
| if (catalogInfo == null) { | ||
| throw new BaseException(ErrorCode.NOT_FOUND, "Catalog not found: " + catalogName); | ||
|
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.
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. Here it's indeed a catalog name. |
||
| } | ||
| return catalogInfo.getId(); | ||
| } | ||
|
|
||
| public CatalogInfo updateCatalog(String name, UpdateCatalog updateCatalog) { | ||
| if (updateCatalog.getNewName() != null) { | ||
| ValidationUtils.validateSqlObjectName(updateCatalog.getNewName()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package io.unitycatalog.server.persist; | ||
|
|
||
| import io.unitycatalog.server.exception.BaseException; | ||
| import io.unitycatalog.server.exception.ErrorCode; | ||
| import io.unitycatalog.server.model.CreateStagingTable; | ||
| import io.unitycatalog.server.model.StagingTableInfo; | ||
| import io.unitycatalog.server.persist.dao.StagingTableDAO; | ||
| import io.unitycatalog.server.persist.dao.TableInfoDAO; | ||
| import io.unitycatalog.server.persist.utils.TransactionManager; | ||
| import io.unitycatalog.server.utils.IdentityUtils; | ||
| import io.unitycatalog.server.utils.ServerProperties; | ||
| import io.unitycatalog.server.utils.ValidationUtils; | ||
| import java.util.UUID; | ||
| import org.hibernate.Session; | ||
| import org.hibernate.SessionFactory; | ||
| import org.hibernate.query.Query; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class StagingTableRepository { | ||
| private static final Logger LOGGER = LoggerFactory.getLogger(StagingTableRepository.class); | ||
| private final SessionFactory sessionFactory; | ||
| private final Repositories repositories; | ||
| private final ServerProperties serverProperties; | ||
|
|
||
| public StagingTableRepository( | ||
| Repositories repositories, SessionFactory sessionFactory, ServerProperties serverProperties) { | ||
| this.repositories = repositories; | ||
| this.sessionFactory = sessionFactory; | ||
| this.serverProperties = serverProperties; | ||
| } | ||
|
|
||
| private StagingTableDAO findBySchemaIdAndName(Session session, UUID schemaId, String name) { | ||
| String hql = "FROM StagingTableDAO t WHERE t.schemaId = :schemaId AND t.name = :name"; | ||
| Query<StagingTableDAO> query = session.createQuery(hql, StagingTableDAO.class); | ||
| query.setParameter("schemaId", schemaId); | ||
| query.setParameter("name", name); | ||
| LOGGER.debug("Finding staging table by schemaId: {} and name: {}", schemaId, name); | ||
| return query.uniqueResult(); // Returns null if no result is found | ||
| } | ||
|
|
||
| private StagingTableDAO findByStagingLocation(Session session, String stagingLocation) { | ||
| String hql = "FROM StagingTableDAO t WHERE t.stagingLocation = :stagingLocation"; | ||
| Query<StagingTableDAO> query = session.createQuery(hql, StagingTableDAO.class); | ||
| query.setParameter("stagingLocation", stagingLocation); | ||
| return query.uniqueResult(); // Returns null if no result is found | ||
| } | ||
|
|
||
| private void validateIfAlreadyExists( | ||
| Session session, UUID schemaId, String tableName, String stagingLocation) { | ||
| // check if staging table or table by the same name already exists | ||
| // Also ensure that no staging table exists at the same location | ||
| StagingTableDAO existingStagingTable = findBySchemaIdAndName(session, schemaId, tableName); | ||
| if (existingStagingTable != null) { | ||
| throw new BaseException( | ||
| ErrorCode.ALREADY_EXISTS, "Staging table already exists: " + tableName); | ||
| } | ||
| TableInfoDAO existingTable = | ||
| repositories.getTableRepository().findBySchemaIdAndName(session, schemaId, tableName); | ||
| if (existingTable != null) { | ||
| throw new BaseException(ErrorCode.ALREADY_EXISTS, "Table already exists: " + tableName); | ||
| } | ||
| StagingTableDAO existingStagingTableAtLocation = | ||
| findByStagingLocation(session, stagingLocation); | ||
| if (existingStagingTableAtLocation != null) { | ||
| throw new BaseException( | ||
| ErrorCode.ALREADY_EXISTS, "Staging table already exists at: " + stagingLocation); | ||
| } | ||
| } | ||
|
|
||
| private String getTableFullName(CreateStagingTable createStagingTable) { | ||
| return createStagingTable.getCatalogName() | ||
| + "." | ||
| + createStagingTable.getSchemaName() | ||
| + "." | ||
| + createStagingTable.getName(); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new staging table in the specified schema. | ||
| * | ||
| * <p>A staging table is a temporary location used during managed table creation. It provides a | ||
| * staging area where data can be written before finalizing the table creation. The staging | ||
| * location is automatically generated and must be committed before the table becomes permanent. | ||
| * | ||
| * @param createStagingTable the request containing catalog name, schema name, and table name | ||
| * @return StagingTableInfo containing the created staging table details including the staging | ||
| * location | ||
| * @throws BaseException with ErrorCode.ALREADY_EXISTS if a staging table or regular table with | ||
| * the same name already exists in the schema | ||
| * @throws BaseException with ErrorCode.NOT_FOUND if the specified catalog or schema does not | ||
| * exist | ||
| */ | ||
| public StagingTableInfo createStagingTable(CreateStagingTable createStagingTable) { | ||
|
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. Seems no usage for this method, do we still need it ?
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. The actual usage is in next PR. I have to split this to avoid a bigger than 500 lines PR.
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. Got it, thanks for the explannation. |
||
| serverProperties.checkManagedTableEnabled(); | ||
| ValidationUtils.validateSqlObjectName(createStagingTable.getName()); | ||
| String callerId = IdentityUtils.findPrincipalEmailAddress(); | ||
| UUID stagingTableId = UUID.randomUUID(); | ||
| String stagingLocation = | ||
| repositories.getFileOperations().createTableDirectory(stagingTableId.toString()); | ||
|
|
||
| return TransactionManager.executeWithTransaction( | ||
| sessionFactory, | ||
| session -> { | ||
| UUID schemaId = | ||
| repositories | ||
| .getSchemaRepository() | ||
| .getSchemaId( | ||
| session, | ||
| createStagingTable.getCatalogName(), | ||
| createStagingTable.getSchemaName()); | ||
| validateIfAlreadyExists(session, schemaId, createStagingTable.getName(), stagingLocation); | ||
|
|
||
| StagingTableDAO stagingTableDAO = new StagingTableDAO(); | ||
| stagingTableDAO.setDefaultFields(); | ||
| stagingTableDAO.setId(stagingTableId); | ||
| stagingTableDAO.setSchemaId(schemaId); | ||
| stagingTableDAO.setName(createStagingTable.getName()); | ||
| stagingTableDAO.setStagingLocation(stagingLocation); | ||
| stagingTableDAO.setCreatedBy(callerId); | ||
|
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. Do we also need to set other non-nullable fields also ? I'm not sure, just question.
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. They are set in |
||
| session.persist(stagingTableDAO); | ||
| return stagingTableDAO.toStagingTableInfo( | ||
| createStagingTable.getCatalogName(), createStagingTable.getSchemaName()); | ||
| }, | ||
| "Error creating table: " + getTableFullName(createStagingTable), | ||
| /* readOnly = */ false); | ||
| } | ||
| } | ||
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: what's the change for this line ?
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.
That's the will->may change to address the comment on the last PR.
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.
thank you for addressing this.