Skip to content

Commit

Permalink
Convert custom columns types in dataset_select_paginated (#339)
Browse files Browse the repository at this point in the history
* Convert custom columns types in dataset_select_paginated

* Move 'dataset_rows_select' to base Warehouse class

---------

Co-authored-by: skshetry <[email protected]>
  • Loading branch information
dreadatour and skshetry authored Aug 22, 2024
1 parent dfa2068 commit 0eb7959
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/datachain/data_storage/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def convert_rows_custom_column_types(
columns: "ColumnCollection[str, ColumnElement[Any]]",
rows: Iterator[tuple[Any, ...]],
dialect: "Dialect",
):
) -> Iterator[tuple[Any, ...]]:
"""
This function converts values of rows columns based on their types which are
defined in columns. We are only converting column values for which types are
Expand Down
11 changes: 1 addition & 10 deletions src/datachain/data_storage/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
from datachain.data_storage import AbstractDBMetastore, AbstractWarehouse
from datachain.data_storage.db_engine import DatabaseEngine
from datachain.data_storage.id_generator import AbstractDBIDGenerator
from datachain.data_storage.schema import (
DefaultSchema,
convert_rows_custom_column_types,
)
from datachain.data_storage.schema import DefaultSchema
from datachain.dataset import DatasetRecord
from datachain.error import DataChainError
from datachain.sql.sqlite import create_user_defined_sql_functions, sqlite_dialect
Expand Down Expand Up @@ -651,12 +648,6 @@ def create_dataset_rows_table(
self.db.create_table(table, if_not_exists=if_not_exists)
return table

def dataset_rows_select(self, select_query: Select, **kwargs):
rows = self.db.execute(select_query, **kwargs)
yield from convert_rows_custom_column_types(
select_query.selected_columns, rows, sqlite_dialect
)

def get_dataset_sources(
self, dataset: DatasetRecord, version: int
) -> list[StorageURI]:
Expand Down
17 changes: 12 additions & 5 deletions src/datachain/data_storage/warehouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from tqdm import tqdm

from datachain.client import Client
from datachain.data_storage.schema import convert_rows_custom_column_types
from datachain.data_storage.serializer import Serializable
from datachain.dataset import DatasetRecord
from datachain.node import DirType, DirTypeGroup, Entry, Node, NodeWithPath, get_path
Expand Down Expand Up @@ -226,7 +227,7 @@ def dataset_select_paginated(
if limit < page_size:
paginated_query = paginated_query.limit(None).limit(limit)

results = self.db.execute(paginated_query.offset(offset))
results = self.dataset_rows_select(paginated_query.offset(offset))

processed = False
for row in results:
Expand Down Expand Up @@ -309,12 +310,18 @@ def merge_dataset_rows(
Merge results should not contain duplicates.
"""

@abstractmethod
def dataset_rows_select(self, select_query: sa.sql.selectable.Select, **kwargs):
def dataset_rows_select(
self,
query: sa.sql.selectable.Select,
**kwargs,
) -> Iterator[tuple[Any, ...]]:
"""
Method for fetching dataset rows from database. This is abstract since
in some DBs we need to use special settings
Fetch dataset rows from database.
"""
rows = self.db.execute(query, **kwargs)
yield from convert_rows_custom_column_types(
query.selected_columns, rows, self.db.dialect
)

@abstractmethod
def get_dataset_sources(
Expand Down

0 comments on commit 0eb7959

Please sign in to comment.