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(warehouse): guard null numeric partition key (#72441)

Daniel RC1b4846bd1c9f
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.py
index a31b012571a2..532397ed213b 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
@@ -945,6 +945,31 @@ def test_append_partition_key_to_table_does_not_type_error(name: str, data: list
pytest.fail(f"raised TypeError for case {name} with data: {data}")
+def test_append_partition_key_numerical_handles_null_key():
+ # Numerical partitioning is only selected when partition_size is set, so the general
+ # does-not-type-error test (partition_size=None) never reaches the `key // partition_size`
+ # path. Pipedrive *_fields endpoints partition by `id` with partition_size=1 and can emit
+ # rows with a null id, which used to crash with `NoneType // int`.
+ partition_key = "id"
+ table = pa.table({partition_key: [1, 2, None, 4]})
+ logger: FilteringBoundLogger = structlog.get_logger()
+
+ result = append_partition_key_to_table(
+ table,
+ partition_keys=[partition_key],
+ partition_mode=None,
+ partition_count=None,
+ partition_size=1,
+ partition_format=None,
+ logger=logger,
+ )
+
+ assert result is not None
+ partitioned_table, mode, _, _ = result
+ assert mode == "numerical"
+ assert partitioned_table.column(PARTITION_KEY).to_pylist() == ["1", "2", "null", "4"]
+
+
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.py
index c8f2ad62eed3..d62341be98fd 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
@@ -516,6 +516,9 @@ def merge_observed_columns_into_schema_metadata(config: dict[str, Any], observed
PARTITION_DATETIME_COLUMN_NAMES = ["created_at", "inserted_at", "createdAt"]
+# Bucket for rows whose numerical partition key is null — they can't be bucketed arithmetically.
+NULL_NUMERICAL_PARTITION = "null"
+
async def setup_partitioning(
pa_table: pa.Table,
@@ -665,9 +668,12 @@ def append_partition_key_to_table(
assert partition_size is not None, "append_partition_key_to_table: partition_size is None"
key = normalized_partition_keys[0]
- partition = row[key] // partition_size
+ key_value = row[key]
- partition_array.append(str(partition))
+ if key_value is None:
+ partition_array.append(NULL_NUMERICAL_PARTITION)
+ else:
+ partition_array.append(str(key_value // partition_size))
elif mode == "datetime":
key = normalized_partition_keys[0]
date = row[key]