Skip to content

Conversation

@beliefer
Copy link
Contributor

@beliefer beliefer commented Sep 8, 2022

What changes were proposed in this pull request?

This PR used to improve the implementation of Spark Decimal. The improvement points are as follows:

  1. Use toJavaBigDecimal instead of toBigDecimal.bigDecimal
  2. Extract longVal / POW_10(_scale) as a new method def actualLongVal: Long
  3. Remove BIG_DEC_ZERO and use decimalVal.signum to judge whether or not equals zero.
  4. Use < instead of compare.
  5. Correct some code style.

Why are the changes needed?

Improve the implementation of Spark Decimal

Does this PR introduce any user-facing change?

'No'.
Just update the inner implementation.

How was this patch tested?

N/A

@github-actions github-actions bot added the SQL label Sep 8, 2022
@beliefer
Copy link
Contributor Author

beliefer commented Sep 8, 2022

The failure test case is not related to this PR.
@MaxGekk @gengliangwang cc @cloud-fan

@cloud-fan
Copy link
Contributor

I don't think eq and ne has observable perf difference, can we revert this part?

@beliefer
Copy link
Contributor Author

beliefer commented Sep 9, 2022

I don't think eq and ne has observable perf difference, can we revert this part?

OK

} else {
BigDecimal(longVal, _scale)
}
def toBigDecimal: BigDecimal = if (decimalVal.ne(null)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we avoid pure code style change? the previous code style was not wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

def % (that: Decimal): Decimal =
if (that.isZero) null
else Decimal(toJavaBigDecimal.remainder(that.toJavaBigDecimal, MATH_CONTEXT))
if (that.isZero) null else Decimal(toJavaBigDecimal.remainder(that.toJavaBigDecimal,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's avoid pure code style changes.

}

def abs: Decimal = if (this.compare(Decimal.ZERO) < 0) this.unary_- else this
def abs: Decimal = if (this < Decimal.ZERO) this.unary_- else this
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this have a real impact?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this < Decimal.ZERO is more clear.


def toFloat: Float = toBigDecimal.floatValue

private def rawLongValue: Long = longVal / POW_10(_scale)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's still use the old name actualLongVal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

@cloud-fan
Copy link
Contributor

thanks, merging to master!

@cloud-fan cloud-fan closed this in 193b5b2 Sep 15, 2022
@beliefer
Copy link
Contributor Author

@cloud-fan Thank you !

LuciferYang pushed a commit to LuciferYang/spark that referenced this pull request Sep 20, 2022
### What changes were proposed in this pull request?
This PR used to improve the implementation of Spark `Decimal`. The improvement points are as follows:
1. Use `toJavaBigDecimal` instead of  `toBigDecimal.bigDecimal`
2. Extract `longVal / POW_10(_scale)` as a new method `def actualLongVal: Long`
3. Remove `BIG_DEC_ZERO` and use `decimalVal.signum` to judge whether or not equals zero.
4. Use `<` instead of `compare`.
5. Correct some code style.

### Why are the changes needed?
Improve the implementation of Spark Decimal

### Does this PR introduce _any_ user-facing change?
'No'.
Just update the inner implementation.

### How was this patch tested?
N/A

Closes apache#37830 from beliefer/SPARK-40387.

Authored-by: Jiaan Geng <[email protected]>
Signed-off-by: Wenchen Fan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants