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-imports): measure partition sizes without delta-rs stats (#108048)

Tom Owers4f99077bf819
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.py
index 3ba889528e45..6e9368aec32a 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
@@ -239,24 +239,42 @@ class RepartitionTarget:
return cls(**{k: v for k, v in data.items() if k in fields})
+# Delta's fixed marker for a Hive-style partition directory whose column value is null.
+_HIVE_NULL_PARTITION = "__HIVE_DEFAULT_PARTITION__"
+
+
+def _partition_key_from_add_path(path: str) -> str | None:
+ """Recover the `_ph_partition_key` value from a Hive-style partitioned add-action file path.
+
+ Every value this pipeline actually partitions by — an md5 bucket index, a numerical bucket or the
+ `null` sentinel, or a datetime tier like "2024-01" — is plain ASCII with nothing that needs
+ percent-encoding, so splitting the path recovers the same value `get_add_actions`'s
+ `partition.<key>` column would give.
+ """
+ prefix = f"{PARTITION_KEY}="
+ for segment in path.split("/"):
+ if segment.startswith(prefix):
+ value = segment[len(prefix) :]
+ return None if value == _HIVE_NULL_PARTITION else value
+ return None
+
+
def measure_partition_bytes(delta_table: deltalake.DeltaTable) -> dict[str | None, int]:
"""At-rest bytes per partition, read from the Delta log (no S3 LIST, no data scan).
Unpartitioned tables collapse to a single `None` bucket. Keyed by the `_ph_partition_key` value.
- """
- actions = delta_table.get_add_actions(flatten=True)
- columns = actions.schema.names
- sizes = actions.column("size_bytes").to_pylist()
- partition_column = f"partition.{PARTITION_KEY}"
- keys: list[str | None]
- if partition_column in columns:
- keys = list(actions.column(partition_column).to_pylist())
- else:
- keys = [None] * len(sizes)
+ Reads `get_add_file_sizes` (file path -> size only), not `get_add_actions`: the latter also
+ materializes every column's min/max/null-count stats into Arrow arrays, and a table with enough
+ files can push a single stats column past Arrow's 2^31-byte offset limit for a default
+ (32-bit-offset) string array, raising `Offset overflow error` and aborting detection for an
+ otherwise healthy table.
+ """
+ partitioned = PARTITION_KEY in (delta_table.metadata().partition_columns or [])
totals: dict[str | None, int] = defaultdict(int)
- for key, size in zip(keys, sizes):
+ for path, size in delta_table._table.get_add_file_sizes().items():
+ key = _partition_key_from_add_path(path) if partitioned else None
totals[key] += size or 0
return dict(totals)
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.py
index d03c27ce80f6..bdd11652a03c 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
@@ -550,6 +550,36 @@ class TestMeasurePartitionBytes:
assert list(sizes.keys()) == [None]
assert sizes[None] > 0
+ def test_partitioned_null_value_groups_under_none(self, tmp_path):
+ table = pa.table(
+ {
+ PARTITION_KEY: pa.array([None, "2024-01", "2024-01"], type=pa.string()),
+ "id": pa.array([1, 2, 3], type=pa.int64()),
+ }
+ )
+ deltalake.write_deltalake(str(tmp_path / "n"), table, partition_by=[PARTITION_KEY])
+ sizes = measure_partition_bytes(deltalake.DeltaTable(str(tmp_path / "n")))
+ assert set(sizes.keys()) == {None, "2024-01"}
+ assert all(v > 0 for v in sizes.values())
+
+ def test_survives_get_add_actions_offset_overflow(self, tmp_path):
+ """A table with enough add-action stats can overflow Arrow's 32-bit string offsets inside
+ `get_add_actions` (`Offset overflow error`, see the error-tracking issue this guards against).
+ Measuring partition bytes must not depend on that call succeeding."""
+ delta = _write_month_partitioned(
+ str(tmp_path / "t"),
+ [
+ (1, datetime.datetime(2024, 1, 5)),
+ (2, datetime.datetime(2024, 2, 2)),
+ ],
+ )
+ with patch.object(
+ deltalake.DeltaTable, "get_add_actions", side_effect=Exception("Offset overflow error: 2229224676")
+ ):
+ sizes = measure_partition_bytes(delta)
+ assert set(sizes.keys()) == {"2024-01", "2024-02"}
+ assert all(v > 0 for v in sizes.values())
+
class TestRewriteIntoTemp:
def test_rebuckets_finer_preserving_all_rows(self, tmp_path):