Query#

Define API Queries.

class google.cloud.bigquery.query.QueryResults(query, client, udf_resources=(), query_parameters=())[source]#

Bases: object

Synchronous job: query tables.

Parameters:
  • query (str) – SQL query string
  • client (google.cloud.bigquery.client.Client) – A client which holds credentials and project configuration for the dataset (which requires a project).
  • udf_resources (tuple) – An iterable of google.cloud.bigquery.job.UDFResource (empty by default)
  • query_parameters (tuple) – An iterable of google.cloud.bigquery._helpers.AbstractQueryParameter (empty by default)
cache_hit#

Query results served from cache.

See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#cacheHit

Return type:bool or NoneType
Returns:True if the query results were served from cache (None until set by the server).
complete#

Server completed query.

See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#jobComplete

Return type:bool or NoneType
Returns:True if the query completed on the server (None until set by the server).
default_dataset#

See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#defaultDataset

dry_run#

See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#dryRun

errors#

Errors generated by the query.

See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#errors

Return type:list of mapping, or NoneType
Returns:Mappings describing errors generated on the server (None until set by the server).
fetch_data(max_results=None, page_token=None, start_index=None, timeout_ms=None, client=None)[source]#

API call: fetch a page of query result data via a GET request

See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/getQueryResults

Parameters:
  • max_results (int) – (Optional) maximum number of rows to return.
  • page_token (str) – (Optional) token representing a cursor into the table’s rows.
  • start_index (int) – (Optional) zero-based index of starting row
  • timeout_ms (int) – (Optional) timeout, in milliseconds, to wait for query to complete
  • client (Client or NoneType) – the client to use. If not passed, falls back to the client stored on the current dataset.
Return type:

tuple

Returns:

(row_data, total_rows, page_token), where row_data is a list of tuples, one per result row, containing only the values; total_rows is a count of the total number of rows in the table; and page_token is an opaque string which can be used to fetch the next batch of rows (None if no further batches can be fetched).

Raises:

ValueError if the query has not yet been executed.

classmethod from_query_job(job)[source]#

Factory: construct from an existing job.

Parameters:job (QueryJob) – existing job
Return type:QueryResults
Returns:the instance, bound to the job
job#

Job instance used to run the query.

Return type:google.cloud.bigquery.job.QueryJob, or NoneType
Returns:Job instance used to run the query (None until jobReference property is set by the server).
max_results#

See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#maxResults

name#

Job name, generated by the back-end.

See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#jobReference

Return type:list of mapping, or NoneType
Returns:Mappings describing errors generated on the server (None until set by the server).
page_token#

Token for fetching next bach of results.

See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#pageToken

Return type:str, or NoneType
Returns:Token generated on the server (None until set by the server).
preserve_nulls#

See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#preserveNulls

project#

Project bound to the job.

Return type:str
Returns:the project (derived from the client).
rows#

Query results.

See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#rows

Return type:list of tuples of row values, or NoneType
Returns:fields describing the schema (None until set by the server).
run(client=None)[source]#

API call: run the query via a POST request

See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query

Parameters:client (Client or NoneType) – the client to use. If not passed, falls back to the client stored on the current dataset.
schema#

Schema for query results.

See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#schema

Return type:list of SchemaField, or NoneType
Returns:fields describing the schema (None until set by the server).
timeout_ms#

See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#timeoutMs

total_bytes_processed#

Total number of bytes processed by the query.

See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#totalBytesProcessed

Return type:int, or NoneType
Returns:Count generated on the server (None until set by the server).
total_rows#

Total number of rows returned by the query.

See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#totalRows

Return type:int, or NoneType
Returns:Count generated on the server (None until set by the server).
use_legacy_sql#

See: https://cloud.google.com/bigquery/docs/reference/v2/jobs/query#useLegacySql

use_query_cache#

See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query#useQueryCache