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(postgres): round decimals exceeding max scale instead of failing sync (#66349)

Tom Owers9fd4773a252c
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.py
index c3ceac4f34be..d9bd2b1a02ae 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/test/test_pipeline_utils.py
@@ -418,6 +418,30 @@ def test_table_from_py_list_huge_int_column_falls_back_to_string():
assert table.column("column").to_pylist() == [str(enormous), "5"]
+def test_table_from_py_list_decimal_exceeding_max_scale_is_rounded():
+ # An unconstrained Postgres `numeric` can carry more decimal places than Delta Lake's max
+ # scale (32). pyarrow refuses to rescale it ("Rescaling Decimal256 value would cause data
+ # loss"); the value must be rounded to the max scale rather than crashing the whole sync.
+ # A None is mixed in to exercise the null-passthrough in the quantize retry path.
+ value = decimal.Decimal("1." + "1" * 80)
+
+ table = table_from_py_list([{"column": value}, {"column": None}])
+
+ col_type = table.schema.field("column").type
+ assert pa.types.is_decimal256(col_type)
+ assert col_type.scale == 32
+ assert table.column("column").to_pylist() == [decimal.Decimal("1." + "1" * 32), None]
+
+
+def test_table_from_py_list_decimal_too_large_for_decimal256_raises():
+ # A value whose integer part can't fit decimal256(76, 32) even after rounding to the max
+ # scale is genuinely unrepresentable, so the hard ValueError must still surface.
+ value = decimal.Decimal("9" * 45 + "." + "1" * 40)
+
+ with pytest.raises(ValueError, match="Cannot build decimal array from values"):
+ table_from_py_list([{"column": value}])
+
+
def test_table_from_py_list_normal_int_column_stays_int64():
# Values within int64 are unaffected by the overflow handling.
table = table_from_py_list([{"column": 1}, {"column": 2}, {"column": None}])
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/utils.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/utils.py
index 9f2aa36b2fd2..bee66f40d124 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/utils.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline/utils.py
@@ -624,6 +624,14 @@ def _get_max_decimal_type(values: list[decimal.Decimal]) -> pa.Decimal128Type |
return build_pyarrow_decimal_type(max_precision, max_scale)
+def _quantize_to_scale(value: decimal.Decimal, scale: int) -> decimal.Decimal:
+ quantum = decimal.Decimal(1).scaleb(-scale)
+ with decimal.localcontext() as ctx:
+ # Hold decimal256's full 76 digits while rounding so the quantize itself can't overflow.
+ ctx.prec = 76
+ return value.quantize(quantum, rounding=decimal.ROUND_HALF_EVEN)
+
+
def _decimal_array_from_values(values: list[decimal.Decimal | None]) -> pa.Array:
non_null = [v for v in values if v is not None]
if non_null:
@@ -632,10 +640,19 @@ def _decimal_array_from_values(values: list[decimal.Decimal | None]) -> pa.Array
return pa.array(values, type=optimal_type)
except pa.ArrowInvalid:
pass
+
+ fallback_type = pa.decimal256(76, MAX_NUMERIC_SCALE)
try:
- return pa.array(values, type=pa.decimal256(76, MAX_NUMERIC_SCALE))
- except Exception as exc:
- raise ValueError("Cannot build decimal array from values") from exc
+ return pa.array(values, type=fallback_type)
+ except Exception:
+ # A value carries more decimal places than Delta Lake's max scale (e.g. an unconstrained
+ # Postgres `numeric`). pyarrow refuses to silently truncate, so round to the max scale
+ # ourselves rather than failing the whole sync.
+ try:
+ quantized = [None if v is None else _quantize_to_scale(v, MAX_NUMERIC_SCALE) for v in values]
+ return pa.array(quantized, type=fallback_type)
+ except Exception as exc:
+ raise ValueError("Cannot build decimal array from values") from exc
def _python_type_to_pyarrow_type(type_: type, value: Any):