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.

perf(data-warehouse): build partition keys from the key columns only (#113195)

Tom Owers8d09976f20b4
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/partitioning.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/partitioning.py
index 3dae113ee029..ecbd3ec61c33 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/partitioning.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/partitioning.py
@@ -188,7 +188,15 @@ def append_partition_key_to_table(
partition_array: list[str] = []
- for batch in table.to_batches():
+ # The bucket of a row depends on its key columns alone. A row dict of every column costs one
+ # Python object per cell, which is most of the time and memory of this function on a wide table.
+ key_column_indices = [
+ table.schema.get_all_field_indices(key)[-1]
+ for key in dict.fromkeys(normalized_partition_keys)
+ if key in table.column_names
+ ]
+
+ for batch in table.select(key_column_indices).to_batches():
for row in batch.to_pylist():
if mode == "md5":
assert partition_count is not None, "append_partition_key_to_table: partition_count is None"
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_utils.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_utils.py
index 8bec8de4fefb..9ce3de9afb06 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_utils.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_utils.py
@@ -1953,6 +1953,59 @@ def test_append_partition_key_missing_column_buckets_into_fallback(
assert result.table.column(PARTITION_KEY).to_pylist() == [expected, expected, expected]
+ "partition_keys,expected_hash_inputs",
+ [
+ (["id"], ["a1", "b2", "None", "d4"]),
+ (
+ ["seen_at", "id"],
+ [
+ "2024-03-01 10:00:00|a1",
+ "2024-03-01 10:00:00.250000|b2",
+ "None|None",
+ "2024-03-02 00:00:00|d4",
+ ],
+ ),
+ (["id", "dropped_field", "count"], ["a1|None|1", "b2|None|2", "None|None|3", "d4|None|None"]),
+ (["id", "id"], ["a1|a1", "b2|b2", "None|None", "d4|d4"]),
+ ],
+)
+def test_append_partition_key_md5_hashes_only_the_key_values_in_key_order(partition_keys, expected_hash_inputs):
+ rows = pa.table(
+ {
+ "payload": pa.array([{"n": 1}, {"n": 2}, None, {"n": 4}]),
+ "count": pa.array([1, 2, 3, None], type=pa.int64()),
+ "id": pa.array(["a1", "b2", None, "d4"]),
+ "seen_at": pa.array(
+ [
+ datetime.datetime(2024, 3, 1, 10),
+ datetime.datetime(2024, 3, 1, 10, 0, 0, 250000),
+ None,
+ datetime.datetime(2024, 3, 2),
+ ],
+ type=pa.timestamp("us"),
+ ),
+ }
+ )
+ table = pa.concat_tables([rows.slice(0, 1), rows.slice(1)])
+
+ result = append_partition_key_to_table(
+ table=table,
+ partition_count=45,
+ partition_size=None,
+ partition_keys=partition_keys,
+ partition_mode="md5",
+ partition_format=None,
+ logger=cast(FilteringBoundLogger, structlog.get_logger()),
+ )
+
+ assert result is not None
+ assert result.table.column_names == [*table.column_names, PARTITION_KEY]
+ assert result.table.column(PARTITION_KEY).to_pylist() == [
+ str(int(hashlib.md5(value.encode()).hexdigest(), 16) % 45) for value in expected_hash_inputs
+ ]
+
+
def test_billing_limit_exception_is_non_reportable_error():
# Subclassing NonReportableError is what keeps the intentional billing-limit halt out of
# error tracking (the activity interceptor re-raises these without capturing them).