Skip to content

bq>: Running Google BigQuery queries

bq> operator runs a query on Google BigQuery.

_export:
  bq:
    dataset: my_dataset

+step1:
  bq>: queries/step1.sql
+step2:
  bq>: queries/step2.sql
  destination_table: result_table
+step3:
  bq>: queries/step3.sql
  destination_table: other_project:other_dataset.other_table

Note: The bq> operator uses standard SQL by default, whereas the default in the BigQuery console is legacy SQL. To run legacy SQL queries, please set use_legacy_sql: true. For more information about standard SQL on BigQuery, see Migrating from legacy SQL.

Secrets

When you don't know how to set secrets, please refer to Managing Workflow Secret

  digdag secrets --project my_project --set gcp.credential=@my-svc-account-b4df00d.json

If gcp.credential is not set, digdag tries to get Application Default Credential.

Options

  • bq>: query.sql

    Path to a query template file. This file can contain ${...} syntax to embed variables.

    Examples:

  bq>: queries/step1.sql
  • query: STRING

    The BigQuery SQL query to execute. Use this parameter to specify the SQL query inline instead of using a file. This parameter can contain ${...} syntax to embed variables. To use this parameter, leave bq> empty. If bq> has a file path, the file is used and query is ignored.

    Examples:

  bq>:
  query: select * from my_table where id = ${my_value}
  • location: LOCATION

    The location where the query job should run. See locations for a list of available locations.

    Examples:

  location: asia-northeast1
  • dataset: NAME

    Specifies the default dataset to use in the query and in the destination_table parameter.

    Examples:

  dataset: my_dataset
  dataset: other_project:other_dataset
  • destination_table: NAME

    Specifies a table to store the query results in.

    Examples:

  destination_table: my_result_table
  destination_table: some_dataset.some_table
  destination_table: some_project:some_dataset.some_table

You can append a date as $YYYYMMDD form at the end of table name to store data in a specific partition. See Creating and Updating Date-Partitioned Tables document for details.

  destination_table: some_dataset.some_partitioned_table$20160101
  • create_disposition: CREATE_IF_NEEDED | CREATE_NEVER

    Specifies whether the destination table should be automatically created when executing the query.

    • CREATE_IF_NEEDED: (default) The destination table is created if it does not already exist.
    • CREATE_NEVER: The destination table must already exist, otherwise the query will fail.

    Examples:

  create_disposition: CREATE_IF_NEEDED
  create_disposition: CREATE_NEVER
  • write_disposition: WRITE_TRUNCATE | WRITE_APPEND | WRITE_EMPTY

    Specifies whether to permit writing of data to an already existing destination table.

    • WRITE_TRUNCATE: If the destination table already exists, any data in it will be overwritten.
    • WRITE_APPEND: If the destination table already exists, any data in it will be appended to.
    • WRITE_EMPTY: (default) The query fails if the destination table already exists and is not empty.

    Examples:

  write_disposition: WRITE_TRUNCATE
  write_disposition: WRITE_APPEND
  write_disposition: WRITE_EMPTY
  • priority: INTERACTIVE | BATCH

    Specifies the priority to use for this query. Default: INTERACTIVE.

  • use_query_cache: BOOLEAN

    Whether to use BigQuery query result caching. Default: true.

  • allow_large_results: BOOLEAN

    Whether to allow arbitrarily large result tables. Requires destination_table to be set and use_legacy_sql to be true.

  • flatten_results: BOOLEAN

    Whether to flatten nested and repeated fields in the query results. Default: true. Requires use_legacy_sql to be true.

  • use_legacy_sql: BOOLEAN

    Whether to use legacy BigQuery SQL. Default: false.

  • maximum_billing_tier: INTEGER

    Limit the billing tier for this query. Default: The project default.

  • table_definitions: OBJECT

    Describes external data sources that are accessed in the query. For more information see BigQuery documentation.

  • user_defined_function_resources: LIST

    Describes user-defined function resources used in the query. For more information see BigQuery documentation.

  • store_last_results: false | first | all

    Specifies whether to store the query results in the bq.last_results output parameter.

    • false: (default) The query results are not stored.
    • first: The first row of the query results is stored.
    • all: All rows of the query results are stored.

    Examples:

  store_last_results: first
  store_last_results: all

Output parameters

  • bq.last_results

    The query results. This is set only when store_last_results is first or all.

    With first, this is the first row as an object, with column names as keys. For example, ${bq.last_results.count} is the value of the count column.

    With all, this is an array of objects, one object for each row. For example, ${bq.last_results[0].name} is the value of the name column in the first row.

  • bq.last_job_id

    The id of the BigQuery job that executed this query.

    Note: bq.last_jobid parameter is kept only for backward compatibility but you must not use it because it will be removed in a near future release.

Limitations

If store_last_results is first or all, the query results must be within the following limits.

  • The query results have at most 64 columns.
  • With all, the query results have at most 8192 rows.
  • Each string value is at most 1 MiB.
  • The stored results are at most 4 MiB in total.