|
55 | 55 | CreateTableTransaction, |
56 | 56 | StagedTable, |
57 | 57 | Table, |
| 58 | + TableProperties, |
58 | 59 | ) |
59 | 60 | from pyiceberg.table.metadata import TableMetadata, TableMetadataV1, new_table_metadata |
60 | 61 | from pyiceberg.table.sorting import UNSORTED_SORT_ORDER, SortOrder |
@@ -757,6 +758,24 @@ def _convert_schema_if_needed(schema: Union[Schema, "pa.Schema"]) -> Schema: |
757 | 758 | pass |
758 | 759 | raise ValueError(f"{type(schema)=}, but it must be pyiceberg.schema.Schema or pyarrow.Schema") |
759 | 760 |
|
| 761 | + @staticmethod |
| 762 | + def metadata_file_location(table_location: str, file_name: str, properties: Properties = EMPTY_DICT) -> str: |
| 763 | + """Get the full path for a metadata file. |
| 764 | +
|
| 765 | + Args: |
| 766 | + table_location (str): The base table location |
| 767 | + file_name (str): Name of the metadata file |
| 768 | + properties (Properties): Table properties that may contain custom metadata path |
| 769 | +
|
| 770 | + Returns: |
| 771 | + str: Full path where the metadata file should be stored |
| 772 | + """ |
| 773 | + if metadata_path := properties.get(TableProperties.WRITE_METADATA_PATH): |
| 774 | + base_path = metadata_path.rstrip("/") |
| 775 | + else: |
| 776 | + base_path = f"{table_location}/metadata" |
| 777 | + return f"{base_path}/{file_name}" |
| 778 | + |
760 | 779 | def __repr__(self) -> str: |
761 | 780 | """Return the string representation of the Catalog class.""" |
762 | 781 | return f"{self.name} ({self.__class__})" |
@@ -840,7 +859,7 @@ def _create_staged_table( |
840 | 859 | database_name, table_name = self.identifier_to_database_and_table(identifier) |
841 | 860 |
|
842 | 861 | location = self._resolve_table_location(location, database_name, table_name) |
843 | | - metadata_location = self._get_metadata_location(location=location) |
| 862 | + metadata_location = self._get_metadata_location(table_location=location, properties=properties) |
844 | 863 | metadata = new_table_metadata( |
845 | 864 | location=location, schema=schema, partition_spec=partition_spec, sort_order=sort_order, properties=properties |
846 | 865 | ) |
@@ -871,7 +890,9 @@ def _update_and_stage_table( |
871 | 890 | ) |
872 | 891 |
|
873 | 892 | new_metadata_version = self._parse_metadata_version(current_table.metadata_location) + 1 if current_table else 0 |
874 | | - new_metadata_location = self._get_metadata_location(updated_metadata.location, new_metadata_version) |
| 893 | + new_metadata_location = self._get_metadata_location( |
| 894 | + updated_metadata.location, new_metadata_version, updated_metadata.properties |
| 895 | + ) |
875 | 896 |
|
876 | 897 | return StagedTable( |
877 | 898 | identifier=table_identifier, |
@@ -929,11 +950,12 @@ def _write_metadata(metadata: TableMetadata, io: FileIO, metadata_path: str) -> |
929 | 950 | ToOutputFile.table_metadata(metadata, io.new_output(metadata_path)) |
930 | 951 |
|
931 | 952 | @staticmethod |
932 | | - def _get_metadata_location(location: str, new_version: int = 0) -> str: |
| 953 | + def _get_metadata_location(table_location: str, new_version: int = 0, properties: Properties = EMPTY_DICT) -> str: |
933 | 954 | if new_version < 0: |
934 | 955 | raise ValueError(f"Table metadata version: `{new_version}` must be a non-negative integer") |
935 | | - version_str = f"{new_version:05d}" |
936 | | - return f"{location}/metadata/{version_str}-{uuid.uuid4()}.metadata.json" |
| 956 | + |
| 957 | + file_name = f"{new_version:05d}-{uuid.uuid4()}.metadata.json" |
| 958 | + return Catalog.metadata_file_location(table_location, file_name, properties) |
937 | 959 |
|
938 | 960 | @staticmethod |
939 | 961 | def _parse_metadata_version(metadata_location: str) -> int: |
|
0 commit comments