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

2003 - PoC: ibis support #2004

Draft
wants to merge 10 commits into
base: devel
Choose a base branch
from
Draft

2003 - PoC: ibis support #2004

wants to merge 10 commits into from

Conversation

sh-rp
Copy link
Collaborator

@sh-rp sh-rp commented Oct 30, 2024

Description

First draft of ibis support to figure out how we would like to implement this. Please also read the attached ticket for the considered approaches.

Ibis tables should work for all destinations, full ibis connection now is implemented for postgres, filesystem and duckdb in this PoC

Discussion at ibis about using ibis only as a query builder: ibis-project/ibis#10452

@sh-rp sh-rp self-assigned this Oct 30, 2024
@sh-rp sh-rp linked an issue Oct 30, 2024 that may be closed by this pull request
Copy link

netlify bot commented Oct 30, 2024

Deploy Preview for dlt-hub-docs canceled.

Name Link
🔨 Latest commit b3af76b
🔍 Latest deploy log https://app.netlify.com/sites/dlt-hub-docs/deploys/672d043053e5100008985929

@@ -1729,3 +1734,11 @@ def _dataset(self, dataset_type: TDatasetType = "dbapi") -> SupportsReadableData
schema=(self.default_schema if self.default_schema_name else None),
dataset_type=dataset_type,
)

def _ibis(self) -> IbisBackend:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think our original idea of exposing an ibis dataset via the same methods as the dbapi one makes sense, the interface of ibis is completely different to ours.

Copy link
Collaborator

Choose a reason for hiding this comment

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

but you can use overloads on Literal dataset_type

@overload
def _dataset(self, dataset_type: TDatasetType = "dbapi") -> SupportsReadableDataset:
   ...
@overload
def _dataset(self, dataset_type: TDatasetType = "ibis") -> IbisBackend:
   ...

to return different types. hmmm but maybe you are right. we should implement readable dataset interface on ibis.

so maybe we add ibis() on DBAPI dataset? that will return ibis backend? I do not think adding it on a relation makes sense

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I like the overload, I did not know this was possible...

raise NotImplementedError()

ibis = ibis.connect(client.config.credentials.to_native_representation())
# NOTE: there seems to be no standardized way to set the current dataset / schema in ibis
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure where we should put the stuff that converts our credentials into something IBIS understands. client.config.credentials.to_native_representation() could work for many cases, but selecting the default dataset will probably be something destination specific..

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also for the filesystem destination this will work quite differently, because we first need to populate the duckdb database and then attach ibis to it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. part that creates backend should go to dlt/helpers/ibis
  2. part that converts credentials should go to common/libs/ibis
    here we can improve a lot. we already pollute credentials in common with concrete converting functions (like fsspec, delta etc.) we should create some universal mechanism to convert them.

@sh-rp sh-rp changed the title 2003 - Experiment: ibis support 2003 - PoC: ibis support Oct 31, 2024
@@ -165,13 +165,18 @@ def open_connection(self) -> duckdb.DuckDBPyConnection:
# set up dataset
if not self.has_dataset():
self.create_dataset()
print("CREATE")
Copy link
Collaborator

Choose a reason for hiding this comment

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

hmmm

@@ -1729,3 +1734,11 @@ def _dataset(self, dataset_type: TDatasetType = "dbapi") -> SupportsReadableData
schema=(self.default_schema if self.default_schema_name else None),
dataset_type=dataset_type,
)

def _ibis(self) -> IbisBackend:
Copy link
Collaborator

Choose a reason for hiding this comment

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

but you can use overloads on Literal dataset_type

@overload
def _dataset(self, dataset_type: TDatasetType = "dbapi") -> SupportsReadableDataset:
   ...
@overload
def _dataset(self, dataset_type: TDatasetType = "ibis") -> IbisBackend:
   ...

to return different types. hmmm but maybe you are right. we should implement readable dataset interface on ibis.

so maybe we add ibis() on DBAPI dataset? that will return ibis backend? I do not think adding it on a relation makes sense

"""fetch arrow table of first 'chunk_size' items"""
...

def ibis(self, chunk_size: int = None) -> Optional[IbisTable]: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd add it on ReadableDBAPIDataset and return implementation of it in _dataset of pipeline

@@ -42,18 +90,91 @@ def __init__(
self.iter_arrow = self._wrap_iter("iter_arrow") # type: ignore
self.iter_fetch = self._wrap_iter("iter_fetch") # type: ignore

# TODO: where should this go, should cursors support "native" ibis or do we do a conversion somewhere
def ibis(self, chunk_size: int = None) -> Optional[IbisTable]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

this does not make sense... we have stuff materialized. way better to just do df() which has data frame interface

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I removed those

"""iterate over arrow tables of 'chunk_size' items"""
...

def iter_ibis(self, chunk_size: int) -> Generator[IbisTable, None, None]: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

how are you going to implement that? you'll need to create IbisTable on a chunk of data which is lazy evaluated. otherwise does not make sense

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

removed it

raise NotImplementedError()

ibis = ibis.connect(client.config.credentials.to_native_representation())
# NOTE: there seems to be no standardized way to set the current dataset / schema in ibis
Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. part that creates backend should go to dlt/helpers/ibis
  2. part that converts credentials should go to common/libs/ibis
    here we can improve a lot. we already pollute credentials in common with concrete converting functions (like fsspec, delta etc.) we should create some universal mechanism to convert them.


def head(self) -> "ReadableDBAPIRelation":
return self.limit(5)


class ReadableDBAPIDataset(SupportsReadableDataset):
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's move ibis() here. then we hand over the connection and return the ibis backend

Copy link
Collaborator

Choose a reason for hiding this comment

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

can we move schema property to the Protocol?

raise NotImplementedError()

# NOTE: there seems to be no standardized way to set the current dataset / schema in ibis
ibis.raw_sql(f"SET search_path TO {dataset_name};")
Copy link
Collaborator

Choose a reason for hiding this comment

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

why? you just do:
return ibis[dataset_name]
to select namespace.
https://duckdb.org/docs/guides/python/ibis.html

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

where does it say that on this page? In any case it does not work and I also looked for a bit before and could not find any standard way to select the current database (as it is called in ibis). The call the whole thing (in the case of duckdb the file) a catalog and what we call dataset is called database there. You can list the database but not set a default one.. At least I have not found a method.

@rudolfix
Copy link
Collaborator

rudolfix commented Nov 1, 2024

also here you wrote a helper: https://github.com/dlt-hub/dlt/pull/1491/files

@sh-rp sh-rp force-pushed the feat/2003-ibis-support branch 3 times, most recently from 59b0977 to ab62c59 Compare November 7, 2024 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ibis support for datasets / destinations
2 participants