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): reconcile decimals on delta append writes (#74636)

Tom Owersaf006d90bb87
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta_table_helper.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta_table_helper.py
index 0100f9426530..23a1c017cfde 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta_table_helper.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/delta_table_helper.py
@@ -605,6 +605,13 @@ class DeltaTableHelper:
storage_options=storage_options,
partition_by=PARTITION_KEY if use_partitioning else None,
)
+ else:
+ # An append re-casts each source column to its stored type, same as a merge. A decimal
+ # column that outgrew decimal128 arrives here as text (decimal256 renders to string),
+ # and delta-rs can't parse the scientific notation arrow emits for scale-heavy zeros
+ # (e.g. '0E-18') back into the stored decimal — an opaque DeltaError that retries
+ # forever. Align to the stored decimal types up front, exactly as the merge path does.
+ data = align_incoming_decimals_to_delta(data, delta_table.schema())
await self._logger.adebug(f"write_to_deltalake: write_type = append")
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_delta_table_helper.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_delta_table_helper.py
index 3eabefb9e63a..ead7652c4851 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_delta_table_helper.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/test/test_delta_table_helper.py
@@ -14,7 +14,10 @@ import deltalake
import pyarrow.compute as pc
from parameterized import parameterized
-from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.arrow_utils import evolve_pyarrow_schema
+from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.arrow_utils import (
+ SchemaColumnTypeChangedException,
+ evolve_pyarrow_schema,
+)
from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.consts import PARTITION_KEY
from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.delta_table_helper import (
DeltaTableHelper,
@@ -599,6 +602,74 @@ class TestLegacyDltTableReconciliation:
assert set(final.column("id").to_pylist()) == {1, 2, 3}
+class TestAppendDecimalReconciliation:
+ """Appending a decimal column that outgrew decimal128 must reconcile to the stored type.
+
+ A batch whose numeric column exceeds decimal128 is promoted to decimal256, which
+ `evolve_pyarrow_schema` renders to text for the Delta write. Arrow emits scientific
+ notation for scale-heavy zeros (e.g. '0E-18'), which delta-rs can't parse back into
+ the stored decimal — an opaque, infinitely-retrying DeltaError on the append path.
+ """
+
+ def _seed_decimal_table(self, delta_path: str) -> deltalake.DeltaTable:
+ table = pa.table(
+ {"id": pa.array([1], type=pa.int64()), "amount": pa.array([Decimal("1.5")], type=pa.decimal128(38, 10))}
+ )
+ deltalake.write_deltalake(delta_path, table)
+ return deltalake.DeltaTable(delta_path)
+
+ @pytest.mark.asyncio
+ async def test_scale_heavy_batch_is_rounded_to_stored_type(self, tmp_path: Path) -> None:
+ delta_path = str(tmp_path / "table")
+ dt = self._seed_decimal_table(delta_path)
+ helper = _make_local_helper(delta_path)
+
+ # Values fit decimal128's integer budget but carry more scale than the stored column,
+ # so they land as decimal256 and evolve renders them to text (the zero as '0E-18').
+ batch = evolve_pyarrow_schema(
+ pa.table(
+ {
+ "id": pa.array([2, 3], type=pa.int64()),
+ "amount": pa.array(
+ [Decimal("0.12345678901234567890"), Decimal("0E-18")], type=pa.decimal256(76, 20)
+ ),
+ }
+ ),
+ dt.schema(),
+ )
+ assert pa.types.is_string(batch.schema.field("amount").type)
+
+ result = await helper.write_to_deltalake(
+ data=batch, write_type="append", should_overwrite_table=False, primary_keys=None
+ )
+
+ final = result.to_pyarrow_table()
+ assert final.schema.field("amount").type == pa.decimal128(38, 10)
+ assert set(final.column("id").to_pylist()) == {1, 2, 3}
+ assert Decimal("0") in final.column("amount").to_pylist()
+
+ @pytest.mark.asyncio
+ async def test_integer_overflow_batch_raises_clean_non_retryable(self, tmp_path: Path) -> None:
+ delta_path = str(tmp_path / "table")
+ dt = self._seed_decimal_table(delta_path)
+ helper = _make_local_helper(delta_path)
+
+ batch = evolve_pyarrow_schema(
+ pa.table(
+ {
+ "id": pa.array([2, 3], type=pa.int64()),
+ "amount": pa.array([Decimal("1" + "0" * 35 + ".5"), Decimal("0E-18")], type=pa.decimal256(76, 18)),
+ }
+ ),
+ dt.schema(),
+ )
+
+ with pytest.raises(SchemaColumnTypeChangedException):
+ await helper.write_to_deltalake(
+ data=batch, write_type="append", should_overwrite_table=False, primary_keys=None
+ )
+
+
class TestIncrementalBatchDeduplication:
"""Duplicate PKs in a source batch must never reach the Delta write.