gfw.common.bigquery.QueryResult#

class QueryResult(query_job, row_iterator)[source]#

Wrapper around bigquery.job.QueryJob with access to results.

This class encapsulates query_job and row_iterator instances, exposing rows via iteration and providing convenience methods like iter_as_dicts() and tolist().

Parameters:
  • query_job (QueryJob) – The original QueryJob, which can be used to access job metadata such as session IDs, job statistics, and more.

  • row_iterator (RowIterator) – The RowIterator returned by the query job.

Example

result = bq_client.run_query("SELECT * FROM my_table")

# Iterate raw rows
for row in result:
    print(row)

# Iterate as dicts
for row in result.iter_as_dicts():
    print(row)

# Materialize
rows = result.tolist()
rows_as_dicts = result.tolist(as_dicts=True)

# Access job metadata
print(result.query_job.job_id)
print(result.session_id)

Methods

iter_as_dicts

Iterates over rows as dictionaries.

tolist

Materializes all rows into a list.

Attributes

session_id

Returns the session_id of the job, or None if not available.

query_job

The encapsulated QueryJob instance.

row_iterator

The RowIterator returned by the query job.

query_job: QueryJob#

The encapsulated QueryJob instance.

row_iterator: RowIterator#

The RowIterator returned by the query job.

property session_id: str | None#

Returns the session_id of the job, or None if not available.

iter_as_dicts()[source]#

Iterates over rows as dictionaries.

Return type:

Iterator[Dict[str, Any]]

tolist(as_dicts=False)[source]#

Materializes all rows into a list.

Parameters:

as_dicts (bool) – If True, rows are converted to dictionaries. Defaults to False.

Returns:

A list of Row objects, or dictionaries if as_dicts=True.

Return type:

List[Row | Dict[str, Any]]