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): drop tz offset from DATETIME incremental cursor literal (#63708)

Tom Owersb0dc23f9a7b0
diff --git a/posthog/temporal/data_imports/sources/bigquery/bigquery.py b/posthog/temporal/data_imports/sources/bigquery/bigquery.py
index 6f47bcb30f90..f5206a15974b 100644
--- a/posthog/temporal/data_imports/sources/bigquery/bigquery.py
+++ b/posthog/temporal/data_imports/sources/bigquery/bigquery.py
@@ -470,7 +470,16 @@ def _get_query(
else:
last_value = db_incremental_field_last_value
- if isinstance(last_value, datetime) or isinstance(last_value, date):
+ if isinstance(last_value, datetime):
+ # BigQuery DATETIME columns are timezone-naive and reject a literal that carries
+ # a UTC offset (e.g. `1970-01-01T00:00:00+00:00`), failing with "Could not cast
+ # literal ... to type DATETIME". The shared initial cursor value is tz-aware UTC,
+ # so drop the offset for DATETIME fields. TIMESTAMP columns are timezone-aware and
+ # keep it.
+ if incremental_field_type == IncrementalFieldType.DateTime and last_value.tzinfo is not None:
+ last_value = last_value.replace(tzinfo=None)
+ last_value = f"'{last_value.isoformat()}'"
+ elif isinstance(last_value, date):
last_value = f"'{last_value.isoformat()}'"
operator = incremental_type_to_operator(incremental_field_type)
diff --git a/posthog/temporal/data_imports/sources/bigquery/tests/test_source.py b/posthog/temporal/data_imports/sources/bigquery/tests/test_source.py
index 91c044acb25e..050dba0c17e5 100644
--- a/posthog/temporal/data_imports/sources/bigquery/tests/test_source.py
+++ b/posthog/temporal/data_imports/sources/bigquery/tests/test_source.py
@@ -186,6 +186,49 @@ def test_bigquery_get_query_keeps_incremental_field_in_projection():
assert "WHERE `updated_at` > 42" in query
+def test_bigquery_get_query_datetime_initial_value_has_no_timezone_offset():
+ """BigQuery DATETIME columns are timezone-naive; a literal with a UTC offset (the shared
+ tz-aware initial cursor value) fails with "Could not cast literal ... to type DATETIME"."""
+ bq_table = mock.MagicMock(dataset_id="ds", table_id="t")
+ query = _get_query(
+ should_use_incremental_field=True,
+ db_incremental_field_last_value=None,
+ bq_table=bq_table,
+ incremental_field="updated_at",
+ incremental_field_type=IncrementalFieldType.DateTime,
+ )
+ assert "WHERE `updated_at` > '1970-01-01T00:00:00'" in query
+ assert "+00:00" not in query
+
+
+def test_bigquery_get_query_datetime_strips_offset_from_stored_value():
+ """A stored tz-aware cursor value must also lose its offset for DATETIME columns."""
+ bq_table = mock.MagicMock(dataset_id="ds", table_id="t")
+ last_value = parser.parse("2024-03-11T09:26:04+00:00")
+ query = _get_query(
+ should_use_incremental_field=True,
+ db_incremental_field_last_value=last_value,
+ bq_table=bq_table,
+ incremental_field="updated_at",
+ incremental_field_type=IncrementalFieldType.DateTime,
+ )
+ assert "WHERE `updated_at` > '2024-03-11T09:26:04'" in query
+ assert "+00:00" not in query
+
+
+def test_bigquery_get_query_timestamp_keeps_timezone_offset():
+ """BigQuery TIMESTAMP columns are timezone-aware, so the UTC offset must be preserved."""
+ bq_table = mock.MagicMock(dataset_id="ds", table_id="t")
+ query = _get_query(
+ should_use_incremental_field=True,
+ db_incremental_field_last_value=None,
+ bq_table=bq_table,
+ incremental_field="created_at",
+ incremental_field_type=IncrementalFieldType.Timestamp,
+ )
+ assert "WHERE `created_at` > '1970-01-01T00:00:00+00:00'" in query
+
+
@pytest.mark.parametrize(
"malicious_column",
[