Skip to content

Commit

Permalink
Fix closing connection dbapi.get_pandas_df (#23452)
Browse files Browse the repository at this point in the history
  • Loading branch information
hubert-pietron authored May 31, 2022
1 parent 41e94b4 commit ab1f637
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions airflow/hooks/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@ 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, *, chunksize, **kwargs):
"""
Executes the sql and returns a generator
:param sql: the sql statement to be executed (str) or a list of
sql statements to execute
:param parameters: The parameters to render the SQL query with
:param chunksize: number of rows to include in each chunk
:param kwargs: (optional) passed into pandas.io.sql.read_sql method
"""
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, chunksize=chunksize, **kwargs)

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

0 comments on commit ab1f637

Please sign in to comment.