[SPARK-56486][SQL] Add XSDToSchema.extendDecimalPrecision to prevent …#57348
Open
vboo123 wants to merge 1 commit into
Open
[SPARK-56486][SQL] Add XSDToSchema.extendDecimalPrecision to prevent …#57348vboo123 wants to merge 1 commit into
vboo123 wants to merge 1 commit into
Conversation
…silent decimal truncation XSDToSchema.read maps an XSD decimal restriction (totalDigits=t, fractionDigits=f) to DecimalType(t, f), which is correct per the W3C XML Schema spec. Spark's decimal scale is fixed, however, while the XSD fractionDigits facet is only a maximum, so values that use fewer fractional digits and more integer digits overflow the precision and are silently read as null. Add an opt-in utility, XSDToSchema.extendDecimalPrecision(schema, maxPrecision), that raises each DecimalType(p, s) to DecimalType(min(maxPrecision, p + s), s), recursing into nested struct fields and array elements. read()'s default inference is unchanged, so existing callers are unaffected. Generated using Kiro (Claude Opus 4.8)
shrirangmhalgi
left a comment
Contributor
There was a problem hiding this comment.
The formula is correct for the stated problem (XSD fractionDigits is a maximum, not fixed → need totalDigits + fractionDigits precision to hold full integer range). Implementation cleanly recurses into nested types; MapType is correctly skipped since XSDToSchema.read() never produces it.
One minor nit: the Scaladoc states maxPrecision "must not exceed DecimalType.MAX_PRECISION" but there's no explicit require - invalid inputs propagate to DecimalType's constructor which throws a less informative error. Consider adding a guard:
require(maxPrecision > 0 && maxPrecision <= DecimalType.MAX_PRECISION,
s"maxPrecision must be in [1, ${DecimalType.MAX_PRECISION}], got $maxPrecision")
Member
|
Let's make the PR title complete. Seems truncated |
Member
|
@sandip-db FYI |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?
Add a new opt-in utility method
XSDToSchema.extendDecimalPrecision(schema: StructType, maxPrecision: Int = 38): StructTypethat widensDecimalTypeprecision to accommodate full integer-digit capacity.For each
DecimalType(p, s)in the schema, the method raises precision tomin(maxPrecision, p + s)while keeping scale unchanged. It recurses into nestedStructTypefields andArrayTypeelements.The default inference behavior of
XSDToSchema.read()is intentionally unchanged: it still maps XSDtotalDigits/fractionDigitstoDecimalType(totalDigits, fractionDigits)per the W3C spec. Users opt in by callingextendDecimalPrecisionon the returned schema.Why are the changes needed?
XSDToSchema.read()maps an XSD decimal restriction withtotalDigits=tandfractionDigits=ftoDecimalType(t, f). This is spec-correct, but Spark'sDecimalTypescale is fixed, whereas the XSDfractionDigitsfacet is only a maximum. A value that uses fewer fractional digits and more integer digits overflows precision. For example,totalDigits=20, fractionDigits=5producesDecimalType(20, 5)which allows only 15 integer digits. Values exceeding that are silently read asnullin non-ANSI mode, causing data loss with no warning.This matches the fix proposed by the JIRA reporter: an opt-in utility rather than changing default behavior.
JIRA: https://issues.apache.org/jira/browse/SPARK-56486
Does this PR introduce any user-facing change?
Yes. A new public utility method
XSDToSchema.extendDecimalPrecisionis added. Existing behavior ofXSDToSchema.read()is unchanged.How was this patch tested?
Three new unit tests in
XSDToSchemaSuite:(12,6)->(18,6),(38,18)unchanged)maxPrecisionparameter is respectedRan the suite locally (all pass). Full lint/test matrix via GitHub Actions.
Was this patch authored or co-authored using generative AI tooling?
Generated using Kiro (Claude Opus 4.8)