Skip to content

Commit

Permalink
storage: index as a dir if no glob (#108)
Browse files Browse the repository at this point in the history
* storage: index as a dir if no glob

* add tests for enlist source on globs, dirs, file paths
  • Loading branch information
shcheklein authored Jul 25, 2024
1 parent e5be7e6 commit b9e60b4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/datachain/catalog/catalog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ast
import glob
import io
import json
import logging
Expand Down Expand Up @@ -709,7 +710,12 @@ def enlist_source(

client_config = client_config or self.client_config
client, path = self.parse_url(source, **client_config)
prefix = posixpath.dirname(path)
stem = os.path.basename(os.path.normpath(path))
prefix = (
posixpath.dirname(path)
if glob.has_magic(stem) or client.fs.isfile(source)
else path
)
storage_dataset_name = Storage.dataset_name(
client.uri, posixpath.join(prefix, "")
)
Expand Down
39 changes: 39 additions & 0 deletions tests/func/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,45 @@ def test_storage_stats(cloud_test_catalog):
assert stats.size == 15


@pytest.mark.parametrize("cloud_type", ["s3", "azure", "gs"], indirect=True)
def test_enlist_source_handles_slash(cloud_test_catalog):
catalog = cloud_test_catalog.catalog
src_uri = cloud_test_catalog.src_uri

catalog.enlist_source(f"{src_uri}/dogs", ttl=1234)
stats = catalog.storage_stats(src_uri)
assert stats.num_objects == len(DEFAULT_TREE["dogs"])
assert stats.size == 15

catalog.enlist_source(f"{src_uri}/dogs/", ttl=1234, force_update=True)
stats = catalog.storage_stats(src_uri)
assert stats.num_objects == len(DEFAULT_TREE["dogs"])
assert stats.size == 15


@pytest.mark.parametrize("cloud_type", ["s3", "azure", "gs"], indirect=True)
def test_enlist_source_handles_glob(cloud_test_catalog):
catalog = cloud_test_catalog.catalog
src_uri = cloud_test_catalog.src_uri

catalog.enlist_source(f"{src_uri}/dogs/*.jpg", ttl=1234)
stats = catalog.storage_stats(src_uri)

assert stats.num_objects == len(DEFAULT_TREE["dogs"])
assert stats.size == 15


@pytest.mark.parametrize("cloud_type", ["s3", "azure", "gs"], indirect=True)
def test_enlist_source_handles_file(cloud_test_catalog):
catalog = cloud_test_catalog.catalog
src_uri = cloud_test_catalog.src_uri

catalog.enlist_source(f"{src_uri}/dogs/dog1", ttl=1234)
stats = catalog.storage_stats(src_uri)
assert stats.num_objects == len(DEFAULT_TREE["dogs"])
assert stats.size == 15


@pytest.mark.parametrize("from_cli", [False, True])
def test_garbage_collect(cloud_test_catalog, from_cli, capsys):
catalog = cloud_test_catalog.catalog
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/lib/test_datachain.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ def test_parse_tabular_partitions(tmp_dir, catalog):

def test_parse_tabular_empty(tmp_dir, catalog):
path = tmp_dir / "test.parquet"
with pytest.raises(DataChainParamsError):
with pytest.raises(FileNotFoundError):
DataChain.from_storage(path.as_uri()).parse_tabular()


Expand Down

0 comments on commit b9e60b4

Please sign in to comment.