You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "vboo123 (via GitHub)" <gi...@apache.org> on 2026/07/18 02:14:22 UTC
[PR] [SPARK-56486][SQL] Add XSDToSchema.extendDecimalPrecision to prevent … [spark]
vboo123 opened a new pull request, #57348:
URL: https://github.com/apache/spark/pull/57348
### What changes were proposed in this pull request?
Add a new opt-in utility method `XSDToSchema.extendDecimalPrecision(schema: StructType, maxPrecision: Int = 38): StructType` that widens `DecimalType` precision to accommodate full integer-digit capacity.
For each `DecimalType(p, s)` in the schema, the method raises precision to `min(maxPrecision, p + s)` while keeping scale unchanged. It recurses into nested `StructType` fields and `ArrayType` elements.
The default inference behavior of `XSDToSchema.read()` is intentionally unchanged: it still maps XSD `totalDigits`/`fractionDigits` to `DecimalType(totalDigits, fractionDigits)` per the W3C spec. Users opt in by calling `extendDecimalPrecision` on the returned schema.
### Why are the changes needed?
`XSDToSchema.read()` maps an XSD decimal restriction with `totalDigits=t` and `fractionDigits=f` to `DecimalType(t, f)`. This is spec-correct, but Spark's `DecimalType` scale is fixed, whereas the XSD `fractionDigits` facet is only a maximum. A value that uses fewer fractional digits and more integer digits overflows precision. For example, `totalDigits=20, fractionDigits=5` produces `DecimalType(20, 5)` which allows only 15 integer digits. Values exceeding that are silently read as `null` in 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.extendDecimalPrecision` is added. Existing behavior of `XSDToSchema.read()` is unchanged.
### How was this patch tested?
Three new unit tests in `XSDToSchemaSuite`:
- Verifies decimal widening and capping at max precision (e.g. `(12,6)` -> `(18,6)`, `(38,18)` unchanged)
- Verifies recursion into nested struct fields and array elements
- Verifies an explicit `maxPrecision` parameter is respected
Ran 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)
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56486][SQL] Add XSDToSchema.extendDecimalPrecision to prevent … [spark]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #57348:
URL: https://github.com/apache/spark/pull/57348#issuecomment-5017688041
@sandip-db FYI
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56486][SQL] Add XSDToSchema.extendDecimalPrecision to prevent silent decimal truncation [spark]
Posted by "vboo123 (via GitHub)" <gi...@apache.org>.
vboo123 commented on PR #57348:
URL: https://github.com/apache/spark/pull/57348#issuecomment-5372032958
@HyukjinKwon
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56486][SQL] Add XSDToSchema.extendDecimalPrecision to prevent silent decimal truncation [spark]
Posted by "sandip-db (via GitHub) via reviews" <re...@spark.apache.org>.
sandip-db commented on code in PR #57348:
URL: https://github.com/apache/spark/pull/57348#discussion_r4649899135
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/xml/XSDToSchema.scala:
##########
@@ -66,6 +66,42 @@ object XSDToSchema extends Logging{
getStructType(xmlSchema)
}
+ /**
+ * Widens every [[DecimalType]] in the given schema so that decimals inferred from an XSD's
+ * `totalDigits`/`fractionDigits` facets can hold their full integer range without silent
+ * truncation. `read` maps `totalDigits=t, fractionDigits=f` to `DecimalType(t, f)`, which is
+ * correct per the W3C XML Schema spec but leaves only `t - f` integer digits. Because Spark's
+ * decimal scale is fixed while the XSD `fractionDigits` facet is only a maximum, values that
+ * use fewer fractional digits and more integer digits overflow the precision and are silently
+ * read as null. This opt-in utility raises each `DecimalType(p, s)` to
+ * `DecimalType(min(maxPrecision, p + s), s)`, recursing into nested struct fields and array
+ * elements. It does not change `read`'s default inference, so existing callers are unaffected.
+ *
+ * @param schema the schema returned by `read`
+ * @param maxPrecision the maximum precision a widened decimal may reach; must not exceed
+ * [[DecimalType.MAX_PRECISION]]
+ * @return a copy of the schema with decimal precisions widened
+ * @throws IllegalArgumentException if `maxPrecision` is not in [1, [[DecimalType.MAX_PRECISION]]]
+ */
+ def extendDecimalPrecision(
+ schema: StructType,
+ maxPrecision: Int = DecimalType.MAX_PRECISION): StructType = {
+ require(maxPrecision > 0 && maxPrecision <= DecimalType.MAX_PRECISION,
+ s"maxPrecision must be in [1, ${DecimalType.MAX_PRECISION}], got $maxPrecision")
+ StructType(schema.map { field =>
+ field.copy(dataType = extendDecimalType(field.dataType, maxPrecision))
+ })
+ }
+
+ private def extendDecimalType(dataType: DataType, maxPrecision: Int): DataType = dataType match {
+ case DecimalType.Fixed(precision, scale) =>
+ DecimalType(math.min(maxPrecision, precision + scale), scale)
Review Comment:
**Blocking (P1):** With no lower bound at `p`, a `maxPrecision` below a field's precision narrows it: at 22, `read`'s `DecimalType(38, 18)` for a plain `xs:decimal` becomes `DecimalType(22, 18)`, so by my reading of `StaxXmlParser` a value like `12345.6` overflows and is read as null in PERMISSIVE mode. A cap below the scale, such as 10, makes the call throw instead.
See **Shared repair plan 1** in the review body.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/xml/XSDToSchema.scala:
##########
@@ -66,6 +66,42 @@ object XSDToSchema extends Logging{
getStructType(xmlSchema)
}
+ /**
+ * Widens every [[DecimalType]] in the given schema so that decimals inferred from an XSD's
+ * `totalDigits`/`fractionDigits` facets can hold their full integer range without silent
Review Comment:
**Non-blocking (P2):** A capped field keeps its scale, so it holds only `38 - s` integer digits: `totalDigits=30, fractionDigits=10` becomes `DecimalType(38, 10)`, with 28. The new flag's documentation should state this limit.
See **Shared repair plan 1** in the review body.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/xml/XSDToSchema.scala:
##########
@@ -66,6 +66,42 @@ object XSDToSchema extends Logging{
getStructType(xmlSchema)
}
+ /**
+ * Widens every [[DecimalType]] in the given schema so that decimals inferred from an XSD's
+ * `totalDigits`/`fractionDigits` facets can hold their full integer range without silent
+ * truncation. `read` maps `totalDigits=t, fractionDigits=f` to `DecimalType(t, f)`, which is
+ * correct per the W3C XML Schema spec but leaves only `t - f` integer digits. Because Spark's
+ * decimal scale is fixed while the XSD `fractionDigits` facet is only a maximum, values that
+ * use fewer fractional digits and more integer digits overflow the precision and are silently
+ * read as null. This opt-in utility raises each `DecimalType(p, s)` to
+ * `DecimalType(min(maxPrecision, p + s), s)`, recursing into nested struct fields and array
+ * elements. It does not change `read`'s default inference, so existing callers are unaffected.
+ *
+ * @param schema the schema returned by `read`
+ * @param maxPrecision the maximum precision a widened decimal may reach; must not exceed
+ * [[DecimalType.MAX_PRECISION]]
+ * @return a copy of the schema with decimal precisions widened
+ * @throws IllegalArgumentException if `maxPrecision` is not in [1, [[DecimalType.MAX_PRECISION]]]
+ */
+ def extendDecimalPrecision(
Review Comment:
**Non-blocking (P2):** Please replace this helper with an opt-in Boolean on both `read` overloads, applied at the `XSD_DECIMAL` case (`XSDToSchema.scala:126-134`) where the facets are known. That widens only facet-derived decimals, leaves `read`'s 38-digit defaults and caller-built decimals alone, and has no `maxPrecision` to narrow with.
See **Shared repair plan 1** in the review body.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56486][SQL] Add XSDToSchema.extendDecimalPrecision to prevent … [spark]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #57348:
URL: https://github.com/apache/spark/pull/57348#issuecomment-5017686027
Let's make the PR title complete. Seems truncated
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org