Skip to content
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

Dataset pull fixes #560

Merged
merged 7 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/datachain/catalog/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
from datachain.node import DirType, Node, NodeWithPath
from datachain.nodes_thread_pool import NodesThreadPool
from datachain.remote.studio import StudioClient
from datachain.sql.types import DateTime, SQLType, String
from datachain.sql.types import DateTime, SQLType
from datachain.utils import (
DataChainDir,
batched,
Expand Down Expand Up @@ -196,11 +196,6 @@ def fix_columns(self, df) -> None:
for c in [c for c, t in self.schema.items() if t == DateTime]:
df[c] = pd.to_datetime(df[c], unit="s")

# strings are represented as binaries in parquet export so need to
# decode it back to strings
for c in [c for c, t in self.schema.items() if t == String]:
df[c] = df[c].str.decode("utf-8")

def do_task(self, urls):
import lz4.frame
import pandas as pd
Expand Down Expand Up @@ -1403,6 +1398,7 @@ def _instantiate_dataset():
query_script=remote_dataset_version.query_script,
create_rows=True,
columns=columns,
feature_schema=remote_dataset_version.feature_schema,
validate_version=False,
)

Expand Down
6 changes: 4 additions & 2 deletions src/datachain/data_storage/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ def query(self, q):


class DataTable:
MAX_RANDOM = 2**63 - 1

def __init__(
self,
name: str,
Expand Down Expand Up @@ -269,8 +271,8 @@ def update(self):
def delete(self):
return self.apply_conditions(self.table.delete())

@staticmethod
def sys_columns():
@classmethod
def sys_columns(cls):
return [
sa.Column("sys__id", Int, primary_key=True),
sa.Column(
Expand Down
2 changes: 2 additions & 0 deletions src/datachain/sql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@

def json(self, value):
if isinstance(value, str):
if value == "":
return {}

Check warning on line 444 in src/datachain/sql/types.py

View check run for this annotation

Codecov / codecov/patch

src/datachain/sql/types.py#L444

Added line #L444 was not covered by tests
return orjson.loads(value)
return value

Expand Down
7 changes: 2 additions & 5 deletions tests/func/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
import sqlalchemy as sa

from datachain.data_storage.sqlite import SQLiteWarehouse
from datachain.data_storage.schema import DataTable
from datachain.dataset import DatasetDependencyType, DatasetStatus
from datachain.error import (
DatasetInvalidVersionError,
Expand Down Expand Up @@ -827,10 +827,7 @@ def test_row_random(cloud_test_catalog):
# Random values are unique
assert len(set(random_values)) == len(random_values)

if isinstance(catalog.warehouse, SQLiteWarehouse):
RAND_MAX = 2**63 # noqa: N806
else:
RAND_MAX = 2**64 # noqa: N806
RAND_MAX = DataTable.MAX_RANDOM # noqa: N806
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Q] Won't this test fail when the number drawn by clickhouse is > 2^63?

And if my math is correct the chances of that happening is:

((2^64) - (2^63)) / (2^64) = 0.5 or 50%

Copy link
Contributor Author

@ilongin ilongin Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Clickhouse we have explicit limit to 2^63-1 https://github.com/iterative/studio/pull/10860/files#diff-1136c5032c82c12345c3103e18093d3256798daa2d4b13e5f5f2d5e8670ac32bR328

In general, now we actually know what is the range of that random number regardless of DB implementation.


# Values are drawn uniformly from range(2**63)
assert 0 <= min(random_values) < 0.4 * RAND_MAX
Expand Down
10 changes: 4 additions & 6 deletions tests/func/test_pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,17 @@ def _adapt_row(row):
"""
adapted = {}
for k, v in row.items():
if isinstance(v, str):
adapted[k] = v.encode("utf-8")
elif isinstance(v, datetime):
if isinstance(v, datetime):
adapted[k] = v.timestamp()
elif v is None:
adapted[k] = b""
adapted[k] = ""
else:
adapted[k] = v

adapted["sys__id"] = 1
adapted["sys__rand"] = 1
adapted["file__location"] = b""
adapted["file__source"] = b"s3://dogs"
adapted["file__location"] = ""
adapted["file__source"] = "s3://dogs"
return adapted

dog_entries = [_adapt_row(e) for e in dog_entries]
Expand Down