-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-32043][SQL] Replace Decimal by Int op in make_interval and make_timestamp
#28886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Test build #124337 has finished for PR 28886 at commit
|
|
@dongjoon-hyun While working on #28873, I had realised that some Decimal ops are not needed actually, and they can be replaced by regular int ops. May I ask you to take a look at this. |
| val nanosPerSec = Decimal(NANOS_PER_SECOND, 10, 0) | ||
| val nanos = ((secAndNanos - secFloor) * nanosPerSec).toInt | ||
| val seconds = secFloor.toInt | ||
| assert(secAndMicros.scale == 6, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I doubt that this is the right place to ask, but I tried to Google this but didn't find anything. I was just wondering what does assert(secAndMicros.scale == 6 do exactly? I am assuming that it changes the Decimal's scale to 6, but what does that exactly help with? And is the added tail just a bunch of zeros? For instance does 3.14 become 3.140000?
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am assuming that it changes the Decimal's scale to 6
This assert doesn't change the scale, it just reads it.
And is the added tail just a bunch of zeros? For instance does 3.14 become 3.140000?
It shifts the decimal point. For example, if you have 3.14 with precision 6 and scale 3 that means 003.140. If you set
- scale to 0, it becomes 3140
- scale to 4 -> 0.314
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, thank you so much!
|
@cloud-fan @juliuszsompolski @HyukjinKwon Could you review this small perf improvement. |
| val totalMonths = Math.addExact(months, Math.multiplyExact(years, MONTHS_PER_YEAR)) | ||
| val totalDays = Math.addExact(days, Math.multiplyExact(weeks, DAYS_PER_WEEK)) | ||
| var micros = (secs * Decimal(MICROS_PER_SECOND)).toLong | ||
| assert(secs.scale == 6, "Seconds fractional must have 6 digits for microseconds") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shall we check the precision as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can check it but precision value is not important for this code...or I am wrong?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, Decimal guarantees that precision >= scale (@Ngone51 correct?). That's enough for the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically, I think we should say DecimalType guarantees precision >= scale. But since any Decimal is directly or indirectly bounded with a DecimalType. Therefore, Decimal also guarantees precision >= scale.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just want to be consistent with https://github.com/apache/spark/pull/28886/files#diff-b83497f7bc11578a0b63a814a2a30f48R2003
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added assert there to guarantee that Int will not overflow. What would you like to assert here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for sanity check? The precision must be <= 8 here, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not anymore, please, take a look at #28873 and https://issues.apache.org/jira/browse/SPARK-32021
|
thanks, merging to master! |
What changes were proposed in this pull request?
Replace Decimal by Int op in the
MakeInterval&MakeTimestampexpression. For instance,(secs * Decimal(MICROS_PER_SECOND)).toLongcan be replaced by the unscaled long because the former one already contains microseconds.Why are the changes needed?
To improve performance.
Before:
After:
Does this PR introduce any user-facing change?
No
How was this patch tested?
IntervalExpressionsSuite,DateExpressionsSuiteand etc.MakeDateTimeBenchmarkin the environment: