Skip to content

Commit c5ac479

Browse files
Fix athena.read_sql_query for empty table and chunk size not returning an empty frame generator (#1685)
1 parent fe955b7 commit c5ac479

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

awswrangler/athena/_read.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,18 @@ def _fetch_parquet_result(
103103
if not paths:
104104
if not temp_table_fqn:
105105
raise exceptions.EmptyDataFrame("Query would return untyped, empty dataframe.")
106+
106107
database, temp_table_name = map(lambda x: x.replace('"', ""), temp_table_fqn.split("."))
107108
dtype_dict = catalog.get_table_types(database=database, table=temp_table_name, boto3_session=boto3_session)
108109
df = pd.DataFrame(columns=list(dtype_dict.keys()))
109110
df = cast_pandas_with_athena_types(df=df, dtype=dtype_dict)
110111
df = _apply_query_metadata(df=df, query_metadata=query_metadata)
112+
113+
if chunked:
114+
return (df,)
115+
111116
return df
117+
112118
ret = s3.read_parquet(
113119
path=paths,
114120
use_threads=use_threads,

tests/test_athena.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,19 @@ def test_read_sql_query_wo_results(path, glue_database, glue_table):
595595
ensure_athena_query_metadata(df=df, ctas_approach=False, encrypted=False)
596596

597597

598+
@pytest.mark.parametrize("ctas_approach", [False, True])
599+
def test_read_sql_query_wo_results_chunked(path, glue_database, glue_table, ctas_approach):
600+
wr.catalog.create_parquet_table(database=glue_database, table=glue_table, path=path, columns_types={"c0": "int"})
601+
sql = f"SELECT * FROM {glue_database}.{glue_table}"
602+
603+
counter = 0
604+
for df in wr.athena.read_sql_query(sql, database=glue_database, ctas_approach=ctas_approach, chunksize=100):
605+
assert df.empty
606+
counter += 1
607+
608+
assert counter == 1
609+
610+
598611
@pytest.mark.xfail()
599612
def test_read_sql_query_wo_results_ctas(path, glue_database, glue_table):
600613
wr.catalog.create_parquet_table(database=glue_database, table=glue_table, path=path, columns_types={"c0": "int"})

0 commit comments

Comments
 (0)