Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add BigQuery row to dictionary conversion in list #176

Merged
merged 6 commits into from
May 23, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions prefect_gcp/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from google.cloud.bigquery.dbapi.cursor import Cursor
from google.cloud.bigquery.table import Row
from google.cloud.exceptions import NotFound
import pandas as pd
except ModuleNotFoundError:
pass

Expand All @@ -52,8 +53,9 @@ async def bigquery_query(
to_dataframe: bool = False,
job_config: Optional[dict] = None,
project: Optional[str] = None,
rows_as_dict: Optional[bool] = False,
davidschrooten marked this conversation as resolved.
Show resolved Hide resolved
location: str = "US",
) -> List["Row"]:
) -> Union[List["Row"], List[dict], pd.DataFrame]:
"""
Runs a BigQuery query.

Expand All @@ -78,6 +80,7 @@ async def bigquery_query(
(e.g., dataset references will be rejected).
project: The project to initialize the BigQuery Client with; if not
provided, will default to the one inferred from your credentials.
rows_as_dict: Converts bigquery client Row to python dictionaries in returned list.
location: Location of the dataset that will be queried.

Returns:
Expand Down Expand Up @@ -159,7 +162,10 @@ def example_bigquery_query_flow():
if to_dataframe:
return result.to_dataframe()
else:
return list(result)
if rows_as_dict:
return [dict(row) for row in list(result)]
else:
return list(result)


@task
Expand Down