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 5 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
31 changes: 31 additions & 0 deletions src/datachain/sql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,22 @@
return read_converter(dialect).int32(value)


class UInt32(Int):
def load_dialect_impl(self, dialect):
return converter(dialect).uint32()

@staticmethod
def default_value(dialect):
return type_defaults(dialect).uint32()

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

View check run for this annotation

Codecov / codecov/patch

src/datachain/sql/types.py#L196

Added line #L196 was not covered by tests

@staticmethod
def db_default_value(dialect):
return db_defaults(dialect).uint32()

def on_read_convert(self, value, dialect):
return read_converter(dialect).uint32(value)


class Int64(Int):
def load_dialect_impl(self, dialect):
return converter(dialect).int64()
Expand Down Expand Up @@ -395,6 +411,9 @@
def int32(self, value):
return value

def uint32(self, value):
return value

def int64(self, value):
return value

Expand All @@ -421,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 All @@ -446,6 +467,9 @@
def int32(self):
return self.int()

def uint32(self):
return self.int()

def int64(self):
return self.int()

Expand Down Expand Up @@ -487,6 +511,9 @@
def int32(self):
return None

def uint32(self):
return None

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

View check run for this annotation

Codecov / codecov/patch

src/datachain/sql/types.py#L515

Added line #L515 was not covered by tests

def int64(self):
return None

Expand Down Expand Up @@ -528,6 +555,9 @@
def int32(self):
return self.int()

def uint32(self):
return self.int()

def int64(self):
return self.int()

Expand Down Expand Up @@ -561,6 +591,7 @@
Boolean,
Int,
Int32,
UInt32,
Int64,
UInt64,
Float,
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
2 changes: 2 additions & 0 deletions tests/unit/lib/test_signal_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
Int32,
Int64,
String,
UInt32,
UInt64,
)

Expand Down Expand Up @@ -721,6 +722,7 @@ def test_mutate_change_type():
[Boolean, bool],
[Int, int],
[Int32, int],
[UInt32, int],
[Int64, int],
[UInt64, int],
[Float, float],
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test_data_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Int32,
Int64,
String,
UInt32,
UInt64,
)
from tests.utils import (
Expand Down Expand Up @@ -173,6 +174,7 @@ def run_convert_type(value, sql_type):
[Boolean(), False],
[Int(), 0],
[Int32(), 0],
[UInt32(), 0],
[Int64(), 0],
[UInt64(), 0],
[Float(), lambda val: math.isnan(val)],
Expand Down