zhenyue-xu opened a new pull request, #9670:
URL: https://github.com/apache/seatunnel/pull/9670
### Purpose of this pull request
This pull request fixes a precision loss issue in the JDBC connector when
converting Float values to BigDecimal during data splitting and mathematical
operations. The main improvements include:
1. **Fixed Float to BigDecimal conversion precision loss** in
`FixedChunkSplitter.convertToBigDecimal()` method
2. **Fixed Float subtraction precision issues** in `ObjectUtils.minus()`
method
3. **Improved numerical accuracy** for JDBC data splitting operations
involving Float values
4. **Enhanced data integrity** in scenarios where precise Float calculations
are critical
This change addresses a critical precision issue where Float values were
losing accuracy during BigDecimal conversions due to Java's automatic type
promotion behavior.
### Does this PR introduce _any_ user-facing change?
**No**, this PR does not introduce any user-facing changes.
This is an internal bug fix that improves the precision of Float to
BigDecimal conversions within the JDBC connector's data processing logic. Users
will not notice any API changes, configuration changes, or behavioral
differences in normal usage scenarios.
**Technical details of the fix**:
**Root cause**: Java's `BigDecimal.valueOf()` method does not have a
`valueOf(float)` overload. When called with a Float parameter, the float value
is automatically promoted to double, which can introduce precision errors.
**Previous behavior**:
```java
Float f = 0.1f;
BigDecimal bd = BigDecimal.valueOf(f); // Automatic promotion: float ->
double
// Result: 0.10000000149011612 (precision loss due to float-to-double
conversion)
```
**New behavior**:
```java
Float f = 0.1f;
BigDecimal bd = new BigDecimal(f.toString());
// Result: 0.1 (preserves original Float precision)
```
**Impact**: This fix ensures that Float values maintain their intended
precision during JDBC data splitting operations, preventing potential data
integrity issues in edge cases involving high-precision Float calculations.
**Backward compatibility**: Fully backward compatible - no changes to public
APIs, configuration options, or expected user behavior.
### How was this patch tested?
This patch addresses a specific precision issue with Float to BigDecimal
conversions. Testing approach:
1. **Root cause verification**:
- Confirmed that `BigDecimal.valueOf()` lacks a `valueOf(float)` method
in Java
- Verified that automatic float-to-double promotion causes precision loss
- Tested that `new BigDecimal(Float.toString())` preserves original Float
precision
2. **Manual precision testing**:
```java
Float testFloat = 0.1f;
// Old approach
BigDecimal oldWay = BigDecimal.valueOf(testFloat); // 0.10000000149011612
// New approach
BigDecimal newWay = new BigDecimal(testFloat.toString()); // 0.1
```
3. **Regression testing**:
- Ran existing JDBC connector unit tests to ensure no functionality
regression
- Verified that all other numeric type conversions (Integer, Long,
Double, etc.) continue to work correctly
- Confirmed that the change only affects Float type processing
4. **Edge case validation**:
- Tested with various Float values that commonly exhibit precision issues
- Verified proper handling in both `convertToBigDecimal()` and `minus()`
operations
- Ensured consistent behavior across different Float value ranges
**Recommended additional testing**: Unit tests should be added to
specifically validate Float precision in BigDecimal conversions, particularly
for the edge cases where float-to-double promotion introduces measurable
precision errors.
### Check list
* [ ] If any new Jar binary package adding in your PR, please add License
Notice according
[New License
Guide](https://github.com/apache/seatunnel/blob/dev/docs/en/contribution/new-license.md)
* [ ] If necessary, please update the documentation to describe the new
feature. https://github.com/apache/seatunnel/tree/dev/docs
* [ ] If you are contributing the connector code, please check that the
following files are updated:
1. Update
[plugin-mapping.properties](https://github.com/apache/seatunnel/blob/dev/plugin-mapping.properties)
and add new connector information in it
2. Update the pom file of
[seatunnel-dist](https://github.com/apache/seatunnel/blob/dev/seatunnel-dist/pom.xml)
3. Add ci label in
[label-scope-conf](https://github.com/apache/seatunnel/blob/dev/.github/workflows/labeler/label-scope-conf.yml)
4. Add e2e testcase in
[seatunnel-e2e](https://github.com/apache/seatunnel/tree/dev/seatunnel-e2e/seatunnel-connector-v2-e2e/)
5. Update connector
[plugin_config](https://github.com/apache/seatunnel/blob/dev/config/plugin_config)
--
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]