From 2d35b5322b6155dbcbe57aad6bfd8931d5d309c6 Mon Sep 17 00:00:00 2001 From: tsreaper Date: Thu, 14 May 2026 14:18:59 +0800 Subject: [PATCH] [core] Introduce Catalog#loadTableMetadata --- .../paimon/catalog/AbstractCatalog.java | 3 +- .../org/apache/paimon/catalog/Catalog.java | 9 +++++ .../paimon/catalog/DelegateCatalog.java | 5 +++ .../org/apache/paimon/rest/RESTCatalog.java | 3 +- .../paimon/table/FileStoreTableFactory.java | 34 ++++++++++++++----- .../org/apache/paimon/hive/HiveCatalog.java | 2 +- 6 files changed, 44 insertions(+), 12 deletions(-) diff --git a/paimon-core/src/main/java/org/apache/paimon/catalog/AbstractCatalog.java b/paimon-core/src/main/java/org/apache/paimon/catalog/AbstractCatalog.java index ad51ab1f4dc0..11c0568b278a 100644 --- a/paimon-core/src/main/java/org/apache/paimon/catalog/AbstractCatalog.java +++ b/paimon-core/src/main/java/org/apache/paimon/catalog/AbstractCatalog.java @@ -668,7 +668,8 @@ public Path newDatabasePath(String database) { return newDatabasePath(warehouse(), database); } - protected TableMetadata loadTableMetadata(Identifier identifier) throws TableNotExistException { + @Override + public TableMetadata loadTableMetadata(Identifier identifier) throws TableNotExistException { return new TableMetadata(loadTableSchema(identifier), false, null); } diff --git a/paimon-core/src/main/java/org/apache/paimon/catalog/Catalog.java b/paimon-core/src/main/java/org/apache/paimon/catalog/Catalog.java index ab66f937ea02..70b25a67c5e9 100644 --- a/paimon-core/src/main/java/org/apache/paimon/catalog/Catalog.java +++ b/paimon-core/src/main/java/org/apache/paimon/catalog/Catalog.java @@ -166,6 +166,15 @@ void alterDatabase(String name, List changes, boolean ignoreIfNo */ Table getTableById(String tableId) throws TableIdNotExistException; + /** + * Return a {@link TableMetadata} identified by the given {@link Identifier}. + * + * @param identifier Path of the table + * @return The requested table metadata + * @throws TableNotExistException if the target does not exist + */ + TableMetadata loadTableMetadata(Identifier identifier) throws TableNotExistException; + /** * Get names of all tables under this database. An empty list is returned if none exists. * diff --git a/paimon-core/src/main/java/org/apache/paimon/catalog/DelegateCatalog.java b/paimon-core/src/main/java/org/apache/paimon/catalog/DelegateCatalog.java index ec5138a8cbfb..7327eb10c8a1 100644 --- a/paimon-core/src/main/java/org/apache/paimon/catalog/DelegateCatalog.java +++ b/paimon-core/src/main/java/org/apache/paimon/catalog/DelegateCatalog.java @@ -158,6 +158,11 @@ public Table getTableById(String tableId) throws TableIdNotExistException { return wrapped.getTableById(tableId); } + @Override + public TableMetadata loadTableMetadata(Identifier identifier) throws TableNotExistException { + return wrapped.loadTableMetadata(identifier); + } + @Override public void renameTable(Identifier fromTable, Identifier toTable, boolean ignoreIfNotExists) throws TableNotExistException, TableAlreadyExistException { diff --git a/paimon-core/src/main/java/org/apache/paimon/rest/RESTCatalog.java b/paimon-core/src/main/java/org/apache/paimon/rest/RESTCatalog.java index 155d6a989324..642db680ef04 100644 --- a/paimon-core/src/main/java/org/apache/paimon/rest/RESTCatalog.java +++ b/paimon-core/src/main/java/org/apache/paimon/rest/RESTCatalog.java @@ -453,7 +453,8 @@ public void rollbackSchema(Identifier identifier, long schemaId) } } - private TableMetadata loadTableMetadata(Identifier identifier) throws TableNotExistException { + @Override + public TableMetadata loadTableMetadata(Identifier identifier) throws TableNotExistException { // if the table is system table, we need to load table metadata from the system table's data // table Identifier loadTableIdentifier = diff --git a/paimon-core/src/main/java/org/apache/paimon/table/FileStoreTableFactory.java b/paimon-core/src/main/java/org/apache/paimon/table/FileStoreTableFactory.java index e4a865679910..bee9ea080f7a 100644 --- a/paimon-core/src/main/java/org/apache/paimon/table/FileStoreTableFactory.java +++ b/paimon-core/src/main/java/org/apache/paimon/table/FileStoreTableFactory.java @@ -19,7 +19,9 @@ package org.apache.paimon.table; import org.apache.paimon.CoreOptions; +import org.apache.paimon.catalog.Catalog; import org.apache.paimon.catalog.CatalogContext; +import org.apache.paimon.catalog.CatalogLoader; import org.apache.paimon.catalog.Identifier; import org.apache.paimon.fs.FileIO; import org.apache.paimon.fs.Path; @@ -192,17 +194,31 @@ private static FileStoreTable createOtherBranchTable( CatalogEnvironment catalogEnvironment) { Options branchOptions = new Options(dynamicOptions.toMap()); branchOptions.set(CoreOptions.BRANCH, branchName); - Optional schema = new SchemaManager(fileIO, tablePath, branchName).latest(); + + Identifier identifier = catalogEnvironment.identifier(); + Identifier branchIdentifier = null; + if (identifier != null) { + branchIdentifier = + new Identifier( + identifier.getDatabaseName(), identifier.getObjectName(), branchName); + } + CatalogLoader catalogLoader = catalogEnvironment.catalogLoader(); + + Optional schema; + if (branchIdentifier != null && catalogLoader != null) { + try (Catalog catalog = catalogLoader.load()) { + schema = Optional.of(catalog.loadTableMetadata(branchIdentifier).schema()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } else { + schema = new SchemaManager(fileIO, tablePath, branchName).latest(); + } + if (schema.isPresent()) { - Identifier identifier = catalogEnvironment.identifier(); CatalogEnvironment branchCatalogEnvironment = catalogEnvironment; - if (identifier != null) { - branchCatalogEnvironment = - catalogEnvironment.copy( - new Identifier( - identifier.getDatabaseName(), - identifier.getObjectName(), - branchName)); + if (branchIdentifier != null) { + branchCatalogEnvironment = catalogEnvironment.copy(branchIdentifier); } return createWithoutFallbackBranch( fileIO, tablePath, schema.get(), branchOptions, branchCatalogEnvironment); diff --git a/paimon-hive/paimon-hive-catalog/src/main/java/org/apache/paimon/hive/HiveCatalog.java b/paimon-hive/paimon-hive-catalog/src/main/java/org/apache/paimon/hive/HiveCatalog.java index ac0706b40bb0..095697dc2a1b 100644 --- a/paimon-hive/paimon-hive-catalog/src/main/java/org/apache/paimon/hive/HiveCatalog.java +++ b/paimon-hive/paimon-hive-catalog/src/main/java/org/apache/paimon/hive/HiveCatalog.java @@ -770,7 +770,7 @@ public org.apache.paimon.table.Table getTable(Identifier identifier) } @Override - protected TableMetadata loadTableMetadata(Identifier identifier) throws TableNotExistException { + public TableMetadata loadTableMetadata(Identifier identifier) throws TableNotExistException { return loadTableMetadata(identifier, getHmsTable(identifier)); }