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): raise decimal precision so the statistics fold doesn't misfire (#108295)

Tom Owersd2bb04cd4bf7
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.py
index b29ab88eb529..309992967259 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
@@ -21,7 +21,7 @@ import uuid
import dataclasses
from collections.abc import Callable, Iterable
from datetime import date, datetime, timedelta
-from decimal import Decimal
+from decimal import Decimal, localcontext
from typing import Any
from django.conf import settings
@@ -328,8 +328,12 @@ def _parse_log_value(delta_type: Any, value: Any) -> Any:
if delta_type in ("timestamp", "timestamp_ntz"):
return datetime.fromisoformat(value)
if delta_type.startswith("decimal("):
- scale = int(delta_type[len("decimal(") : -1].split(",")[1])
- return Decimal(value).quantize(Decimal(1).scaleb(-scale))
+ precision, scale = (int(p) for p in delta_type[len("decimal(") : -1].split(","))
+ # The default context (28 significant digits) is narrower than Delta allows (up to 38),
+ # so a value using the column's full precision would otherwise blow the context and
+ # raise a spurious InvalidOperation on quantize even though it fits the column's type.
+ with localcontext(prec=precision):
+ return Decimal(value).quantize(Decimal(1).scaleb(-scale))
except (ValueError, ArithmeticError):
raise _UnparseableValue(f"cannot parse log value as {delta_type}") from None
raise TypeError(f"no log representation for {delta_type}")
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.py
index fc09e6fa74c8..f996eec28cb9 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
@@ -33,6 +33,7 @@ from products.warehouse_sources.backend.temporal.data_imports.workflow_activitie
ComputeTableStatisticsInputs,
ComputeTableStatisticsWorkflow,
_aggregate_add_action_stats,
+ _parse_log_value,
compute_table_statistics_activity,
compute_table_statistics_sync,
)
@@ -136,6 +137,19 @@ class TestAggregateAddActionStats:
assert stats["v"].max_value == expected_max
+class TestParseLogValue:
+ def test_decimal_using_its_full_precision_is_not_mistaken_for_unparseable(self) -> None:
+ # Regression: quantize() used the default decimal context (28 significant digits), which is
+ # narrower than Delta allows (up to 38). A high-precision decimal(38,32) value legitimately
+ # using all 38 digits blew that context and raised _UnparseableValue even though the value
+ # fits its column's type fine.
+ value = Decimal("123456.12345678901234567890123456789012")
+ assert _parse_log_value("decimal(38,32)", value) == value
+
+ def test_decimal_rounds_to_the_columns_scale(self) -> None:
+ assert _parse_log_value("decimal(10,2)", Decimal("1.505")) == Decimal("1.50")
+
+
@pytest.mark.django_db
class TestComputeTableStatisticsSync:
def _team(self, *, ai_approved: bool = False) -> Team: