: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-warehouse): guard datetime partition key against unparseable strings (#70783)
posthog[bot]f60085dbe7d9
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.pyindex 60aa82014fa2..f3dec4963a8b 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.py@@ -1233,3 +1233,37 @@ def test_merge_observed_columns_unions_and_refreshes():)def test_source_uses_delta_write_column_selection(source_type, expected):assert source_uses_delta_write_column_selection(source_type) is expected+++ "value,expected",+ [+ # Parseable date strings bucket by their actual date.+ ("2024-03-15", "2024-03"),+ ("2024-06-01T12:00:00", "2024-06"),+ # Non-date-like strings (e.g. a UUIDv7 primary key) must not crash the repartition —+ # they fall back to the unknown-date sentinel.+ ("0198d26d-134b-713a-84f7-24d78a416d9c", "1970-01"),+ ("not a date at all", "1970-01"),+ # Empty / whitespace-only strings also fall back to the sentinel.+ ("", "1970-01"),+ (" ", "1970-01"),+ ],+)+def test_append_partition_key_datetime_string_column(value, expected):+ table = pa.table({"id": pa.array([value], type=pa.string())})++ result = append_partition_key_to_table(+ table=table,+ partition_count=None,+ partition_size=None,+ partition_keys=["id"],+ partition_mode="datetime",+ partition_format="month",+ logger=cast(FilteringBoundLogger, structlog.get_logger()),+ )++ assert result is not None+ partitioned_table, mode, _, _ = result+ assert mode == "datetime"+ assert partitioned_table.column(PARTITION_KEY).to_pylist() == [expected]diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/utils.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/utils.pyindex 666086762661..19d794eeca35 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/utils.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/utils.py@@ -684,8 +684,12 @@ def append_partition_key_to_table(elif isinstance(date, datetime.date):partition_array.append(date.strftime(date_format))elif isinstance(date, str) and date.strip():- date = parser.parse(date)- partition_array.append(date.strftime(date_format))+ try:+ date = parser.parse(date)+ partition_array.append(date.strftime(date_format))+ except (ValueError, OverflowError):+ # Non-date-like string (e.g. a UUID primary key) — treat as unknown date+ partition_array.append("1970-01")elif isinstance(date, str):# Empty string — treat as unknown datepartition_array.append("1970-01")