[SPARK-49095][SQL] Update DecimalTypeand decimal fields compatible logic of Avro data source to avoid loss of decimal precision
#47584
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
This PR aims to enhance comparison logic to avoid precision loss in decimal parts of
Decimalin Avro data source. It refers to the related logic of convertDecimaltype inParquetdata source #44513 .Before:
precision - scale) matches, it can be converted.After:
precision - scale) and decimal part(scale) lengths must match.Why are the changes needed?
Fixed the issue causing missing data accuracy.
Does this PR introduce any user-facing change?
Yes, stricter matching requirements for conversions between Spark
DecimalTypeandAvroDecimal type.Previously, decimal(12,10) -> decimal(5,3) was allowed, but there would be some loss of precision in the decimal part, such as: 13.1234567890 -> 13.123
After that, the exception
avroIncompatibleReadErrorwill be thrown, because 3 is not greater than or equal 10. Unless both the integer and the decimal part are greater than or equal. For example, decimal(15, 13) is OK because 13>=10 and 15-13>=12-10But users can restore the legacy behavior, set
spark.sql.legacy.avro.allowIncompatibleDecimalTypetotrue.How was this patch tested?
Pass GA and add new test cases.
Was this patch authored or co-authored using generative AI tooling?
No.