: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-imports): bucket non-integer numerical partition keys instead of crashing (#73125)
Tom Owers63adad630afb
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 b7688ccb0e45..67c7c5aa9dcf 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@@ -1006,6 +1006,29 @@ def test_append_partition_key_numerical_handles_null_key():assert partitioned_table.column(PARTITION_KEY).to_pylist() == ["1", "2", "null", "4"]+def test_append_partition_key_numerical_handles_non_int_key():+ # Numerical mode is persisted per source and passed back in on later batches, skipping the+ # detection guard that requires an integer key column. If the source's key column has since+ # changed type, the values arrive as strings and used to crash the batch on `str // int`.+ # Numeric strings must keep their original bucket; non-numeric ones fall into the null bucket.+ table = pa.table({"id": pa.array(["250", "10", "not-a-number"], type=pa.string())})++ result = append_partition_key_to_table(+ table=table,+ partition_count=None,+ partition_size=100,+ partition_keys=["id"],+ partition_mode="numerical",+ partition_format=None,+ logger=cast(FilteringBoundLogger, structlog.get_logger()),+ )++ assert result is not None+ partitioned_table, mode, _, _ = result+ assert mode == "numerical"+ assert partitioned_table.column(PARTITION_KEY).to_pylist() == ["2", "0", "null"]++def _mock_schema(**overrides: Any) -> MagicMock:schema = MagicMock()schema.partition_count = overrides.get("partition_count")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 8cba0513ac66..5eaff7e8603c 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@@ -670,8 +670,19 @@ def append_partition_key_to_table(if key_value is None:partition_array.append(NULL_NUMERICAL_PARTITION)- else:+ elif isinstance(key_value, int):partition_array.append(str(key_value // partition_size))+ else:+ # A persisted "numerical" mode can outlive the integer key column that+ # justified it (e.g. the source's key column changed type mid-sync). Coerce+ # numeric values back to int so rows keep their original bucket; anything that+ # isn't integer-like lands in the null bucket instead of crashing the sync.+ try:+ coerced_key_value = int(key_value)+ except (TypeError, ValueError):+ partition_array.append(NULL_NUMERICAL_PARTITION)+ else:+ partition_array.append(str(coerced_key_value // partition_size))elif mode == "datetime":key = normalized_partition_keys[0]date = row[key]