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(data-warehouse): use resolved partition keys for all repartition batches (#70813)

Tom Owers0f4cbe56b3f5
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/repartition.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/repartition.py
index 06497a7c2ee1..51b188a7ec8c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/repartition.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/repartition.py
@@ -359,11 +359,14 @@ async def _rewrite_into_temp(
if PARTITION_KEY in table.column_names:
table = table.drop([PARTITION_KEY])
+ # After the first batch resolves, later batches must use the *resolved* keys too: datetime
+ # auto-detect swaps the key from the primary key to the detected timestamp column, and pairing
+ # the resolved mode with the original (e.g. UUID) key would fail to parse it as a date.
result = append_partition_key_to_table(
table=table,
partition_count=target.partition_count,
partition_size=target.partition_size,
- partition_keys=target.partition_keys,
+ partition_keys=resolved.partition_keys if resolved else target.partition_keys,
partition_mode=resolved.partition_mode if resolved else target.partition_mode,
partition_format=resolved.partition_format if resolved else target.partition_format,
logger=logger,
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test_repartition.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test_repartition.py
index 55e3f488038e..935317945a9c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test_repartition.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test_repartition.py
@@ -262,6 +262,37 @@ class TestRewriteIntoTemp:
# created_at is a timestamp column named like a datetime key → auto-detects datetime mode.
assert resolved.partition_mode == "datetime"
+ def test_resolved_keys_apply_to_batches_after_the_first(self, tmp_path):
+ # Auto-detect swaps the target's primary key (a UUID string) for the detected timestamp
+ # column. Batches after the first must use the resolved key — pairing the resolved
+ # datetime mode with the original UUID key raised ParserError mid-rewrite in production.
+ 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),
+ batch_size=3, # force batches after the resolving first one
+ logger=logger,
+ )
+ )
+
+ assert rows_written == rows
+ assert resolved.partition_mode == "datetime"
+ assert resolved.partition_keys == ["created_at"]
+
class _FakeS3CM:
"""Minimal async-context-manager stand-in for `aget_s3_client()`."""