: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): give unpartitioned repartition targets an md5 bucket count (#80368)
Tom Owers1a81c8d70275
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition.pyindex 5f3b298720db..74822fb2df4b 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/repartition.py@@ -350,9 +350,11 @@ def select_repartition_target(Computes the target directly from measured bytes so one repartition lands under budget ratherthan stepping blindly: md5 grows the bucket count, numerical shrinks the row-size, datetime steps- one format tier finer. When no target is chosen the reason explains why (reported in metrics so a- skipped table is diagnosable): `within_budget`, `datetime_at_finest_tier`, `numerical_cannot_shrink`,- `numerical_no_size`, or `unpartitionable_no_keys`. A chosen target carries reason `selected`.+ one format tier finer. An unpartitioned table gets an auto target, which sizes its bucket count+ the same way so md5 is reachable whatever its keys turn out to be. When no target is chosen the+ reason explains why (reported in metrics so a skipped table is diagnosable): `within_budget`,+ `datetime_at_finest_tier`, `numerical_cannot_shrink`, `numerical_no_size`, or+ `unpartitionable_no_keys`. A chosen target carries reason `selected`."""if not partition_bytes:return None, "no_partitions"@@ -405,7 +407,18 @@ def select_repartition_target(# Unpartitioned but over budget: attempt to enable partitioning via auto-detection. Needs keys.if not keys:return None, "unpartitionable_no_keys"- return RepartitionTarget(partition_keys=keys, trigger_reason="", partition_mode=None), "selected"+ # Carry a bucket count so md5 is always reachable. Auto-detection prefers numerical, then+ # datetime, and falls through to md5 only when neither applies — but md5 needs a count, and+ # without one a table whose keys suit none of the detectors (a UUID primary key and no+ # `created_at`-style column) has no scheme at all. It flags as over budget every day, fails the+ # rewrite with "No supported partition mode", and grows unbounded. Hashed keys bucket any key+ # type, and a primary key never changes, so a row keeps its bucket across merges.+ return RepartitionTarget(+ partition_keys=keys,+ trigger_reason="",+ partition_mode=None,+ partition_count=max(1, math.ceil(total_bytes / target_partition_bytes)),+ ), "selected"# Coarsening (the reverse direction) rebuilds an over-fragmented table into fewer, larger partitions.diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition.pyindex 5e40e069bfa1..c74f3c267798 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test_repartition.py@@ -214,7 +214,8 @@ class TestSelectRepartitionTarget:{"partition_mode": None, "primary_key_columns": ["id"]},{None: 5000},1000,- {"partition_mode": None, "partition_keys": ["id"]},+ # The sized count is what makes md5 reachable when auto-detection finds nothing else.+ {"partition_mode": None, "partition_keys": ["id"], "partition_count": 5},),("unpartitioned_without_keys_noop",@@ -702,6 +703,72 @@ class TestRewriteIntoTemp:assert resolved.partition_mode == "datetime"assert resolved.partition_keys == ["created_at"]+ def test_uuid_key_without_a_datetime_column_resolves_md5(self, tmp_path):+ # The production failure: a UUID primary key and no `created_at`-style column matches none of+ # the detectors, so the rewrite died with "No supported partition mode" and the table stayed+ # unpartitioned, over budget, and OOM-killing its pod on every merge.+ rows = 10+ table = pa.table(+ {+ "id": pa.array([f"0198d931-1efe-73b9-aad5-feb84ed767{i:02d}" for i in range(rows)], type=pa.string()),+ "endTimestamp": pa.array(+ [datetime.datetime(2024, 1, (i % 27) + 1) for i in range(rows)], type=pa.timestamp("us")+ ),+ }+ )+ deltalake.write_deltalake(str(tmp_path / "src"), table)+ old_delta = deltalake.DeltaTable(str(tmp_path / "src"))++ rows_written, resolved = asyncio.run(+ _rewrite_into_temp(+ old_delta=old_delta,+ temp_uri=str(tmp_path / "tmp"),+ storage_options={},+ target=RepartitionTarget(+ partition_keys=["id"], trigger_reason="test", partition_mode=None, partition_count=4+ ),+ batch_size=3,+ logger=logger,+ )+ )++ assert rows_written == rows+ assert resolved.partition_mode == "md5"+ # Hashing the primary key, not the timestamp: a primary key never changes, so a row keeps its+ # bucket when a later merge updates it.+ assert resolved.partition_keys == ["id"]++ def test_datetime_still_wins_over_the_md5_fallback(self, tmp_path):+ # The count only supplies a floor. A table auto-detection can partition properly must still+ # get that scheme, or every table would collapse onto hashed buckets.+ rows = 10+ table = pa.table(+ {+ "id": pa.array([f"0198d931-1efe-73b9-aad5-feb84ed767{i:02d}" for i in range(rows)], type=pa.string()),+ "created_at": pa.array(+ [datetime.datetime(2024, 1, (i % 27) + 1) for i in range(rows)], type=pa.timestamp("us")+ ),+ }+ )+ deltalake.write_deltalake(str(tmp_path / "src"), table)+ old_delta = deltalake.DeltaTable(str(tmp_path / "src"))++ _rows_written, resolved = asyncio.run(+ _rewrite_into_temp(+ old_delta=old_delta,+ temp_uri=str(tmp_path / "tmp"),+ storage_options={},+ target=RepartitionTarget(+ partition_keys=["id"], trigger_reason="test", partition_mode=None, partition_count=4+ ),+ batch_size=3,+ logger=logger,+ )+ )++ assert resolved.partition_mode == "datetime"+ assert resolved.partition_keys == ["created_at"]+def test_batch_with_real_null_in_non_nullable_column_is_backfilled_not_crashed(self, tmp_path):# The live table's own declared schema can mark a column non-nullable (e.g. a source NOT# NULL constraint recorded on first sync) while a scanned batch still carries an actual