-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-40014][SQL] Support cast of decimals to ANSI intervals #37466
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
|
@HyukjinKwon @cloud-fan @gengliangwang Could you review this PR, please. |
| d: Decimal, p: Int, s: Int, startField: Byte, endField: Byte): Int = { | ||
| try { | ||
| val months = if (endField == YEAR) d.toBigDecimal * MONTHS_PER_YEAR else d.toBigDecimal | ||
| months.setScale(0, BigDecimal.RoundingMode.HALF_UP).toIntExact |
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.
do we have a sql reference doc for cast? we should mention the rounding behavior.
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 have the doc for cast: https://spark.apache.org/docs/latest/sql-ref-ansi-compliance.html#cast but it says that Spark doesn't support cast of intervals to numerics. Let me open a PR and document recent changes in casting of ANSI intervals.
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.
@cloud-fan I opened the PR #37495 which updates the doc.
|
Merging to master. Thank you, @cloud-fan for review. |
### What changes were proposed in this pull request? In the PR, I propose to update the doc page https://spark.apache.org/docs/latest/sql-ref-ansi-compliance.html#cast about new behaviour of CAST introduced by the following PRs: 1. [[SPARK-40008][SQL] Support casting of integrals to ANSI intervals](#37442) 2. [[SPARK-39451][SQL] Support casting intervals to integrals in ANSI mode](#36811) 3. [[SPARK-39470][SQL] Support cast of ANSI intervals to decimals](#36857) 4. [[SPARK-40014][SQL] Support cast of decimals to ANSI intervals](#37466) <img width="1004" alt="Screenshot 2022-08-12 at 15 00 10" src="https://user-images.githubusercontent.com/1580697/184349887-700c64f6-e9c8-40c4-85cc-a77072506cf5.png"> ### Why are the changes needed? To inform Spark SQL about new behaviour of `CAST`. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? By building Spark SQL docs and check them manually: ``` $ SKIP_SCALADOC=1 SKIP_PYTHONDOC=1 SKIP_RDOC=1 bundle exec jekyll build ``` Closes #37495 from MaxGekk/doc-interval-cast. Authored-by: Max Gekk <[email protected]> Signed-off-by: Max Gekk <[email protected]>
What changes were proposed in this pull request?
In the PR, I propose to support casts of decimals to ANSI intervals, and follow the SQL standard:

Why are the changes needed?
To improve user experience with Spark SQL, and to conform to the SQL standard.
Does this PR introduce any user-facing change?
No, it just extends existing behavior of casts.
Before:
After:
How was this patch tested?
By running new tests: