Skip to content

Support managed table creation - #1135

Merged
tdas merged 8 commits into
unitycatalog:mainfrom
yili-db:staging_table_service
Nov 6, 2025
Merged

tdas merged 8 commits into
unitycatalog:mainfrom
yili-db:staging_table_service

Conversation

@yili-db

@yili-db yili-db commented Oct 27, 2025 •

Copy link
Copy Markdown
Collaborator

🥞 Stacked PR

Use this link to review incremental changes.


PR Checklist

  • A description of the changes is added to the description of this PR.

  • If there is a related issue, make sure it is linked to this PR.

  • If you've fixed a bug or added code that should be tested, add tests!

  • If you've added or modified a feature, documentation in docs is updated

    Support managed table creation

    • Update TableService for creation of managed tables
    • Update API for CreateTable to make storage_location optional for managed tables
    • Enhance TemporaryTableCredentialsService with staging table support
    • Update KeyMapper to also check staging tables
    • Update repository classes to use new helper methods

Related issue is: #1143

@yili-db yili-db changed the title [Managed table 2] Add staging table service and integrate with server [Managed table 3] Add staging table service and integrate with server Oct 27, 2025
@yili-db
yili-db force-pushed the staging_table_service branch 8 times, most recently from 7c101bb to 8000700 Compare October 29, 2025 23:16
@yili-db
yili-db marked this pull request as draft October 31, 2025 20:07
@yili-db
yili-db force-pushed the staging_table_service branch from 8000700 to 3c41fd4 Compare November 2, 2025 09:32
@yili-db yili-db changed the title [Managed table 3] Add staging table service and integrate with server Support managed table creation Nov 2, 2025
@yili-db
yili-db force-pushed the staging_table_service branch 2 times, most recently from e706611 to 402466e Compare November 4, 2025 08:41
@yili-db
yili-db marked this pull request as ready for review November 4, 2025 08:42
Comment thread api/Models/CreateTable.md Outdated
| **data\_source\_format** | [**DataSourceFormat**](DataSourceFormat.md) | | [default to null] |
| **columns** | [**List**](ColumnInfo.md) | The array of __ColumnInfo__ definitions of the table's columns. | [default to null] |
| **storage\_location** | **String** | Storage root URL for external table | [default to null] |
| **storage\_location** | **String** | Storage root URL for external table | [optional] [default to null] |

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why make this explicitly to be optional ? maybe, one dumb question, for managed table, do people have the access to specify the storage_location as they required, since it's a managed table, then people should not be able to perceive the storage_location.

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.

It's optional once it's removed from the requried section in all.yaml.
The reason is explained here: #1135 (comment)

Comment thread api/all.yaml
- table_type
- data_source_format
- columns
- storage_location

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this auto-generated, or changed by manual ? seems like it's easily to be broken, I mean forgot to change this, and leave an inconsistent api definition.

@yili-db yili-db Nov 4, 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.

This is intentional.
Previously creating an EXTERNAL always required a storage_location.
With MANAGED tables there are two new ways:

  1. Create a staging table, then specify TableType=MANAGED and storage_location="staging_table_url" here to create a table
  2. Skip the staging table part, just create table with TableType=MANAGED and storage_location=null.

Both ways would end up with a managed table with valid managed URL.

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 file is manually changed.

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.

@tdas decided that we don't want to support the 2nd code path. So I am reverting this line.

@yili-db
yili-db requested a review from openinx November 4, 2025 18:32
Comment on lines +67 to +86
try {
TableInfo table =
isTableName
? tableRepository.getTable(fullName)
: tableRepository.getTableById(fullName);
catalogName = table.getCatalogName();
schemaName = table.getSchemaName();
tableId = table.getTableId();
} catch (BaseException e) {
if (!isTableName && e.getErrorCode() == ErrorCode.NOT_FOUND) {
// Check if this is actually a request for staging table. Staging tables are only
// addressed via their IDs.
StagingTableInfo stagingTable = stagingTableRepository.getStagingTableById(fullName);
catalogName = stagingTable.getCatalogName();
schemaName = stagingTable.getSchemaName();
tableId = stagingTable.getId();
} else {
throw e;
}
}

@openinx openinx Nov 4, 2025 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Will it be more clear if we just seperate the id-based selection and name-based selection here ?

      if (isTableName) {
        TableInfo table = tableRepository.getTable(fullName);
        catalogName = table.getCatalogName();
        schemaName = table.getSchemaName();
        tableId = table.getTableId();
      } else {
        try {
          TableInfo table = tableRepository.getTableById(fullName);
          catalogName = table.getCatalogName();
          schemaName = table.getSchemaName();
          tableId = table.getTableId();
        } catch (BaseException e) {
          if (e.getErrorCode() == ErrorCode.NOT_FOUND) {
            StagingTableInfo stagingTable = stagingTableRepository.getStagingTableById(fullName);
            catalogName = stagingTable.getCatalogName();
            schemaName = stagingTable.getSchemaName();
            tableId = stagingTable.getId();
          } else {
            throw e;
          }
        }
      }

}

public static Pair<String, String> getCatalogAndSchemaNames(Session session, UUID schemaId) {
SchemaInfoDAO schemaInfoDAO =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: could you plesae help to make it to be a full line ?

SchemaInfoDAO schemaInfoDAO = session.get(SchemaInfoDAO.class, schemaId);

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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Passing the whole serverProperties to the TableRepository seems strange to me, because the database layer should not be aware of the whole server-side properties. A better option is: use passing the managedTableEnabled boolean switch to the database layer.

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.

It wants to call a function checkManagedTableEnabled() from serverProperties.
If a boolean from serverProperties is passed into TableRepository, tableRepository and stagingTableRepository will have to check the boolean by themselves.

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.

i am okay with the server properties being passed as a whole. today there is one property that affects the behavior. tomorrow there may be many more properties. we dont want to keep changing the constructor signature every time. So better in the long run to send the whole serverproperties in the repo

think of it like hadoopConf. hadoopConf passed round everywhere for exactly this reason.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Okay, then let's keep it.

stagingTableDAO.setStageCommitted(true);
stagingTableDAO.setStageCommittedAt(now);
stagingTableDAO.setAccessedAt(now);
session.merge(stagingTableDAO);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What will happen if two contention competitors are committing the same stagingTable at the same time ?

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.

The session is provided by TransactionManager.executeWithTransaction(... session -> {...})
So it's protected by a DB transaction. This is the same mechanism used for all DB operations in this repo.

return TransactionManager.executeWithTransaction(
sessionFactory,
session -> {
LOGGER.debug("Getting storage location of table by id: {}", tableId);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: Please left a blank line to separate the different code blocks, which make it will be more easier to read.

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.

Do you mean before this line? Or before line 53 the next LOGGER.debug?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yea, I think the intention blank line separate the code block, which makes easily to read.

this.serverProperties = serverProperties;
}

public String getStorageLocationForTableOrStagingTable(String tableId) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why do we need to mix both the normal table access and staging table access in the same method ? because the table id can always belongs to either specified normal table or staging table ?

( this make the code some kind of complicated --- since someone may forget to access the staging table, it's possible to use some abstraction to avoid other forgetting access the staging table ? )

@yili-db yili-db Nov 4, 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.

Do you meaning integrating the staging table access into getTableById to make it impossible to miss staging table?
The problem is that staging table and table are very different. Staging table is very incomplete that it lacks many essential metadata. In most code paths they should not care about staging table. There are only a limited number of scenarios that should access staging tables:

  1. creating a MANAGED table
  2. vending a table credential when it's still a staging table
  3. auth of vending a table credential when it's still a staging table

The other code paths of normal table operations should skip staging tables even if the ID/name matches
Mixing up tables and staging tables will be more error prone.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the context, I understand the staging table is a temporary status which is different from the normal table. Then the question is: how can we clearly org the code or comment , so that the following authors can easily got the points and context from your side, and finally they won't mistake when should we access the staging table and when should not. I think that's the key point.

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.

Added method doc to clarify the use cases of staging table.

Pair<String, String> catalogAndSchemaNames =
RepositoryUtils.getCatalogAndSchemaNames(session, tableInfoDAO.getSchemaId());
return tableInfoDAO.toTableInfo(
true, catalogAndSchemaNames.getLeft(), catalogAndSchemaNames.getRight());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

for the fetchColumns=true, is it lazily fetching or sync fetching ?

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.

It's lazily fetched. But this part is unchanged in this PR anyway. It's just a little code refactor to make it shorter and cleaner.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Got it, thanks.

if (createTable.getDataSourceFormat() != DataSourceFormat.DELTA) {
throw new BaseException(
ErrorCode.INVALID_ARGUMENT,
"MANAGED table creation is only supported for delta tables.");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is this temporarly support delta only, or future we will support other table types ?

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.

I don't know about the future. But for now we just support delta tables.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Okay, I just try to understand if we want to support further tables, then how could we do the changes to support it? Seems like currently we don't have the abstraction to support different types of managed table.....

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.

We can revise this when different types of managed table is to be supported.

.accessKeyId(awsSessionCredentials.accessKeyId())
.secretAccessKey(awsSessionCredentials.secretAccessKey())
.sessionToken(awsSessionCredentials.sessionToken()));
if (storageScheme != null) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

When will the storageScheme to be nullable ?

@yili-db yili-db Nov 4, 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.

If it happens to be a local path like /tmp/something

@yili-db
yili-db force-pushed the staging_table_service branch from 4f9b239 to 6ad8a38 Compare November 5, 2025 20:58
return catalogInfoDAO;
}

public static Pair<String, String> getCatalogAndSchemaNames(Session session, UUID schemaId) {

@tdas tdas Nov 5, 2025 •

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.

this is doing two database query right?

it would be good add this to the methods docs.

@yili-db
yili-db requested a review from tdas November 5, 2025 23:06
@yili-db
yili-db force-pushed the staging_table_service branch 2 times, most recently from e351cfd to ec00da7 Compare November 6, 2025 00:17
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.

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.

this.serverProperties = serverProperties;
}

public TableInfo getTableById(String tableId) {

@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.

getTableById is no longer needed.
It is replaced by getCatalogSchemaIdsByTableOrStagingTableId and getStorageLocationForTableOrStagingTable

@yili-db
yili-db force-pushed the staging_table_service branch from ec00da7 to d531938 Compare November 6, 2025 00:24
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

@tdas tdas left a comment

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.

LGTM. but i will wait for @openinx to sign off before merging

yili-db and others added 6 commits November 6, 2025 10:31
- Update TableService for creation of managed tables
- Update API for CreateTable to make storage_location optional for managed tables
- Enhance TemporaryTableCredentialsService with staging table support
- Update KeyMapper to also check staging tables
- Update repository classes to use new helper methods

Signed-off-by: Yi Li <[email protected]>
Signed-off-by: Yi Li <[email protected]>
Signed-off-by: Yi Li <[email protected]>
…hod docs to explain why staging table is required.

Signed-off-by: Yi Li <[email protected]>
* @return StagingTableInfo containing the staging table details
* @throws BaseException with ErrorCode.NOT_FOUND if the staging table does not exist
*/
public StagingTableInfo getStagingTableById(String stagingTableId) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Seems like no usage in this PR ? Could we just remove it in this PR, and if it's required in the following PR, then I think it's better to have it in the next PR.

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.

Removed. I forgot this one when I introduced getCatalogSchemaIdsByTableOrStagingTableId

* @throws BaseException with ErrorCode.NOT_FOUND if neither a table nor staging table is found
* with the given ID
*/
public String getStorageLocationForTableOrStagingTable(UUID tableId) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you so much for the javadoc, which makes more clear now.

@openinx openinx left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Generally looks good to me, approved. Thanks @yili-db for the contribution, and thanks @tdas for the reviewing work.

@yili-db
yili-db force-pushed the staging_table_service branch from beee7af to 90c0f2a Compare November 6, 2025 19:25
@tdas
tdas merged commit f10952f into unitycatalog:main Nov 6, 2025
yili-db added a commit to yili-db/unitycatalog that referenced this pull request Nov 19, 2025
Use this
[link](https://github.com/unitycatalog/unitycatalog/pull/1135/files) to
review incremental changes.
-
[**staging_table_service**](unitycatalog#1135)
[[Files
changed](https://github.com/unitycatalog/unitycatalog/pull/1135/files)]
-
[refactor_base_table_crud_test](unitycatalog#1153)
[[Files
changed](https://github.com/unitycatalog/unitycatalog/pull/1153/files/90c0f2a454c595eeb19df146f38a74e404de61b9..9e82c72fb64a8f5b50a11e0558b01ebb86f906a5)]
-
[staging_table_cli_test](unitycatalog#1136)
[[Files
changed](https://github.com/unitycatalog/unitycatalog/pull/1136/files/9e82c72fb64a8f5b50a11e0558b01ebb86f906a5..b7cbb71bbf046d9926394052258e625b9921d303)]

---------
**PR Checklist**

- [x] A description of the changes is added to the description of this
PR.
- [x] If there is a related issue, make sure it is linked to this PR.
- [ ] If you've fixed a bug or added code that should be tested, add
tests!
- [ ] If you've added or modified a feature, documentation in `docs` is
updated

    Support managed table creation

    - Update TableService for creation of managed tables
- Update API for CreateTable to make storage_location optional for
managed tables
- Enhance TemporaryTableCredentialsService with staging table support
    - Update KeyMapper to also check staging tables
    - Update repository classes to use new helper methods

<!-- Please state what you've changed and how it might affect the users.
-->
Related issue is:
unitycatalog#1143

---------

Signed-off-by: Yi Li <[email protected]>
Co-authored-by: Tathagata Das <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants