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

feat(python): Support passing instantiated adbc/alchemy connection objects to write_database #16099

Merged
merged 3 commits into from
May 9, 2024

Conversation

alexander-beedie
Copy link
Collaborator

@alexander-beedie alexander-beedie commented May 7, 2024

Closes #16095.
Closes #16053.
Closes #15538.

Also: ref #7852.


So, The Great Database Write Rewrite™ is still pending, but this PR closes a number of outstanding Issues and enables various opt-in speedups to ensure that we can take better advantage of the underlying engines that we currently call in to (sqlalchemy/pandas and adbc).

ℹ️ This is a non-breaking change; all existing usage is unaffected.

Updates

  • Can now supply an instantiated SQLAlchemy or ADBC connection directly; no longer limited to passing just the URI.
  • This allows such connections to be established with specific optimisations, such as fast_executemany for pyodbc mssql connections, and other per-connection configuration parameters.
  • Adds a new engine_options parameter that gets passed down to the chosen engine's underlying insert method (eg: pandas' to_sql if using SQLAlchemy, or adbc_ingest if using ADBC).

Examples

Establish a pyodbc connection using "fast_executemany=True" and insert using that:

from sqlalchemy import create_engine
import polars as pl

pyodbc_uri = (
    "mssql+pyodbc://user:pass@server:1433/test?"
    "driver=ODBC+Driver+18+for+SQL+Server"
)
conn = create_engine(pyodbc_uri, fast_executemany=True)

df.write_database(
    table_name="target_table",
    connection=conn,
)

Use a regular URI connection and pass "method='multi'" down to the to_sql insert method:

df.write_database(
    connection="hive://user:pass@server:port/database",
    table_name="target_table",
    engine="sqlalchemy",
    engine_options={"method": "multi"},
)

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars labels May 7, 2024
@alexander-beedie alexander-beedie force-pushed the write-database-connection branch from 2bbdf1d to a75500c Compare May 7, 2024 13:34
@alexander-beedie alexander-beedie added the A-io-database Area: reading/writing to databases label May 7, 2024
@alexander-beedie alexander-beedie force-pushed the write-database-connection branch 2 times, most recently from f6d946a to b97beb0 Compare May 7, 2024 14:05
@alexander-beedie alexander-beedie force-pushed the write-database-connection branch from b97beb0 to 3f8f6b2 Compare May 7, 2024 15:25
@ritchie46
Copy link
Member

So, The Great Database Write Rewrite™ is still pending

I want your magic on this one so bad! ^^

Closes #16095.
Closes #16053.
Closes #15538.
Also: ref #7852.

Quite some birds in one stone. 👏

@ritchie46 ritchie46 merged commit 9aecfd3 into pola-rs:main May 9, 2024
19 checks passed
@alexander-beedie alexander-beedie deleted the write-database-connection branch May 9, 2024 05:41
@ma-dentalcorp
Copy link

ma-dentalcorp commented May 13, 2024

@alexander-beedie Thank you for the updates. To confirm, these updates are not yet available, right?
I upgraded my polars to latest version 0.20.25 but can't seem to use these features yet.

@alexander-beedie
Copy link
Collaborator Author

@alexander-beedie Thank you for the updates. To confirm, these updates are not yet available, right?

Correct - coming in 0.20.26 (soon) ;)

@MrAlexBailey
Copy link

Do you need a documentation update for this? Happy to make a PR if so.

@alexander-beedie
Copy link
Collaborator Author

Do you need a documentation update for this? Happy to make a PR if so.

@MrAlexBailey An update to the User Guide docs would be very welcome! (The function docstring itself has some examples/explanation, but a guide entry with more detail would be great 😎)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-io-database Area: reading/writing to databases enhancement New feature or an improvement of an existing feature python Related to Python Polars
Projects
None yet
4 participants