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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]