-
Notifications
You must be signed in to change notification settings - Fork 117
read_storage() - filter by name pattern #1285
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
Draft
dmpetrov
wants to merge
5
commits into
main
Choose a base branch
from
feature/pattern-based-filtering
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.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5c69187
read_storage() - filter by name pattern
dmpetrov dc49a22
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9636ba4
linter
dmpetrov 2bf2413
linter2
dmpetrov 5798e9a
Merge branch 'main' into feature/pattern-based-filtering
dmpetrov 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
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,7 +2,6 @@ | |||||||||||||||||
from collections.abc import Sequence | ||||||||||||||||||
from functools import reduce | ||||||||||||||||||
from typing import ( | ||||||||||||||||||
TYPE_CHECKING, | ||||||||||||||||||
Optional, | ||||||||||||||||||
Union, | ||||||||||||||||||
) | ||||||||||||||||||
|
@@ -19,8 +18,35 @@ | |||||||||||||||||
) | ||||||||||||||||||
from datachain.query import Session | ||||||||||||||||||
|
||||||||||||||||||
if TYPE_CHECKING: | ||||||||||||||||||
from .datachain import DataChain | ||||||||||||||||||
from .datachain import C, DataChain | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
def _apply_pattern_filtering( | ||||||||||||||||||
chain: DataChain, pattern: Optional[Union[str, list[str]]], column: str | ||||||||||||||||||
) -> DataChain: | ||||||||||||||||||
"""Apply pattern filtering to a storage chain. | ||||||||||||||||||
|
||||||||||||||||||
Args: | ||||||||||||||||||
chain: The DataChain to filter | ||||||||||||||||||
pattern: Pattern(s) to filter by | ||||||||||||||||||
column: The column name to apply filtering to (e.g., "file") | ||||||||||||||||||
|
||||||||||||||||||
Returns: | ||||||||||||||||||
Filtered DataChain | ||||||||||||||||||
""" | ||||||||||||||||||
if not pattern: | ||||||||||||||||||
return chain | ||||||||||||||||||
|
||||||||||||||||||
pattern_list = pattern if isinstance(pattern, list) else [pattern] | ||||||||||||||||||
filters = [] | ||||||||||||||||||
|
||||||||||||||||||
for pattern_item in pattern_list: | ||||||||||||||||||
filters.append(C(f"{column}.path").glob(f"*{pattern_item}")) | ||||||||||||||||||
|
||||||||||||||||||
Comment on lines
+41
to
+45
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. suggestion (code-quality): Convert for loop into list comprehension (
Suggested change
|
||||||||||||||||||
combined_filter = filters[0] | ||||||||||||||||||
for filter_expr in filters[1:]: | ||||||||||||||||||
combined_filter = combined_filter | filter_expr | ||||||||||||||||||
return chain.filter(combined_filter) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
def read_storage( | ||||||||||||||||||
|
@@ -34,6 +60,7 @@ def read_storage( | |||||||||||||||||
column: str = "file", | ||||||||||||||||||
update: bool = False, | ||||||||||||||||||
anon: Optional[bool] = None, | ||||||||||||||||||
pattern: Optional[Union[str, list[str]]] = None, | ||||||||||||||||||
delta: Optional[bool] = False, | ||||||||||||||||||
delta_on: Optional[Union[str, Sequence[str]]] = ( | ||||||||||||||||||
"file.path", | ||||||||||||||||||
|
@@ -57,6 +84,7 @@ def read_storage( | |||||||||||||||||
column : Created column name. | ||||||||||||||||||
update : force storage reindexing. Default is False. | ||||||||||||||||||
anon : If True, we will treat cloud bucket as public one | ||||||||||||||||||
pattern : Optional pattern(s) to filter by file names like "*.jpg". | ||||||||||||||||||
client_config : Optional client configuration for the storage client. | ||||||||||||||||||
delta: If True, only process new or changed files instead of reprocessing | ||||||||||||||||||
everything. This saves time by skipping files that were already processed in | ||||||||||||||||||
|
@@ -113,6 +141,15 @@ def read_storage( | |||||||||||||||||
], session=session, recursive=True) | ||||||||||||||||||
``` | ||||||||||||||||||
|
||||||||||||||||||
Filter by file patterns: | ||||||||||||||||||
```python | ||||||||||||||||||
# Single pattern | ||||||||||||||||||
chain = dc.read_storage("s3://my-bucket/my-dir", pattern="*.mp3") | ||||||||||||||||||
|
||||||||||||||||||
# Multiple patterns | ||||||||||||||||||
chain = dc.read_storage("s3://my-bucket/my-dir", pattern=["*.mp3", "*.wav"]) | ||||||||||||||||||
``` | ||||||||||||||||||
|
||||||||||||||||||
Note: | ||||||||||||||||||
When using multiple URIs with `update=True`, the function optimizes by | ||||||||||||||||||
avoiding redundant updates for URIs pointing to the same storage location. | ||||||||||||||||||
|
@@ -220,4 +257,4 @@ def lst_fn(ds_name, lst_uri): | |||||||||||||||||
delta_retry=delta_retry, | ||||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
return storage_chain | ||||||||||||||||||
return _apply_pattern_filtering(storage_chain, pattern, column) |
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.
suggestion: Pattern matching may be too permissive for certain use cases.
Consider whether using
*{pattern_item}
could unintentionally match more files than intended, especially if user-supplied patterns already include wildcards.