Github user liancheng commented on a diff in the pull request:
https://github.com/apache/spark/pull/8907#discussion_r40371067
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/CatalystRowConverter.scala
---
@@ -332,24 +338,30 @@ private[parquet] class CatalystRowConverter(
private def toDecimal(value: Binary): Decimal = {
val precision = decimalType.precision
val scale = decimalType.scale
- val bytes = value.getBytes
if (precision <= CatalystSchemaConverter.MAX_PRECISION_FOR_INT64) {
- // Constructs a `Decimal` with an unscaled `Long` value if
possible.
+ // Constructs a `Decimal` with an unscaled `Long` value if
possible. The underlying
+ // `ByteBuffer` implementation is guaranteed to be
`HeapByteBuffer`, so here we are using
+ // `Binary.toByteBuffer.array()` to steal the underlying byte
array without copying it.
+ val buffer = value.toByteBuffer
+ val bytes = buffer.array()
+ val start = buffer.position()
+ val end = buffer.limit()
+
var unscaled = 0L
- var i = 0
+ var i = start
- while (i < bytes.length) {
+ while (i < end) {
unscaled = (unscaled << 8) | (bytes(i) & 0xff)
i += 1
}
- val bits = 8 * bytes.length
+ val bits = 8 * (end - start)
unscaled = (unscaled << (64 - bits)) >> (64 - bits)
Decimal(unscaled, precision, scale)
} else {
// Otherwise, resorts to an unscaled `BigInteger` instead.
- Decimal(new BigDecimal(new BigInteger(bytes), scale), precision,
scale)
+ Decimal(new BigDecimal(new BigInteger(value.getBytes), scale),
precision, scale)
--- End diff --
For decimals whose precision is greater than 18, we still need to copy the
byte array anyway to construct Java `BigInteger` instances.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]