Skip to content

Targets

BigQuery

Load into BigQuery (beta): rows streamed as Arrow through the Storage Write API into a staging table, then one query job per write.

This target is in beta: it is tested, but gets less everyday use than the other connectors, so expect rough edges, and please report them in GitHub issues.

A BigQuery target loads into BigQuery datasets. Each batch streams as Arrow through the Storage Write API into a staging table, then one query job moves it into the table.

With a service account’s key file:

[connections.bq]
kind = "bigquery"
location = "EU"
service_account_key = "${ENV:BIGQUERY_KEY_JSON}"
# project = "my-project"
[targets.bq]
connection = "bq"
loader_schema = "raw"
Key Meaning
location Where query jobs run and datasets are created, such as EU or europe-west2. Required.
service_account_key A service-account key file’s JSON text, not its path. Without it, SQLLoad signs in as Authentication describes.
impersonate_service_account A service account’s email to act as, such as loader@my-project.iam.gserviceaccount.com, whichever identity signs in.
project The project tables load into and jobs run in. Default: the project of service_account_key; without one, GOOGLE_CLOUD_PROJECT, else the credentials’ own project (a key’s project_id, the impersonated account’s project, or the file’s quota_project_id). On a VM or Cloud Run, set project or GOOGLE_CLOUD_PROJECT.

loader_schema is the dataset tables load into; a source’s destination_schema overrides it. sqlload apply creates the dataset when it doesn’t exist. Pass the key through the environment, such as export BIGQUERY_KEY_JSON="$(cat loader-key.json)", to keep it out of sqlload.toml.

SQLLoad signs in with the first of these it finds, as Google’s own libraries do:

  1. The connection’s service_account_key.
  2. The file GOOGLE_APPLICATION_CREDENTIALS names. A missing file fails rather than signing in as someone else.
  3. gcloud’s login, from gcloud auth application-default login.
  4. The service account of the machine, from its metadata server: a Compute Engine VM, Cloud Run, or GKE with Workload Identity.

A credentials file can be:

Type What signs in Roles it needs besides BigQuery’s
service_account A service-account key.
authorized_user A gcloud user login, renewed with its refresh token.
impersonated_service_account Its source credentials, acting as the service account it names (gcloud auth application-default login --impersonate-service-account=…). Service Account Token Creator (roles/iam.serviceAccountTokenCreator) on that service account.
external_account Workload identity federation: another cloud’s token, exchanged at Google STS. A workload identity pool and provider that trust it, and Workload Identity User (roles/iam.workloadIdentityUser) for the pool’s principals on the service account the file impersonates.

An external_account file (from gcloud iam workload-identity-pools create-cred-config) reads the other cloud’s token from a file, a URL, a command (only with GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES=1), or AWS: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN and AWS_REGION (or AWS_DEFAULT_REGION), else the EC2 instance role. Workforce identity pools and other file types are refused. service_account_key takes only a service-account key; put other credentials in the GOOGLE_APPLICATION_CREDENTIALS file.

The loading identity needs BigQuery Data Editor and BigQuery Job User in the project, and, with impersonate_service_account, Token Creator on that account (not with a credentials file that already impersonates one). Tokens are renewed before they expire, so a load can outlast any one. A failed sign-in names the step and Google’s reason. sqlload debug shows how each connection signs in, never a key or token, then runs a query job and prints the project and location.

With the quickstart’s Postgres database, the connection above and default_target = "bq":

$ sqlload plan -v
Plan ready 2 selected, 2 objects to create, 2 to load
to BigQuery bq
Objects (2) sqlload apply
…
└── raw.app__orders create table
├── order_id int -> INT64
├── customer_id int -> INT64
├── status text -> STRING
├── total decimal(12,2) -> NUMERIC(12,2)
├── updated_at timestamp_tz -> TIMESTAMP (stored as a UTC instant; …
└── + _sqlload_load_id, _loaded_at
Loads (2) sqlload load
…
└── raw.app__orders merge (unique_key: order…
├── source postgres app public.orders
├── route read as Arrow -> BigQuery takes Arrow
└── reads updated_at: first run, everything
✓ Plan complete 2 selected, 2 objects to create, 2 to load
$ sqlload apply
…
$ sqlload load
…
loaded raw.app__customers 50 rows 0.1s
loaded raw.app__orders 1,000 rows 0.1s

Rows stream into a staging table, <table>__sqlload_staging_<load id> (a build table for replace), never into the table itself. A batch commits whole or not at all, so no reader sees part of one. A value that doesn’t fit its column fails the batch; it never loads as NULL. One query job then moves the rows into the table, and a retried request never runs it twice. A load drops its staging tables when it ends; the next load into the dataset drops any that a failed run left once they’re over a day old.

Parquet files in a gcs bucket can load without passing through SQLLoad: BigQuery queries them where they lie, and a batch whose row count differs from the files’ footers fails. BigQuery can’t fill _sqlload_source_row, which every files table keeps by default, so exclude it:

[sources.orders]
kind = "files"
connection = "lake"
path = "orders/"
format = "parquet"
exclude_columns = ["_sqlload_source_row"]
└── route BigQuery reads directly from gs://example-bucket/orders/ (nothing through the loader)

Otherwise SQLLoad reads the files, and plan -v says why:

├── route read parquet into Arrow -> BigQuery takes Arrow
├── why SQLLoad reads the files because BigQuery doesn't report a row's position in its file, so it can't fill _sqlload_source_row; to have BigQuery load the files itself, add _sqlload_source_row to exclude_columns; …

BigQuery reads the bucket as the BigQuery connection’s service account, not with the gcs connection’s key: give that account roles/storage.objectViewer on the bucket. plan checks with a dry run, which bills nothing, and fails naming the fix.

SQLLoad also reads the files itself for masked columns, a custom endpoint on the gcs connection, a UUID, list, JSON, native type or decimal(20,0) (unsigned 64-bit) column, or a column name BigQuery would change: one not of letters, digits and underscores starting with a letter or underscore, one with a reserved prefix such as _FILE_, or two differing only in case.

Each statement is its own query job and commits on its own; the run record commits after it.

Strategy Statement
append One INSERT ... SELECT from the staging table.
merge One MERGE on the key.
delete_insert One MERGE that inserts the batch and deletes the rows it replaces.
replace CREATE OR REPLACE TABLE ... LIKE the build table AS SELECT from it, so columns keep their precision, scale and length, such as NUMERIC(20,2).

replace loads the whole source into a build table and swaps it in at the end, keeping the table’s layout. The description comes from the config; labels, column descriptions and policy tags set outside SQLLoad are lost. Columns a contract or on_schema_change = "ignore" leaves out aren’t loaded.

A type change that rewrites the table, such as int to float, keeps the layout and column types, except that a BIGNUMERIC that fits a NUMERIC becomes one. A table with a STRUCT, RANGE, GEOGRAPHY, INTERVAL or BIGNUMERIC over 38 digits is rewritten with types from the query, losing precision, scale and length.

SQLLoad creates a table partitioned and clustered as its config says:

[sources.app.tables."public.events"]
write_strategy = "merge"
unique_key = ["id", "day"]
cursor_column = "updated_at"
partition_by = "day"
cluster_by = ["id"]
Key Meaning
partition_by A DATE column; DATE(col) of a TIMESTAMP or DATETIME; DATE_TRUNC(col, MONTH|YEAR) of a DATE; TIMESTAMP_TRUNC(col, HOUR|DAY|MONTH|YEAR) of a TIMESTAMP; DATETIME_TRUNC(col, ...) of a DATETIME; or RANGE_BUCKET(col, GENERATE_ARRAY(start, end, interval)) of an integer, of at most 10,000 ranges.
cluster_by Up to 4 columns, not FLOAT64, TIME, JSON, BYTES or an array.
table_settings Only { require_partition_filter = true }, which needs partition_by.

plan refuses what BigQuery would, naming the fix, such as a partition column the table doesn’t load or of the wrong type, or ClickHouse’s engine, order_by and primary_key.

A table whose config sets no layout keeps the one it has: removing partition_by and cluster_by stops SQLLoad managing it. A replace of a table partitioned or clustered by hand fails, as BigQuery won’t replace it with one laid out otherwise; set its layout in the config.

A merge scans only its batch’s partitions when unique_key holds the partition column, as does a delete_insert whose replace_key holds it, or by a cursor that is the partition column. Otherwise the write scans the whole table. Rows with a NULL partition column are always scanned, and a batch that touches every partition, such as a first load, scans the whole table.

A table with require_partition_filter loads by append, replace, delete_insert by cursor, and merge or delete_insert by key when the key holds the partition column. plan refuses a key without it, and deletes, checks.max_row_drop, newest_by and unnest, whose queries can’t filter:

└── reason require_partition_filter: a merge filters on the partition column only when unique_key holds it, and unique_key lacks day; set table_settings.require_partition_filter = false, or add day to unique_key

BigQuery can’t change a table’s partitioning or clustering in place, so plan fails a table laid out otherwise than its config says:

Errors (1)
└── raw.app__events
└── reason the table is unpartitioned, unclustered, but its config lays it out partitioned by DAY of day, unclustered; load with --reload to rebuild it as configured, or change the config to match

sqlload load --reload rebuilds it as configured, and plan --reload warns that it will. The table is missing for a moment during the swap. If the load fails then, the old rows stay in <table>__sqlload_relayout.

Type BigQuery
bool BOOL
smallint INT64 ¹
int INT64
decimal(p,s) NUMERIC(p,s), or BIGNUMERIC(p,s) for more than 29 digits before the point or a scale over 9
float FLOAT64
text STRING
varchar(n) STRING(n)
date DATE
time TIME
timestamp_tz TIMESTAMP ¹
timestamp_ntz DATETIME
json JSON
uuid STRING ¹
binary BYTES

¹ Held with a loss: smallint is stored as a 64-bit integer, uuid as text, and timestamp_tz as a UTC instant without its offset.

  • Records load as JSON, not as typed STRUCT columns. Columns pinned as array(T) load as ARRAY<T>, except array(timestamp_tz).
  • copy_to_internal_stage is for Snowflake and is refused here.
  • A Postgres CDC source can’t load into BigQuery; plan refuses it.

A local fake BigQuery covers the protocol and failure cases: wrong offsets, refused appends and commits, and expired tokens. A live round against a real BigQuery project covered:

  • Sign-in: a service-account key, a VM’s and a Cloud Run job’s service account, impersonation, and workload identity federation from AWS and from GitHub Actions OIDC, including a load that outlasted its token. A gcloud user login and federation from Azure are tested only against a simulated Google.
  • Loads: Postgres, Parquet files, Python and sl.http sources under every strategy plan accepts, 19 tables checked against their sources over rounds of new and updated rows, added columns and a widened decimal; incremental reads, contracts and deletes; a failed load leaving no rows or staging table.
  • Parquet in GCS: the same files loaded by BigQuery and by SQLLoad’s reader matched value for value.
  • Rebuilds: a type change kept NUMERIC(20,2), turned BIGNUMERIC(20,2) into NUMERIC(20,2), and rewrote a table with a STRUCT column.