-
Notifications
You must be signed in to change notification settings - Fork 444
Added implementation to delete file and CLI to add file #2997
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
Open
subkanthi
wants to merge
10
commits into
apache:main
Choose a base branch
from
subkanthi:delete_file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+459
−5
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e63feff
Added implementation to delete file.
subkanthi a3952c2
moved tests to test_deletes.py
subkanthi 5ee0ca6
Fixed lint errors.
subkanthi 2091f00
Added cli command to delete file.
subkanthi 911f8a5
fixed lint errors
subkanthi 99c8878
fixed lint errors
subkanthi 937d16a
fixed subscriptable error when converting from tuple to list.
subkanthi 698218a
fixed lint errors
subkanthi 2364490
fix unit tests.
subkanthi 7e9e4b3
Added add-files option to CLI.
subkanthi 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -936,6 +936,41 @@ def add_files( | |
| for data_file in data_files: | ||
| append_files.append_data_file(data_file) | ||
|
|
||
| def delete_files( | ||
| self, | ||
| file_paths: list[str], | ||
| snapshot_properties: dict[str, str] = EMPTY_DICT, | ||
| branch: str | None = MAIN_BRANCH, | ||
| ) -> None: | ||
| """ | ||
| Shorthand API for removing data files from the table transaction by their paths. | ||
|
|
||
| Args: | ||
| file_paths: The list of full file paths to be removed from the table | ||
| snapshot_properties: Custom properties to be added to the snapshot summary | ||
| branch: Branch to delete files from | ||
|
|
||
| Raises: | ||
| ValueError: If file_paths contains duplicates | ||
| ValueError: If any file paths are not found in the table | ||
| """ | ||
| unique_file_paths = set(file_paths) | ||
|
|
||
| if len(file_paths) != len(unique_file_paths): | ||
| raise ValueError("File paths must be unique") | ||
|
|
||
| data_files = _get_data_files_from_snapshot( | ||
| table_metadata=self.table_metadata, file_paths=unique_file_paths, io=self._table.io, branch=branch | ||
| ) | ||
|
|
||
| missing_files = unique_file_paths - set(data_files.keys()) | ||
| if missing_files: | ||
| raise ValueError(f"Cannot delete files that are not referenced by table, files: {', '.join(sorted(missing_files))}") | ||
|
|
||
| with self.update_snapshot(snapshot_properties=snapshot_properties, branch=branch).overwrite() as overwrite_snapshot: | ||
| for data_file in data_files.values(): | ||
| overwrite_snapshot.delete_data_file(data_file) | ||
|
|
||
| def update_spec(self) -> UpdateSpec: | ||
| """Create a new UpdateSpec to update the partitioning of the table. | ||
|
|
||
|
|
@@ -1506,6 +1541,31 @@ def add_files( | |
| branch=branch, | ||
| ) | ||
|
|
||
| def delete_files( | ||
| self, | ||
| file_paths: list[str], | ||
| snapshot_properties: dict[str, str] = EMPTY_DICT, | ||
| branch: str | None = MAIN_BRANCH, | ||
| ) -> None: | ||
| """ | ||
| Shorthand API for removing data files from the table by their paths. | ||
|
|
||
| Args: | ||
| file_paths: The list of full file paths to be removed from the table | ||
| snapshot_properties: Custom properties to be added to the snapshot summary | ||
| branch: Branch to delete files from | ||
|
|
||
| Raises: | ||
| ValueError: If file_paths contains duplicates | ||
| ValueError: If any file paths are not found in the table | ||
| """ | ||
| with self.transaction() as tx: | ||
| tx.delete_files( | ||
| file_paths=file_paths, | ||
| snapshot_properties=snapshot_properties, | ||
| branch=branch, | ||
| ) | ||
|
|
||
| def update_spec(self, case_sensitive: bool = True) -> UpdateSpec: | ||
| return UpdateSpec(Transaction(self, autocommit=True), case_sensitive=case_sensitive) | ||
|
|
||
|
|
@@ -2175,3 +2235,21 @@ def _parquet_files_to_data_files(table_metadata: TableMetadata, file_paths: list | |
| futures = [executor.submit(parquet_file_to_data_file, io, table_metadata, file_path) for file_path in file_paths] | ||
|
|
||
| return [f.result() for f in futures if f.result()] | ||
|
|
||
|
|
||
| def _get_data_files_from_snapshot( | ||
| table_metadata: TableMetadata, file_paths: set[str], io: FileIO, branch: str | None = MAIN_BRANCH | ||
| ) -> dict[str, DataFile]: | ||
| snapshot = table_metadata.snapshot_by_name(branch) if branch else table_metadata.current_snapshot() | ||
| if snapshot is None: | ||
| return {} | ||
|
|
||
| result: dict[str, DataFile] = {} | ||
| for manifest in snapshot.manifests(io): | ||
| if manifest.content == ManifestContent.DATA: | ||
|
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. Should we also drop related deletes? |
||
| for entry in manifest.fetch_manifest_entry(io, discard_deleted=True): | ||
| if entry.data_file.file_path in file_paths: | ||
| result[entry.data_file.file_path] = entry.data_file | ||
| if len(result) == len(file_paths): | ||
| return result | ||
| return result | ||
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.
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.
Can we use metadata tables for this? I'd like to avoid functions like these since they do not parallelize