: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): handle arro3 RecordBatch in table statistics aggregation (#66423)
Tom Owers19e6b3ee2d48
diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.pyindex e1ab450872b6..67418d1a7d94 100644--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/compute_table_statistics.py@@ -17,7 +17,7 @@ import jsonimport uuidimport dataclassesfrom datetime import timedelta-from typing import TYPE_CHECKING, Any+from typing import Anyfrom django.utils import timezone@@ -38,9 +38,6 @@ from products.warehouse_sources.backend.models.external_data_schema import Exterfrom products.warehouse_sources.backend.models.table import DataWarehouseTablefrom products.warehouse_sources.backend.models.util import clean_type-if TYPE_CHECKING:- import pyarrow as pa-logger = structlog.get_logger(__name__)STATISTICS_FEATURE_FLAG = "data-warehouse-column-statistics"@@ -119,14 +116,18 @@ class _ColumnStat:has_min_max: bool-def _aggregate_add_action_stats(add_actions: "pa.Table", columns: dict[str, Any]) -> tuple[int, dict[str, _ColumnStat]]:+def _aggregate_add_action_stats(add_actions: Any, columns: dict[str, Any]) -> tuple[int, dict[str, _ColumnStat]]:"""Aggregate the Delta log's per-file stats into per-column whole-table stats.`add_actions` is `DeltaTable.get_add_actions(flatten=True)` — one row per live file, with`num_records` and (where the log carries stats) `null_count.<col>`, `min.<col>`, `max.<col>`.Returns `(row_count, {column_name: _ColumnStat})`. Columns with no log stats get `has_min_max=False`."""- data = add_actions.to_pydict()+ import pyarrow as pa # noqa: PLC0415 — heavy dep kept off this module's flag-check import path++ # deltalake>=1.x returns an arro3 RecordBatch (no `to_pydict`); pa.table() normalizes both that+ # (via the Arrow C interface) and an already-pyarrow input to a pyarrow Table we can read as a dict.+ data = pa.table(add_actions).to_pydict()row_count = sum(n for n in data.get("num_records", []) if n is not None)result: dict[str, _ColumnStat] = {}diff --git a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.pyindex d11ee7f743b3..57f0b12cbc5f 100644--- a/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py+++ b/products/warehouse_sources/backend/temporal/data_imports/workflow_activities/tests/test_compute_table_statistics.py@@ -53,6 +53,19 @@ class TestAggregateAddActionStats:assert stats["amount"].max_value == "15"assert stats["amount"].has_min_max is True+ def test_accepts_arro3_record_batch_from_deltalake(self) -> None:+ # deltalake>=1.x returns an arro3 RecordBatch (no `to_pydict`) from get_add_actions; the helper+ # must normalize it to pyarrow rather than crash with AttributeError.+ from arro3.core import RecordBatch++ pa_table = pa.table({"num_records": [10, 20], "null_count.amount": [1, 2], "min.amount": [5, 3]})+ arro3_batch = RecordBatch.from_arrow(pa_table.to_batches()[0])++ row_count, stats = _aggregate_add_action_stats(arro3_batch, {"amount": "Int64"})+ assert row_count == 30+ assert stats["amount"].null_count == 3+ assert stats["amount"].min_value == "3"+def test_column_without_log_stats_marks_has_min_max_false(self) -> None:# A column present in the table but with no min/max/null in the Delta log (e.g. nested type).add_actions = pa.table({"num_records": [5]})