-
Notifications
You must be signed in to change notification settings - Fork 340
feat: delete orphaned files #1958
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
jayceslesar
wants to merge
32
commits into
apache:main
Choose a base branch
from
jayceslesar:feat/orphan-files
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.
Open
Changes from 6 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
9dcb580
feat: delete orphaned files
jayceslesar e43505c
simpler and a test
jayceslesar eed5ea8
remove
jayceslesar 8cca600
updates from review!
jayceslesar 75b1240
include dry run and older than
jayceslesar 6379480
add case for dry run
jayceslesar 0c2822e
use .path so we get paths pack
jayceslesar aaf8fc2
actually pass in iterable
jayceslesar b09641b
capture manifest_list files
jayceslesar beec233
refactor into `all_known_files`
jayceslesar b888c56
fix type in docstring
jayceslesar ff461ed
mildly more readable
jayceslesar 3b3b10e
beef up tests
jayceslesar a62c8cf
make `older_than` required
jayceslesar 07cbf1b
move under `optimize` namespace
jayceslesar 54e1e00
add some better logging about what was/was not deleted
jayceslesar 7c780d3
Merge branch 'main' into feat/orphan-files
jayceslesar 9b6c9ed
Merge branch 'main' into feat/orphan-files
jayceslesar 34d10b9
rename optimize -> maintenance
jayceslesar 0335957
make orphaned_files private
jayceslesar 9f8145c
correctly coerce list
jayceslesar fbdcbd3
add metadata files
jayceslesar 85b4ab3
Merge branch 'main' into feat/orphan-files
jayceslesar c414df8
Merge branch 'main' into feat/orphan-files
jayceslesar aa9d536
Merge branch 'main' into feat/orphan-files
jayceslesar b4c14fc
fix test
jayceslesar f4d98d2
allow older_than to be None
jayceslesar acd8ed6
Merge branch 'main' into feat/orphan-files
jayceslesar 2a9c607
add partition statistics
jayceslesar aae92bc
safer
jayceslesar 756e199
Merge branch 'main' into feat/orphan-files
jayceslesar ad5387a
work with both file IO's
jayceslesar 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 |
---|---|---|
@@ -0,0 +1,117 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
import os | ||
from datetime import datetime, timedelta | ||
from pathlib import Path, PosixPath | ||
from unittest.mock import PropertyMock, patch | ||
|
||
import pyarrow as pa | ||
import pytest | ||
|
||
from pyiceberg.catalog import Catalog | ||
from pyiceberg.schema import Schema | ||
from pyiceberg.types import IntegerType, NestedField, StringType | ||
from tests.catalog.test_base import InMemoryCatalog | ||
|
||
|
||
@pytest.fixture | ||
def catalog(tmp_path: PosixPath) -> InMemoryCatalog: | ||
catalog = InMemoryCatalog("test.in_memory.catalog", warehouse=tmp_path.absolute().as_posix()) | ||
catalog.create_namespace("default") | ||
return catalog | ||
|
||
|
||
def test_delete_orphaned_files(catalog: Catalog) -> None: | ||
identifier = "default.test_delete_orphaned_files" | ||
|
||
schema = Schema( | ||
NestedField(1, "city", StringType(), required=True), | ||
NestedField(2, "inhabitants", IntegerType(), required=True), | ||
# Mark City as the identifier field, also known as the primary-key | ||
identifier_field_ids=[1], | ||
) | ||
|
||
tbl = catalog.create_table(identifier, schema=schema) | ||
|
||
arrow_schema = pa.schema( | ||
[ | ||
pa.field("city", pa.string(), nullable=False), | ||
pa.field("inhabitants", pa.int32(), nullable=False), | ||
] | ||
) | ||
|
||
df = pa.Table.from_pylist( | ||
[ | ||
{"city": "Drachten", "inhabitants": 45019}, | ||
{"city": "Drachten", "inhabitants": 45019}, | ||
], | ||
schema=arrow_schema, | ||
) | ||
tbl.append(df) | ||
|
||
orphaned_file = Path(tbl.location()) / "orphan.txt" | ||
|
||
orphaned_file.touch() | ||
assert orphaned_file.exists() | ||
|
||
# assert no files deleted if dry run... | ||
tbl.delete_orphaned_files(dry_run=True) | ||
assert orphaned_file.exists() | ||
|
||
# should not delete because it was just created... | ||
tbl.delete_orphaned_files() | ||
assert orphaned_file.exists() | ||
|
||
# modify creation date to be older than 3 days | ||
five_days_ago = (datetime.now() - timedelta(days=5)).timestamp() | ||
os.utime(orphaned_file, (five_days_ago, five_days_ago)) | ||
|
||
|
||
def test_delete_orphaned_files_with_invalid_file_doesnt_error(catalog: Catalog) -> None: | ||
identifier = "default.test_delete_orphaned_files" | ||
|
||
schema = Schema( | ||
NestedField(1, "city", StringType(), required=True), | ||
NestedField(2, "inhabitants", IntegerType(), required=True), | ||
# Mark City as the identifier field, also known as the primary-key | ||
identifier_field_ids=[1], | ||
) | ||
|
||
tbl = catalog.create_table(identifier, schema=schema) | ||
|
||
arrow_schema = pa.schema( | ||
[ | ||
pa.field("city", pa.string(), nullable=False), | ||
pa.field("inhabitants", pa.int32(), nullable=False), | ||
] | ||
) | ||
|
||
df = pa.Table.from_pylist( | ||
[ | ||
{"city": "Drachten", "inhabitants": 45019}, | ||
{"city": "Drachten", "inhabitants": 45019}, | ||
], | ||
schema=arrow_schema, | ||
) | ||
tbl.append(df) | ||
|
||
file_that_does_not_exist = "foo/bar.baz" | ||
with patch.object(type(tbl), "inspect", new_callable=PropertyMock) as mock_inspect: | ||
mock_inspect.return_value.orphaned_files = lambda location, older_than: {file_that_does_not_exist} | ||
with patch.object(tbl.io, "delete", wraps=tbl.io.delete) as mock_delete: | ||
tbl.delete_orphaned_files() | ||
mock_delete.assert_called_with(file_that_does_not_exist) |
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.
Uh oh!
There was an error while loading. Please reload this page.