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

ibis support - hand over credentials to ibis backend for a number of destinations #2004

Merged
merged 30 commits into from
Nov 23, 2024

Conversation

sh-rp
Copy link
Collaborator

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

Description

Adds support for handing over our credentials to an ibis backend. Supported destinations are:

    "dlt.destinations.postgres",
    "dlt.destinations.duckdb",
    "dlt.destinations.motherduck",
    "dlt.destinations.filesystem",
    "dlt.destinations.bigquery",
    "dlt.destinations.snowflake",
    "dlt.destinations.redshift",
    "dlt.destinations.mssql",
    "dlt.destinations.synapse",
    "dlt.destinations.clickhouse", 

Discussion at ibis about using ibis only as a query builder: ibis-project/ibis#10452. There also is a question there about setting a default database (they call what we call dataset a database)

@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 7f767b8
🔍 Latest deploy log https://app.netlify.com/sites/dlt-hub-docs/deploys/673cbca7bae10700080dec9c

@@ -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 force-pushed the feat/2003-ibis-support branch from 2adb193 to 5befef9 Compare October 31, 2024 12:10
@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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it seems like there is no way to consistently pre-select a database, and for example setting the schema on connection in snowflake for some reason also does not work, so I am opting now to not set any default schema/database on a connection and we need to tell the user to give the database name for listing and retrieving tables when using the ibis interfaces.

@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 4 times, most recently from ab62c59 to 500b5ff Compare November 7, 2024 15:19
@sh-rp sh-rp changed the title 2003 - PoC: ibis support ibis support - hand over credentials to ibis backend for a number of destinations Nov 11, 2024
@sh-rp sh-rp force-pushed the feat/2003-ibis-support branch from 54af827 to cbf98d6 Compare November 12, 2024 12:50
@sh-rp sh-rp marked this pull request as ready for review November 12, 2024 13:35
@sh-rp sh-rp requested a review from rudolfix November 12, 2024 13:35
Copy link
Collaborator

@rudolfix rudolfix left a comment

Choose a reason for hiding this comment

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

very good. I'm for keeping ibis there if we are going to implement ibis-based dataset.

@@ -131,7 +131,7 @@ def double_items():
yield pipeline

# NOTE: we need to drop pipeline data here since we are keeping the pipelines around for the whole module
drop_pipeline_data(pipeline)
# drop_pipeline_data(pipeline)
Copy link
Collaborator

Choose a reason for hiding this comment

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

all ok here?

con = ibis.duckdb.from_connection(duck)

# NOTE: there seems to be no standardized way to set the current dataset / schema in ibis
con.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.

I do not need this here. ibis uses fully qualified names so con.dataset.table

dlt/common/libs/ibis.py Show resolved Hide resolved
@@ -228,6 +237,17 @@ def __init__(
self._sql_client: SqlClientBase[Any] = None
self._schema: Schema = None

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.

I think this is OK. when we have full ibis support we can return Dataset with ibis implementation.

Copy link
Collaborator

Choose a reason for hiding this comment

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

btw. maybe you should overload

def _dataset(self, dataset_type: TDatasetType = "dbapi") -> SupportsReadableDataset:

to return different dataset impl. per engine. or just return DBAPI one for now. otherwise people won't see 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 quite sure what you mean. But I was thinking we should have dataset_type be "dbapi", "ibis" and "auto". If it is auto it will select dbapi if there are no ibis expressions available and ibis if there is. That said, dbapi should have a different name, since the ibis expression based one also uses dbapi.



# helpers
def _get_client_for_destination(
Copy link
Collaborator

Choose a reason for hiding this comment

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

we need to extract _get_destination_clients and _get_destination_client_initial_config from Pipeline and put it here. there are many corner case they do (ie. we support a multi dataset layout for pipelines with many schemas, also empty dataset names are allowed ie. on clikchouse)

you can do it now or we do it in next ticket that will wrap up all work before we release dataset functionality

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 did this, let's see if the tests pass. I maybe we should also put the function for creating a dataset_name there?

tests/load/test_read_interfaces.py Show resolved Hide resolved
@sh-rp sh-rp force-pushed the feat/2003-ibis-support branch 3 times, most recently from c7f44d5 to bdaf034 Compare November 13, 2024 10:23
@sh-rp sh-rp force-pushed the feat/2003-ibis-support branch from aa8640f to cf954ca Compare November 18, 2024 10:14
@sh-rp sh-rp linked an issue Nov 19, 2024 that may be closed by this pull request
5 tasks
Copy link
Collaborator

@rudolfix rudolfix left a comment

Choose a reason for hiding this comment

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

LGTM!

@rudolfix rudolfix merged commit dfde071 into devel Nov 23, 2024
58 of 59 checks passed
@rudolfix rudolfix deleted the feat/2003-ibis-support branch November 23, 2024 21:49
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.

Release Dataset Feature ibis support for datasets / destinations
2 participants