This is an automated email from the ASF dual-hosted git repository.

vatsrahul1001 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 94a793c2a66 Preserve Decimal precision through XCom serialization 
(#72064)
94a793c2a66 is described below

commit 94a793c2a667499c84b4a585ee697e44eca46906
Author: 2sumtech <[email protected]>
AuthorDate: Mon Nov 30 12:34:20 2026 -0800

    Preserve Decimal precision through XCom serialization (#72064)
    
    A Decimal that needs more than 53 bits of mantissa is silently rounded on
    its way through XCom because the serde encoder stores it as a float. Encode
    fractional Decimals as strings instead. The deserializer has rebuilt values
    with Decimal(str(data)) since the serializer was added, so the version
    marker stays at 1 and old float payloads keep decoding unchanged.
    
    Generated-by: Claude Code (Fable 5)
---
 task-sdk/src/airflow/sdk/serde/serializers/bignum.py |  7 ++++---
 task-sdk/tests/task_sdk/serde/test_serializers.py    | 11 ++++++++++-
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/task-sdk/src/airflow/sdk/serde/serializers/bignum.py 
b/task-sdk/src/airflow/sdk/serde/serializers/bignum.py
index 8d53786221f..6d6d9bdbb41 100644
--- a/task-sdk/src/airflow/sdk/serde/serializers/bignum.py
+++ b/task-sdk/src/airflow/sdk/serde/serializers/bignum.py
@@ -42,9 +42,10 @@ def serialize(o: object) -> tuple[U, str, int, bool]:
     _, _, exponent = o.as_tuple()
     if isinstance(exponent, int) and exponent >= 0:  # No digits after the 
decimal point.
         return int(o), name, __version__, True
-    # Technically lossy due to floating point errors, but the best we
-    # can do without implementing a custom encode function.
-    return float(o), name, __version__, True
+    # ``str`` keeps every significant digit where ``float`` would round to 53 
bits
+    # of mantissa. The version stays at 1 because ``deserialize`` has always 
gone
+    # through ``Decimal(str(data))``, so older readers accept this payload too.
+    return str(o), name, __version__, True
 
 
 def deserialize(cls: type, version: int, data: object) -> decimal.Decimal:
diff --git a/task-sdk/tests/task_sdk/serde/test_serializers.py 
b/task-sdk/tests/task_sdk/serde/test_serializers.py
index 7e23dc86b28..3c8b34ac706 100644
--- a/task-sdk/tests/task_sdk/serde/test_serializers.py
+++ b/task-sdk/tests/task_sdk/serde/test_serializers.py
@@ -183,7 +183,16 @@ class TestSerializers:
 
     @pytest.mark.parametrize(
         ("expr", "expected"),
-        [("1", "1"), ("52e4", "520000"), ("2e0", "2"), ("12e-2", "0.12"), 
("12.34", "12.34")],
+        [
+            ("1", "1"),
+            ("52e4", "520000"),
+            ("2e0", "2"),
+            ("12e-2", "0.12"),
+            ("12.34", "12.34"),
+            ("3.14159265358979323846", "3.14159265358979323846"),
+            ("12345678901234567.89", "12345678901234567.89"),
+            ("0.1234567890123456789", "0.1234567890123456789"),
+        ],
     )
     def test_encode_decimal(self, expr, expected):
         assert deserialize(serialize(decimal.Decimal(expr))) == 
decimal.Decimal(expected)

Reply via email to