-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[spark] Add managed Format Table partition administration #8730
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
Closed
sundapeng
wants to merge
3
commits into
apache:master
from
sundapeng:split-v4/format-table-managed-spark-admin
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -206,6 +206,52 @@ CREATE TABLE my_table ( | |
| ); | ||
| ``` | ||
|
|
||
| ### Manage Format Table Partitions | ||
|
|
||
| For managed Format Tables, whose `metastore.partitioned-table` option is `true`, partition | ||
| metadata is stored in the catalog and Spark supports the standard partition DDL: | ||
|
|
||
| ```sql | ||
| ALTER TABLE my_table ADD PARTITION (dt='2025-01-01'); | ||
| ALTER TABLE my_table DROP PARTITION (dt='2025-01-01'); | ||
| MSCK REPAIR TABLE my_table; | ||
| SHOW PARTITIONS my_table; | ||
| ``` | ||
|
|
||
| On a Format Table that is not managed, `ADD PARTITION`, `DROP PARTITION` and | ||
| `MSCK REPAIR TABLE` fail with an error. | ||
|
|
||
| `SHOW PARTITIONS` throws an error when the number of partitions exceeds | ||
| `spark.paimon.format-table.show-partitions.max-results` (default: 10000). For larger tables, use | ||
| `CALL sys.list_format_table_partitions` to list partitions page by page. | ||
|
|
||
| `ADD PARTITION` creates the partition directory and registers the partition; querying a newly | ||
| added partition before any data is written returns no rows. `DROP PARTITION` unregisters the | ||
| partition and deletes its directory. | ||
|
|
||
| :::info | ||
|
|
||
| `metastore.partitioned-table = true` enables catalog-managed partitions, which requires an | ||
| internal Format Table in a catalog that supports it (currently the REST catalog) and cannot be | ||
| combined with `format-table.implementation = engine`. The REST catalog validates this | ||
| combination on `CREATE TABLE`, with catalog-level table defaults | ||
| (`spark.sql.catalog.paimon.table-default.*`) participating in the effective options: a default | ||
| that makes the combination invalid fails the DDL. Other catalogs treat the option as inert. | ||
|
|
||
| A table that carries `metastore.partitioned-table = true` where it cannot be honored (a non-REST | ||
| catalog, or an external table) loads as an unmanaged Format Table and a warning is logged. To | ||
| remove the option, use `ALTER TABLE my_table UNSET TBLPROPERTIES ('metastore.partitioned-table')`. | ||
| On a REST catalog, an existing Format Table whose partitions were never registered reads as empty | ||
| until you register them with `MSCK REPAIR TABLE my_table` or | ||
| `CALL sys.sync_format_table_metadata`. | ||
|
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. Why not just use MSCK? |
||
|
|
||
| Mixed-version note: only writers that support catalog-managed partitions register the partitions | ||
| they produce. During a rolling upgrade, upgrade all writers before relying on managed reads, or | ||
| run `MSCK REPAIR TABLE` / `CALL sys.sync_format_table_metadata` afterwards, since data written by | ||
| an older writer is not visible to a managed read until its partitions are registered. | ||
|
|
||
| ::: | ||
|
|
||
| ### Create External Table | ||
|
|
||
| When the catalog's `metastore` type is `hive`, if the `location` is specified when creating a table, that table will be considered an external table; otherwise, it will be a managed table. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ | |
| import org.apache.paimon.table.FormatTable; | ||
| import org.apache.paimon.table.Table; | ||
| import org.apache.paimon.table.TableSnapshot; | ||
| import org.apache.paimon.table.format.FormatTableCatalogProvider; | ||
| import org.apache.paimon.table.iceberg.IcebergTable; | ||
| import org.apache.paimon.table.lance.LanceTable; | ||
| import org.apache.paimon.table.object.ObjectTable; | ||
|
|
@@ -54,6 +55,9 @@ | |
| import org.apache.paimon.utils.Pair; | ||
| import org.apache.paimon.utils.Preconditions; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
@@ -85,6 +89,8 @@ | |
| /** Utils for {@link Catalog}. */ | ||
| public class CatalogUtils { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(CatalogUtils.class); | ||
|
|
||
| public static Path path(String warehouse, String database, String table) { | ||
| return new Path(String.format("%s/%s.db/%s", warehouse, database, table)); | ||
| } | ||
|
|
@@ -173,6 +179,7 @@ private static void validateFormatTableOptions(Options options, boolean dataToke | |
| options.get(PRIMARY_KEY) == null, | ||
| "Cannot define %s for format table.", | ||
| PRIMARY_KEY.key()); | ||
| validateManagedFormatTableOptions(options); | ||
| if (dataTokenEnabled && options.get(PATH) == null) { | ||
| checkArgument( | ||
| options.get(FORMAT_TABLE_IMPLEMENTATION) | ||
|
|
@@ -213,6 +220,44 @@ private static void validateFormatTableOptions(Options options, boolean dataToke | |
| } | ||
| } | ||
|
|
||
| /** Validate options which are specific to catalog-managed format tables. */ | ||
| public static void validateManagedFormatTableOptions(Map<String, String> tableOptions) { | ||
| validateManagedFormatTableOptions(Options.fromMap(tableOptions)); | ||
| } | ||
|
|
||
| private static void validateManagedFormatTableOptions(Options options) { | ||
| if (options.get(CoreOptions.METASTORE_PARTITIONED_TABLE)) { | ||
| checkArgument( | ||
| options.get(FORMAT_TABLE_IMPLEMENTATION) | ||
| != CoreOptions.FormatTableImplementation.ENGINE, | ||
| "Cannot define %s=true together with %s=engine for a managed format table.", | ||
| CoreOptions.METASTORE_PARTITIONED_TABLE.key(), | ||
| FORMAT_TABLE_IMPLEMENTATION.key()); | ||
| } | ||
| } | ||
|
|
||
| /** Validate that a catalog can persist the requested managed Format Table. */ | ||
| public static void validateManagedFormatTableCatalog( | ||
| Identifier identifier, | ||
| Map<String, String> tableOptions, | ||
| Catalog catalog, | ||
| boolean isExternal) { | ||
| CoreOptions options = CoreOptions.fromMap(tableOptions); | ||
| if (options.type() != TableType.FORMAT_TABLE || !options.partitionedTableInMetastore()) { | ||
| return; | ||
| } | ||
| checkArgument( | ||
| !isExternal, | ||
| "Managed format table %s must be an internal table.", | ||
| identifier.getFullName()); | ||
| checkArgument( | ||
| catalog.supportsManagedFormatTablePartitions(), | ||
| "Managed format table %s requires a catalog with catalog-managed partition support" | ||
| + " (e.g. a REST catalog), but %s does not support it.", | ||
| identifier.getFullName(), | ||
| catalog.getClass().getSimpleName()); | ||
| } | ||
|
|
||
| public static void validateNamePattern(Catalog catalog, String namePattern) { | ||
| if (Objects.nonNull(namePattern) && !catalog.supportsListByPattern()) { | ||
| throw new UnsupportedOperationException( | ||
|
|
@@ -271,6 +316,34 @@ public static List<Partition> listPartitionsFromFileSystem( | |
| return partitions; | ||
| } | ||
|
|
||
| /** | ||
| * Why the managed Format Table semantics cannot be honored when loading the table from this | ||
| * catalog, or {@code null} if they can. | ||
| */ | ||
| @Nullable | ||
| private static String managedFormatTableLoadProblem( | ||
|
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. Just throw exception, remove this. |
||
| Catalog catalog, TableMetadata metadata, Map<String, String> tableOptions) { | ||
| if (!catalog.supportsManagedFormatTablePartitions()) { | ||
| return String.format( | ||
| "catalog %s does not support catalog-managed Format Table partitions", | ||
| catalog.getClass().getSimpleName()); | ||
| } | ||
| if (metadata.isExternal()) { | ||
| return "managed format tables must be internal tables"; | ||
| } | ||
| if (Options.fromMap(tableOptions).get(FORMAT_TABLE_IMPLEMENTATION) | ||
| == CoreOptions.FormatTableImplementation.ENGINE) { | ||
| return String.format( | ||
| "the option conflicts with %s=engine", FORMAT_TABLE_IMPLEMENTATION.key()); | ||
| } | ||
| if (catalog.catalogLoader() == null) { | ||
| return String.format( | ||
| "catalog %s does not provide a catalog loader", | ||
| catalog.getClass().getSimpleName()); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Load table from {@link Catalog}, this table can be: | ||
| * | ||
|
|
@@ -304,7 +377,30 @@ public static Table loadTable( | |
| Function<Path, FileIO> dataFileIO = metadata.isExternal() ? externalFileIO : internalFileIO; | ||
|
|
||
| if (options.type() == TableType.FORMAT_TABLE) { | ||
| return toFormatTable(identifier, schema, dataFileIO, catalogContext); | ||
| FormatTableCatalogProvider catalogProvider = null; | ||
| if (options.partitionedTableInMetastore()) { | ||
| String problem = managedFormatTableLoadProblem(catalog, metadata, schema.options()); | ||
| if (problem == null) { | ||
| catalogProvider = | ||
| new FormatTableCatalogProvider(identifier, catalog.catalogLoader()); | ||
| } else { | ||
| // Existing tables may carry the option from a version (or catalog) where it | ||
| // was inert; refusing to load them would leave no SQL way to repair the | ||
| // table. Keep them readable with filesystem partition discovery instead. | ||
| LOG.warn( | ||
| "Ignoring '{}=true' on format table {}: {}. Falling back to " | ||
| + "filesystem partition discovery, the table behaves as an " | ||
| + "unmanaged format table. Use ALTER TABLE ... RESET/UNSET " | ||
| + "to remove the option.", | ||
| CoreOptions.METASTORE_PARTITIONED_TABLE.key(), | ||
| identifier, | ||
| problem); | ||
| Map<String, String> effectiveOptions = new HashMap<>(schema.options()); | ||
| effectiveOptions.remove(CoreOptions.METASTORE_PARTITIONED_TABLE.key()); | ||
| schema = schema.copy(effectiveOptions); | ||
| } | ||
| } | ||
| return toFormatTable(identifier, schema, dataFileIO, catalogContext, catalogProvider); | ||
| } | ||
|
|
||
| if (options.type() == TableType.OBJECT_TABLE) { | ||
|
|
@@ -453,7 +549,8 @@ private static FormatTable toFormatTable( | |
| Identifier identifier, | ||
| TableSchema schema, | ||
| Function<Path, FileIO> fileIO, | ||
| CatalogContext catalogContext) { | ||
| CatalogContext catalogContext, | ||
| @Nullable FormatTableCatalogProvider catalogProvider) { | ||
| Map<String, String> options = schema.options(); | ||
| FormatTable.Format format = | ||
| FormatTable.parseFormat( | ||
|
|
@@ -471,6 +568,7 @@ private static FormatTable toFormatTable( | |
| .options(options) | ||
| .comment(schema.comment()) | ||
| .catalogContext(catalogContext) | ||
| .catalogProvider(catalogProvider) | ||
| .build(); | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Hive behavior?