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): close backtick before INFORMATION_SCHEMA in get_columns (#88962)

Tom Owersfa859587a388
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 fb55b4d8b7e9..3c34b0cf85f0 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
@@ -1197,9 +1197,14 @@ class BigQueryImplementation(SQLSourceImplementation[BigQuerySourceConfig, bigqu
# 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".
+ # The backtick-quoted identifier must close after the dataset, not after `INFORMATION_SCHEMA.COLUMNS`
+ # — quoting the whole path as one identifier (like a regular `project.dataset.table` reference)
+ # stops BigQuery from resolving the trailing segments as the INFORMATION_SCHEMA view, which
+ # raises the same "ProjectId must be non-empty" error this qualification was meant to fix.
project = _resolve_query_project(config)
+ qualified_dataset = f"{project}.{_resolve_dataset_id(config)}"
query = conn.query(
- f"SELECT table_name, column_name, data_type, is_nullable FROM `{project}.{_resolve_dataset_id(config)}.INFORMATION_SCHEMA.COLUMNS` ORDER BY table_name ASC",
+ f"SELECT table_name, column_name, data_type, is_nullable FROM `{qualified_dataset}`.INFORMATION_SCHEMA.COLUMNS ORDER BY table_name ASC",
project=project,
)
rows = query.result()
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 7d922cd6d596..a8a8194bca1e 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
@@ -1019,7 +1019,7 @@ 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 "`524098457564.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"
@@ -1028,7 +1028,10 @@ def test_bigquery_get_columns_trims_whitespace_in_identifiers():
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"."""
+ reject the job with "ProjectId must be non-empty". The backtick-quoted identifier must close
+ after the dataset (matching `get_primary_keys`/`get_leading_index_columns`) rather than wrapping
+ `INFORMATION_SCHEMA.COLUMNS` inside it too — quoting the whole path as one identifier stops
+ BigQuery from resolving it as the INFORMATION_SCHEMA view and raises the same error again."""
fake_client = mock.MagicMock()
fake_client.query.return_value.result.return_value = []
@@ -1040,7 +1043,8 @@ def test_bigquery_get_columns_qualifies_information_schema_with_dataset_project(
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 "`dataset-project.posthog_export`.INFORMATION_SCHEMA.COLUMNS" in sql
+ assert "INFORMATION_SCHEMA.COLUMNS`" not in sql
assert fake_client.query.call_args.kwargs["project"] == "dataset-project"