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

Fix closing connection dbapi.get_pandas_df #23452

Merged
merged 18 commits into from
May 31, 2022
Merged
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions airflow/hooks/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ def get_pandas_df(self, sql, parameters=None, **kwargs):
:param parameters: The parameters to render the SQL query with.
:param kwargs: (optional) passed into pandas.io.sql.read_sql method
"""
if 'chunksize' in kwargs:
return self.get_pandas_df_by_chunks(sql, parameters=parameters, **kwargs)

try:
from pandas.io import sql as psql
except ImportError:
Expand All @@ -128,6 +131,15 @@ def get_pandas_df(self, sql, parameters=None, **kwargs):
with closing(self.get_conn()) as conn:
return psql.read_sql(sql, con=conn, params=parameters, **kwargs)

def get_pandas_df_by_chunks(self, sql, parameters=None, **kwargs):
hubert-pietron marked this conversation as resolved.
Show resolved Hide resolved
try:
from pandas.io import sql as psql
except ImportError:
raise Exception("pandas library not installed, run: pip install 'apache-airflow[pandas]'.")

with closing(self.get_conn()) as conn:
yield from psql.read_sql(sql, con=conn, params=parameters, **kwargs)

def get_records(self, sql, parameters=None):
"""
Executes the sql and returns a set of records.
Expand Down