Github user JoshRosen commented on a diff in the pull request:
https://github.com/apache/spark/pull/8018#discussion_r36581734
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala ---
@@ -131,52 +106,36 @@ final class Decimal extends Ordered[Decimal] with
Serializable {
*/
def set(decimal: Decimal): Decimal = {
this.decimalVal = decimal.decimalVal
- this.longVal = decimal.longVal
this._precision = decimal._precision
- this._scale = decimal._scale
this
}
- def toBigDecimal: BigDecimal = {
- if (decimalVal.ne(null)) {
- decimalVal(MATH_CONTEXT)
- } else {
- BigDecimal(longVal, _scale)(MATH_CONTEXT)
- }
- }
+ def toBigDecimal: BigDecimal = BigDecimal(toJavaBigDecimal)
- def toJavaBigDecimal: java.math.BigDecimal = toBigDecimal.underlying()
+ private[sql] def toJavaBigDecimal: JavaBigDecimal = decimalVal
def toUnscaledLong: Long = {
- if (decimalVal.ne(null)) {
- decimalVal.underlying().unscaledValue().longValue()
+ val unscaled = PlatformDependent.UNSAFE.getLong(decimalVal,
--- End diff --
In my JDK's version of `BigDecimal`, it looks like the `intCompact` field
is marked as `transient`:
```
private transient long intCompact;
```
As a result, I wondered if we have to worry about this returning the wrong
value after roundtrip. However, it looks like BigDecimal's `readObject()`
method will end up recomputing / repopulating this field:
```java
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in all fields
s.defaultReadObject();
// validate possibly bad fields
if (intVal == null) {
String message = "BigDecimal: null intVal in stream";
throw new java.io.StreamCorruptedException(message);
// [all values of scale are now allowed]
}
intCompact = compactValFor(intVal);
}
```
As a result, I think this is fine.
---
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]