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.

fix(bigquery): qualify INFORMATION_SCHEMA with the dataset project (#69141)

Tom Owersa967031d0583
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 56dda3078446..d50802dcf42d 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
@@ -996,9 +996,14 @@ class BigQueryImplementation(SQLSourceImplementation[BigQuerySourceConfig, bigqu
# `bigquery.jobs.create`) or auth failure surfaces here rather than at `result()`.
# Both calls must sit inside the try so a permission-denied account degrades to
# "no new schemas" instead of crashing schema discovery.
+ # Qualify INFORMATION_SCHEMA with the project: when the dataset lives in a different
+ # project than the service account (the `dataset_project` option), the client's default
+ # project and the job's billing project diverge, and BigQuery can't resolve an unqualified
+ # `dataset.INFORMATION_SCHEMA.*` — it rejects the job with "ProjectId must be non-empty".
+ project = _resolve_query_project(config)
query = conn.query(
- f"SELECT table_name, column_name, data_type, is_nullable FROM `{_resolve_dataset_id(config)}.INFORMATION_SCHEMA.COLUMNS` ORDER BY table_name ASC",
- project=_resolve_query_project(config),
+ f"SELECT table_name, column_name, data_type, is_nullable FROM `{project}.{_resolve_dataset_id(config)}.INFORMATION_SCHEMA.COLUMNS` ORDER BY table_name ASC",
+ project=project,
)
rows = query.result()
except Forbidden:
@@ -1082,12 +1087,15 @@ class BigQueryImplementation(SQLSourceImplementation[BigQuerySourceConfig, bigqu
# the BigQuery region / metadata version, hence the
# `removeprefix` below — the JOIN has to tolerate both shapes or
# half the rows get dropped.
+ # Project-qualify the dataset (see `get_columns`): an unqualified `dataset.INFORMATION_SCHEMA.*`
+ # fails with "ProjectId must be non-empty" when the dataset lives in a different project.
+ qualified_dataset = f"{project}.{dataset_id}"
query = f"""
SELECT tc.table_name, kcu.column_name
- FROM `{dataset_id}`.INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
- JOIN `{dataset_id}`.INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
+ FROM `{qualified_dataset}`.INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
+ JOIN `{qualified_dataset}`.INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
ON tc.constraint_name = kcu.constraint_name
- JOIN `{dataset_id}`.INFORMATION_SCHEMA.COLUMNS c
+ JOIN `{qualified_dataset}`.INFORMATION_SCHEMA.COLUMNS c
ON kcu.table_name = c.table_name
AND (
kcu.column_name = c.column_name
@@ -1136,9 +1144,11 @@ class BigQueryImplementation(SQLSourceImplementation[BigQuerySourceConfig, bigqu
project = _resolve_query_project(config)
+ # Project-qualify the dataset (see `get_columns`): an unqualified `dataset.INFORMATION_SCHEMA.*`
+ # fails with "ProjectId must be non-empty" when the dataset lives in a different project.
query = f"""
SELECT table_name, column_name
- FROM `{_resolve_dataset_id(config)}`.INFORMATION_SCHEMA.COLUMNS
+ FROM `{project}.{_resolve_dataset_id(config)}`.INFORMATION_SCHEMA.COLUMNS
WHERE is_partitioning_column = 'YES'
OR clustering_ordinal_position = 1
"""
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 87ea1abd283d..50f596c467c4 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
@@ -808,11 +808,52 @@ def test_bigquery_get_columns_trims_whitespace_in_identifiers():
BigQueryImplementation().get_columns(fake_client, config, names=None)
sql = fake_client.query.call_args.args[0]
- assert "`bigquery_aloalo.INFORMATION_SCHEMA.COLUMNS`" in sql
+ assert "`524098457564.bigquery_aloalo.INFORMATION_SCHEMA.COLUMNS`" in sql
assert " bigquery_aloalo" not in sql
+ assert " 524098457564" not in sql
assert fake_client.query.call_args.kwargs["project"] == "524098457564"
+def test_bigquery_get_columns_qualifies_information_schema_with_dataset_project():
+ """When the dataset lives in a different project (`dataset_project`), the INFORMATION_SCHEMA
+ reference must carry that project — an unqualified `dataset.INFORMATION_SCHEMA.*` makes BigQuery
+ reject the job with "ProjectId must be non-empty"."""
+ fake_client = mock.MagicMock()
+ fake_client.query.return_value.result.return_value = []
+
+ config = _make_config(
+ project_id="service-account-project",
+ dataset_id="posthog_export",
+ dataset_project=BigQueryDatasetProjectConfig(dataset_project_id="dataset-project", enabled=True),
+ )
+ BigQueryImplementation().get_columns(fake_client, config, names=None)
+
+ sql = fake_client.query.call_args.args[0]
+ assert "`dataset-project.posthog_export.INFORMATION_SCHEMA.COLUMNS`" in sql
+ assert fake_client.query.call_args.kwargs["project"] == "dataset-project"
+
+
[email protected]("method_name", ["get_primary_keys", "get_leading_index_columns"])
+def test_bigquery_discovery_qualifies_information_schema_with_dataset_project(method_name):
+ """`get_primary_keys` and `get_leading_index_columns` share the same unqualified-reference
+ defect as `get_columns`, but swallow the error and silently lose PK/index detection. Their
+ INFORMATION_SCHEMA references must carry the dataset project too."""
+ config = _make_config(
+ project_id="service-account-project",
+ dataset_id="posthog_export",
+ dataset_project=BigQueryDatasetProjectConfig(dataset_project_id="dataset-project", enabled=True),
+ )
+
+ fake_client = mock.MagicMock()
+ fake_client.query.return_value.result.return_value = []
+
+ getattr(BigQueryImplementation(), method_name)(fake_client, config, tables=["t"])
+
+ sql = fake_client.query.call_args.args[0]
+ assert "`dataset-project.posthog_export`.INFORMATION_SCHEMA" in sql
+ assert fake_client.query.call_args.kwargs["project"] == "dataset-project"
+
+
def test_bigquery_get_primary_keys_trims_whitespace_in_identifiers():
fake_client = mock.MagicMock()
fake_client.query.return_value.result.return_value = []
@@ -821,8 +862,9 @@ def test_bigquery_get_primary_keys_trims_whitespace_in_identifiers():
BigQueryImplementation().get_primary_keys(fake_client, config, tables=["t"])
sql = fake_client.query.call_args.args[0]
- assert "`my_dataset`.INFORMATION_SCHEMA.TABLE_CONSTRAINTS" in sql
- assert " my_dataset`" not in sql
+ assert "`my-project.my_dataset`.INFORMATION_SCHEMA.TABLE_CONSTRAINTS" in sql
+ assert " my_dataset" not in sql
+ assert " my-project" not in sql
assert fake_client.query.call_args.kwargs["project"] == "my-project"