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

ENH: add optional logging to read_sql #55797

Open
1 of 3 tasks
jimenezj8 opened this issue Nov 1, 2023 · 4 comments
Open
1 of 3 tasks

ENH: add optional logging to read_sql #55797

jimenezj8 opened this issue Nov 1, 2023 · 4 comments
Labels
Enhancement IO SQL to_sql, read_sql, read_sql_query Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@jimenezj8
Copy link

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

It would be a nice QoL upgrade to be able to pass any additional, optional, params to calls to pandas.read_sql which will be passed to any call to sqlalchemy.create_engine. My particular use case is that I would like to pass echo=True to pd.read_sql so that the executed statements would be logged automatically, when using a connection string rather than an existing SQLAlchemy Connection.

Feature Description

Add a new parameter to pandas.read_sql, either connect_kwargs or echo. I'll provide an example for adding connect_kwargs

# pandas/pandas/io/sql.py

class SQLDatabase(..., **connect_kwargs):  
    ...
    if isinstance(con, str):  # line 1576
        con = create_engine(con, **connect_kwargs)
    ...

def pandasSQL_builder(..., **connect_kwargs) -> PandasSQL:
    ...
    if sqlalchemy is not None and isinstance(con, (str, sqlalchemy.engine.Connectable)):  # line 850
        return SQLDatabase(con, schema, need_transaction, **connect_kwargs)
    ...

def read_sql(..., **connect_kwargs):
    ...
    with pandasSQL_builder(con, **connect_kwargs) as pandas_sql:  # line 652
        ...
    ...

Alternative Solutions

No alternative solution, besides using a SQLAlchemy connectable rather than the proposed edits.

Additional Context

No response

@jimenezj8 jimenezj8 added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 1, 2023
@hvsesha
Copy link

hvsesha commented Nov 3, 2023

@jimenezj8 Kindly provide example how to add kwargs

@jimenezj8
Copy link
Author

jimenezj8 commented Nov 4, 2023

@hvsesha here is an example of an updated call to pandas.read_sql:

import pandas as pd

sql_str = "SELECT * FROM example"
con_str = "postgresql://..."

pd.read_sql(sql_str, con=con_str, echo=True)

This works with the implementation example provided in the original description - any kwargs not present on the function signature will be packed into the value **connect_kwargs.

@WillAyd
Copy link
Member

WillAyd commented Nov 6, 2023

Have you looked at SQLAlchemy's logging? Does this not work?

https://docs.sqlalchemy.org/en/20/core/engines.html#configuring-logging

@jimenezj8
Copy link
Author

@WillAyd that works, this suggestion is just to minimize the boilerplate needed to configure logging

@simonjayhawkins simonjayhawkins added the IO SQL to_sql, read_sql, read_sql_query label Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement IO SQL to_sql, read_sql, read_sql_query Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

4 participants