nacz0 opened a new issue, #41706:
URL: https://github.com/apache/beam/issues/41706

   ### What happened?
   
   ### What happened?
   
   `DecimalCoderImpl` in the Python SDK encodes and decodes values under the 
thread's `decimal` context. The default
   context has a precision of 28 significant digits. A `Decimal` with more 
digits is rounded without any error: on encode
   the wire scale stays while trailing digits become zeros, and on decode 
trailing digits and scale can both be truncated.
   Java's `BigDecimalCoder` accepts the rounded value, so nothing fails 
downstream.
   
   Environment: Apache Beam Python SDK 2.77.0, Python 3.12. The code on master
   (4139cd43b7cec8512d739c188912761166229d56) is unchanged.
   
   ```python
   import decimal
   from apache_beam.coders.coders import DecimalCoder
   
   coder = DecimalCoder()
   value = decimal.Decimal("1234567890123456789012345678.0123456789")  # 38 
significant digits
   print(coder.decode(coder.encode(value)))
   # 1234567890123456789012345678   <- the 10 fraction digits are gone
   
   decimal.getcontext().prec = 38
   print(coder.decode(coder.encode(value)))
   # 1234567890123456789012345678.0123456789
   ```
   
   Both directions lose digits:
   - **Encode.** `int(value.scaleb(scale))` rounds to the context precision 
before it writes the unscaled integer. For
     the value above, Java receives `1234567890123456789012345678.0000000000`.
   - **Decode.** `decimal.Decimal(int_value).scaleb(-scale)` rounds again. 
Exact bytes written by Java's
     `BigDecimalCoder` decode in Python as `1234567890123456789012345678`.
   
   Code:
   [`coder_impl.py`, 
`DecimalCoderImpl`](https://github.com/apache/beam/blob/4139cd43b7cec8512d739c188912761166229d56/sdks/python/apache_beam/coders/coder_impl.py#L2195-L2215).
   
   ### Impact
   
   Any decimal with more than 28 significant digits that passes through 
`DecimalCoder` is affected. That includes row
   coders with `DECIMAL` or `fixed_decimal` fields. Common sources are SQL 
`NUMERIC(38, s)` and `DECIMAL(38, s)`
   columns, which many databases and Iceberg, Parquet and BigQuery `BIGNUMERIC` 
support. We found it while writing
   `NUMERIC(38,10)` values from JDBC to Iceberg through Managed I/O: the row 
coder rounded them before the Java writer,
   which accepted the rounded values without an error.
   
   A workaround is to raise `decimal.getcontext().prec` on the thread that 
encodes. That is fragile, because the coder
   often runs on a runner thread that user code does not set up.
   
   ### Expected behavior
   
   `DecimalCoder` round-trips every finite `Decimal` without losing digits or 
scale (`-0` still decodes as `0`, as before),
   whatever the thread's context precision, and writes the same bytes as today 
for the values it already handles. Building
   the unscaled integer from `value.as_tuple()`, and the decoded value from the 
integer's digits with `-scale` as exponent,
   gives that. A patch with a test follows in a PR.
   We checked it against 20,000 random values of up to 120 digits at precisions 
5, 28 and 60:
   - all of them round-trip with identical digits and scale (`-0` still decodes 
as `0`);
   - every value the current coder already keeps exactly is encoded to 
identical bytes and decoded to the identical
     value, so the format stays compatible with Java's `BigDecimalCoder`.
   
   ### Related
   
   - #23373 (Python support for logical types with arguments, such as 
`fixed_decimal`) and #41520 (JdbcIO drops the
     precision and scale of JDBC `DECIMAL`) also concern decimals, but they are 
about types, not values.
   
   
   ### Issue Priority
   
   Priority: 2 (default / most bugs should be filed as P2)
   
   ### Issue Components
   
   - [x] Component: Python SDK
   - [ ] Component: Java SDK
   - [ ] Component: Go SDK
   - [ ] Component: Typescript SDK
   - [ ] Component: IO connector
   - [ ] Component: Beam YAML
   - [ ] Component: Beam examples
   - [ ] Component: Beam playground
   - [ ] Component: Beam katas
   - [ ] Component: Website
   - [ ] Component: Infrastructure
   - [ ] Component: Spark Runner
   - [ ] Component: Flink Runner
   - [ ] Component: Prism Runner
   - [ ] Component: Hazelcast Jet Runner
   - [ ] Component: Google Cloud Dataflow Runner


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to