Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(bigquery): support BigQuery API version v2 (#71673)

Tom Owers01fb876d5b44
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py
index fb8ef3a94ae4..665fb380f315 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py
@@ -52,6 +52,7 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline
from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline.utils import (
DEFAULT_PARTITION_TARGET_SIZE_IN_BYTES,
)
+from products.warehouse_sources.backend.temporal.data_imports.sources.common.base import UNVERSIONED_API_VERSION
from products.warehouse_sources.backend.temporal.data_imports.sources.common.grpc import make_tracked_channel
from products.warehouse_sources.backend.temporal.data_imports.sources.common.http import (
DEFAULT_RETRY,
@@ -80,6 +81,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.generated_
from products.warehouse_sources.backend.types import IncrementalFieldType, PartitionSettings
__all__ = [
+ "BIGQUERY_API_VERSION_V2",
"BIGQUERY_DATASET_NOT_FOUND_ERROR",
"BIGQUERY_INVALID_IDENTIFIER_ERROR",
"BIGQUERY_TOKEN_RESPONSE_ERROR",
@@ -101,6 +103,23 @@ __all__ = [
# tracked gRPC transport's logs/metrics.
BIGQUERY_STORAGE_HOST = "bigquerystorage.googleapis.com"
+# The core BigQuery REST API is stable at v2 — every resource path is served under /bigquery/v2/ —
+# so both the legacy unversioned pin and the explicit v2 label resolve to the same REST endpoint.
+# `_REST_API_VERSION_BY_LABEL` is the single place to extend should BigQuery ever ship a new REST
+# path version; mapping the legacy label onto v2 keeps existing pinned syncs byte-for-byte unchanged
+# (the google-cloud-bigquery client has always talked to the v2 endpoint).
+BIGQUERY_API_VERSION_V2 = "v2"
+_REST_API_VERSION_BY_LABEL = {
+ UNVERSIONED_API_VERSION: BIGQUERY_API_VERSION_V2,
+ BIGQUERY_API_VERSION_V2: BIGQUERY_API_VERSION_V2,
+}
+
+
+def _bigquery_rest_api_version(api_version: str | None) -> str:
+ """Map a source instance's opaque version pin onto BigQuery's REST API path segment."""
+ return _REST_API_VERSION_BY_LABEL.get(api_version or "", BIGQUERY_API_VERSION_V2)
+
+
# Stable, source-specific marker for a failed service-account OAuth token refresh.
# Used both when raising below and when matching in `BigQuerySource.get_non_retryable_errors`,
# so it must stay free of volatile data (urls, ids, timestamps).
@@ -350,6 +369,7 @@ def bigquery_client(
private_key_id: str,
client_email: str,
token_uri: str,
+ api_version: str = BIGQUERY_API_VERSION_V2,
) -> typing.Iterator[bigquery.Client]:
"""Manage a BigQuery client."""
project_id = _normalize_identifier(project_id)
@@ -376,6 +396,13 @@ def bigquery_client(
credentials=credentials,
_http=authed_session,
)
+ # Pin the REST API version segment the client builds request paths from (/bigquery/<version>/).
+ # BigQuery is stable at v2, so this matches the library default — setting it makes the source's
+ # version pin authoritative rather than implicit. Written fail-soft on the private `_connection`
+ # like the API_BASE_URL read below, so a library rename degrades instead of crashing the sync.
+ connection = getattr(client, "_connection", None)
+ if connection is not None:
+ connection.API_VERSION = api_version
# `_connection.API_BASE_URL` is the endpoint the client will actually call (it honors
# api_endpoint overrides and universe-domain hosts), so the logged host can't drift.
# It's a private attribute, so read it fail-soft: a library rename must degrade the log
@@ -1320,6 +1347,7 @@ class BigQueryImplementation(SQLSourceImplementation[BigQuerySourceConfig, bigqu
region=region,
dataset_project_id=dataset_project_id,
bq_destination_table_id=destination_table,
+ rest_api_version=_bigquery_rest_api_version(inputs.api_version),
)
finally:
# Delete the destination table (if it exists) after we're done with it
@@ -1341,6 +1369,7 @@ class BigQueryImplementation(SQLSourceImplementation[BigQuerySourceConfig, bigqu
region: str | None,
dataset_project_id: str | None,
bq_destination_table_id: str,
+ rest_api_version: str = BIGQUERY_API_VERSION_V2,
partition_size_bytes: int = DEFAULT_PARTITION_TARGET_SIZE_IN_BYTES,
) -> SourceResponse:
"""Produce a pipeline source for BigQuery.
@@ -1380,6 +1409,7 @@ class BigQueryImplementation(SQLSourceImplementation[BigQuerySourceConfig, bigqu
private_key_id=private_key_id,
client_email=client_email,
token_uri=token_uri,
+ api_version=rest_api_version,
) as bq_client:
bq_table = bq_client.get_table(fully_qualified_table_name)
primary_keys = _get_primary_keys_for_table(bq_table, bq_client)
@@ -1404,6 +1434,7 @@ class BigQueryImplementation(SQLSourceImplementation[BigQuerySourceConfig, bigqu
private_key_id=private_key_id,
client_email=client_email,
token_uri=token_uri,
+ api_version=rest_api_version,
) as bq_client:
bq_table = bq_client.get_table(fully_qualified_table_name)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/source.py
index b8c835ab39d5..5efdad645f52 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/source.py
@@ -12,6 +12,7 @@ from posthog.schema import (
)
from products.warehouse_sources.backend.temporal.data_imports.sources.bigquery.bigquery import (
+ BIGQUERY_API_VERSION_V2,
BIGQUERY_CREDENTIALS_REJECTED_ERROR,
BIGQUERY_DATASET_NOT_FOUND_ERROR,
BIGQUERY_INVALID_IDENTIFIER_ERROR,
@@ -22,7 +23,10 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.bigquery.b
build_destination_table_prefix,
validate_bigquery_credentials,
)
-from products.warehouse_sources.backend.temporal.data_imports.sources.common.base import FieldType
+from products.warehouse_sources.backend.temporal.data_imports.sources.common.base import (
+ UNVERSIONED_API_VERSION,
+ FieldType,
+)
from products.warehouse_sources.backend.temporal.data_imports.sources.common.registry import SourceRegistry
from products.warehouse_sources.backend.temporal.data_imports.sources.common.sql.base import SQLSource
from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs import BigQuerySourceConfig
@@ -35,6 +39,11 @@ _BIGQUERY_IMPLEMENTATION = BigQueryImplementation()
@SourceRegistry.register
class BigQuerySource(SQLSource[BigQuerySourceConfig]):
+ # BigQuery's core REST API is stable at v2. Existing sources stay on the legacy unversioned pin;
+ # new sources start on v2. Both resolve to the same /bigquery/v2/ REST endpoint, so the default
+ # bump leaves existing syncs byte-for-byte unchanged.
+ supported_versions = (UNVERSIONED_API_VERSION, BIGQUERY_API_VERSION_V2)
+ default_version = BIGQUERY_API_VERSION_V2
api_docs_url = "https://cloud.google.com/bigquery/docs/release-notes"
@property
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/tests/test_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/tests/test_source.py
index 569ac1edb163..6642f84229fb 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/tests/test_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/tests/test_source.py
@@ -1742,3 +1742,40 @@ def test_bigquery_resources_exceeded_is_non_retryable():
assert matching, "resourcesExceeded query failure should be recognised as non-retryable"
assert all(non_retryable_errors[key] is not None for key in matching)
+
+
+def test_bigquery_source_declares_v2_as_default():
+ # The core of this PR: v2 is a supported version and the default for new sources, while the
+ # legacy label stays supported so existing pins keep resolving. Guards a revert of the default
+ # bump or an accidental drop of a supported label (base-class pin resolution itself is already
+ # covered by the registry-invariant suite).
+ source = BigQuerySource()
+ assert source.supported_versions == ("v1", "v2")
+ assert source.default_version == "v2"
+
+
[email protected]("pin,expected_segment", [("v1", "v2"), ("v2", "v2"), (None, "v2"), ("v99", "v2")])
+def test_bigquery_rest_api_version_maps_labels_to_v2(pin, expected_segment):
+ # Both supported labels (and any fallback) resolve to BigQuery's single stable REST path
+ # segment — a wrong mapping would point the client at a nonexistent /bigquery/<x>/ endpoint.
+ assert bq_module._bigquery_rest_api_version(pin) == expected_segment
+
+
[email protected]("pin", ["v1", "v2"])
+def test_bigquery_build_pipeline_threads_resolved_rest_api_version(pin):
+ # The source's version pin must reach the request layer (the REST segment the read/query
+ # clients run under); dropping it would stop a future version from dispatching.
+ with (
+ mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.bigquery.bigquery.delete_all_temp_destination_tables",
+ ),
+ mock.patch.object(
+ BigQueryImplementation, "_build_source_response", return_value=mock.MagicMock()
+ ) as mock_build,
+ mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.bigquery.bigquery.delete_table",
+ ),
+ ):
+ BigQuerySource().source_for_pipeline(_make_config(), _make_inputs(api_version=pin))
+
+ assert mock_build.call_args.kwargs["rest_api_version"] == "v2"