codope opened a new pull request, #12993:
URL: https://github.com/apache/hudi/pull/12993

   ### Change Logs
   
   When reading column statistics for Decimal fields from the metadata table, 
the unwrapped decimal values are incorrectly handled. Specifically:
   
   - **Type Loss in Unwrapping:**  
     The `tryUnpackValueWrapper` method was matching a `BytesWrapper` before a 
`DecimalWrapper`, causing values written as `DecimalWrapper` (which are 
actually decimals) to be interpreted as raw bytes.
   
   - **Incorrect Deserialization:**  
     In the `deserialize` method, the decimal conversion was not robust enough:
     - It only handled the case where the unwrapped value is a `ByteBuffer`.
     - It did not enforce the correct scale/precision when the value was 
already a decimal (either as a Scala `BigDecimal` or a `java.math.BigDecimal`).
     - Additionally, it was using a private Avro constructor for `Decimal`, 
which prevented proper conversion.
   
   This PR addresses these issues with the following changes:
   
   1. **Reordering in `tryUnpackValueWrapper`:**  
      - The `DecimalWrapper` case is moved before the `BytesWrapper` case. This 
ensures that values written as decimals are unwrapped correctly.
   
   2. **Enhancements to `deserialize`:**  
      - **ByteBuffer Handling:**  
        The conversion now uses Avro’s public factory method 
(`org.apache.avro.LogicalTypes.decimal(precision, scale)`) to create a Decimal 
logical type with the correct precision and scale.
      - **Direct Decimal Values:**  
        The method now properly handles cases where the unwrapped value is 
already a `scala.math.BigDecimal` or a `java.math.BigDecimal`. In both cases, 
it enforces the target scale using `.setScale(dt.scale, 
java.math.RoundingMode.UNNECESSARY)`.
        
   3. **Unit Tests:**  
      - New unit tests have been added to cover:
        - Decimal values unwrapped as a `ByteBuffer`.
        - Decimal values unwrapped as a `java.math.BigDecimal`.
        - Decimal values unwrapped as a Scala `BigDecimal`.
      - Additionally, we reuse the existing utility method to generate a 
DataFrame with decimals to ensure that our logic works correctly in an 
integrated scenario.
   
   ### Impact
   
   Decimal values retain their intended semantics when read from the metadata 
table, ensuring that comparisons and filtering, and hence data skipping, based 
on these values work correctly.
   
   ### Risk level (write none, low medium or high below)
   
   low
   
   ### Documentation Update
   
   _Describe any necessary documentation update if there is any new feature, 
config, or user-facing change. If not, put "none"._
   
   - _The config description must be updated if new configs are added or the 
default value of the configs are changed_
   - _Any new feature or user-facing change requires updating the Hudi website. 
Please create a Jira ticket, attach the
     ticket number here and follow the 
[instruction](https://hudi.apache.org/contribute/developer-setup#website) to 
make
     changes to the website._
   
   ### Contributor's checklist
   
   - [ ] Read through [contributor's 
guide](https://hudi.apache.org/contribute/how-to-contribute)
   - [ ] Change Logs and Impact were stated clearly
   - [ ] Adequate tests were added if applicable
   - [ ] CI passed
   


-- 
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