Skip to main content
Bird & Bear
Menu
← All articles

BigQuery continuous queries can now write into Apache Iceberg tables, with SQL UDFs and UNION ALL support

bigquery ai analytics sql
BigQuery continuous queries can now write into Apache Iceberg tables, with SQL UDFs and UNION ALL support

BigQuery’s continuous queries, SQL statements that run indefinitely against incoming data instead of once against a fixed snapshot, have recently added three capabilities: writing output directly into Apache Iceberg tables, calling SQL user-defined functions from inside a continuous query, and merging multiple live streams with UNION ALL.

What is a continuous query?

A normal BigQuery query runs once and returns a result. A continuous query is a job that keeps running, reading new rows as they arrive (via the APPENDS or CHANGES table-valued functions) and processing each one as it comes in. Run by a user account, it stops automatically after two days. Run by a service account, it can run for up to 150 days before it needs restarting. Either way, it keeps going after you close the console tab or terminal window that started it.

Writing into Iceberg tables

Continuous queries could already read from Apache Iceberg managed tables. What’s new is writing to one: an INSERT INTO statement can now target an Iceberg managed table using exactly the same syntax as writing to a standard BigQuery table. The destination table has to already exist, created separately through BigQuery’s own Iceberg managed-table setup, and you don’t need to specify a connection or a storage bucket in the query itself, because both are already defined on the destination table.

Google’s own documentation gives this example, filtering and transforming a stream of taxi-ride data and writing the result into another table:

INSERT INTO `myproject.real_time_taxi_streaming.transformed_taxirides`
SELECT
  timestamp,
  meter_reading,
  ride_status,
  passenger_count,
  ST_Distance(
    ST_GeogPoint(pickup_longitude, pickup_latitude),
    ST_GeogPoint(dropoff_longitude, dropoff_latitude)) AS euclidean_trip_distance,
    SAFE_DIVIDE(meter_reading, passenger_count) AS cost_per_passenger
FROM
  APPENDS(TABLE `myproject.real_time_taxi_streaming.taxirides`,
    CURRENT_TIMESTAMP() - INTERVAL 10 MINUTE)
WHERE
  ride_status = 'dropoff';

To write to an Iceberg managed table instead of a standard one, the only change is the table named after INSERT INTO. Nothing else in the query changes, because the connection and storage details live on the table, not the query.

Iceberg is an open table format, readable by tools outside BigQuery entirely (Spark, Trino, Snowflake, and a growing list of AI and ML platforms). A real-time pipeline that used to have one exit, BigQuery’s own storage, now has a second one that isn’t locked to a single vendor. Data that lands in an open format the moment it’s created doesn’t need a migration project later if it has to be read somewhere else.

Reusable transformation logic with SQL UDFs

A continuous query can now call a persistent or temporary SQL user-defined function. Only SQL UDFs work here: JavaScript UDFs, Python UDFs, remote functions, and user-defined aggregate functions aren’t supported.

Creating a persistent one is a normal CREATE FUNCTION statement, run once outside the continuous query:

CREATE FUNCTION `myproject.real_time_taxi_streaming.calculate_fare_per_passenger`(
  meter_reading FLOAT64,
  passenger_count INT64)
RETURNS FLOAT64
AS (
  ROUND(SAFE_DIVIDE(meter_reading, passenger_count), 2)
);

The continuous query then calls it like any other function:

INSERT INTO `myproject.real_time_taxi_streaming.transformed_taxirides`
SELECT
  timestamp,
  meter_reading,
  ride_status,
  passenger_count,
  `myproject.real_time_taxi_streaming.calculate_fare_per_passenger`(
    meter_reading, passenger_count) AS cost_per_passenger
FROM
  APPENDS(TABLE `myproject.real_time_taxi_streaming.taxirides`,
    CURRENT_TIMESTAMP() - INTERVAL 10 MINUTE)
WHERE
  ride_status = 'dropoff';

Updating a persistent UDF, even just a label or description change, while a continuous query references it, terminates that continuous query job outright. The same applies to a nested UDF called by another UDF the query uses. Google’s documented workaround is to update the function, then start a new continuous query from the exact point the old one stopped, using APPENDS or CHANGES with the previous job’s end timestamp. There’s no quiet, zero-downtime way to redeploy a UDF a live continuous query depends on.

Merging streams with UNION ALL

UNION ALL is now a supported stateful operation in continuous queries, alongside JOINs and windowed aggregations, which already let a continuous query correlate rows across streams or compute a rolling metric like a 30-minute average. UNION ALL is explicitly marked Preview in Google’s documentation, unlike the Iceberg write support and SQL UDF support above, neither of which carries a preview label on the page. In practice, this means a continuous query can combine the output of two or more streaming sources into a single output stream, the kind of thing that previously meant running separate pipelines and merging their results downstream.

Permissions

Reading from or writing to an Iceberg managed table needs more than the usual BigQuery roles. In addition to the standard job-creation and table-access permissions, the account running the continuous query needs the BigQuery Connection User role to use the Google Cloud resource connection tied to the Iceberg table, and the service account behind that connection needs appropriate Cloud Storage permissions (roles/storage.objectUser and roles/storage.legacyBucketReader) on the underlying bucket. Check this before assuming a continuous query that already writes to standard BigQuery tables will work against an Iceberg one without any changes.

Why this matters

Most of the “AI-ready data” conversation is about data that’s already sitting still: cleaning up a warehouse, documenting what a metric means, fixing a tracking gap. This is the same problem from the other direction, what happens to data the moment it’s created, before it’s ever queried by anyone. Writing a live event stream straight into an open format, with reusable, auditable transformation logic attached, is what makes “ready for whatever reads it next” true at the point of creation rather than something fixed up later in a cleanup project.

Newsletter

Occasional notes on data, tracking & AI-readiness

No spam, no growth-hacking. Just an email when there's something worth reading.