-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-39470][SQL] Support cast of ANSI intervals to decimals #36857
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
Changes from all commits
ade9160
d992ca5
3160dd5
679571d
d6fc899
77818ff
90fb367
654b2d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,3 +116,13 @@ select cast(interval '10' day as bigint); | |
|
|
||
| select cast(interval '-1000' month as tinyint); | ||
| select cast(interval '1000000' second as smallint); | ||
|
|
||
| -- cast ANSI intervals to decimals | ||
| select cast(interval '-1' year as decimal(10, 0)); | ||
| select cast(interval '1.000001' second as decimal(10, 6)); | ||
| select cast(interval '08:11:10.001' hour to second as decimal(10, 4)); | ||
| select cast(interval '1 01:02:03.1' day to second as decimal(8, 1)); | ||
| select cast(interval '10.123' second as decimal(4, 2)); | ||
| select cast(interval '10.005' second as decimal(4, 2)); | ||
| select cast(interval '10.123' second as decimal(5, 2)); | ||
| select cast(interval '10.123' second as decimal(1, 0)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a check. The result is truncated. Please, take a look. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -838,3 +838,71 @@ struct<> | |
| -- !query output | ||
| org.apache.spark.SparkArithmeticException | ||
| [CAST_OVERFLOW] The value INTERVAL '1000000' SECOND of the type "INTERVAL SECOND" cannot be cast to "SMALLINT" due to an overflow. Use `try_cast` to tolerate overflow and return NULL instead. If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error. | ||
|
|
||
|
|
||
| -- !query | ||
| select cast(interval '-1' year as decimal(10, 0)) | ||
| -- !query schema | ||
| struct<CAST(INTERVAL '-1' YEAR AS DECIMAL(10,0)):decimal(10,0)> | ||
| -- !query output | ||
| -1 | ||
|
|
||
|
|
||
| -- !query | ||
| select cast(interval '1.000001' second as decimal(10, 6)) | ||
| -- !query schema | ||
| struct<CAST(INTERVAL '01.000001' SECOND AS DECIMAL(10,6)):decimal(10,6)> | ||
| -- !query output | ||
| 1.000001 | ||
|
|
||
|
|
||
| -- !query | ||
| select cast(interval '08:11:10.001' hour to second as decimal(10, 4)) | ||
| -- !query schema | ||
| struct<CAST(INTERVAL '08:11:10.001' HOUR TO SECOND AS DECIMAL(10,4)):decimal(10,4)> | ||
| -- !query output | ||
| 29470.0010 | ||
|
|
||
|
|
||
| -- !query | ||
| select cast(interval '1 01:02:03.1' day to second as decimal(8, 1)) | ||
| -- !query schema | ||
| struct<CAST(INTERVAL '1 01:02:03.1' DAY TO SECOND AS DECIMAL(8,1)):decimal(8,1)> | ||
| -- !query output | ||
| 90123.1 | ||
|
|
||
|
|
||
| -- !query | ||
| select cast(interval '10.123' second as decimal(4, 2)) | ||
| -- !query schema | ||
| struct<CAST(INTERVAL '10.123' SECOND AS DECIMAL(4,2)):decimal(4,2)> | ||
| -- !query output | ||
| 10.12 | ||
|
|
||
|
|
||
| -- !query | ||
| select cast(interval '10.005' second as decimal(4, 2)) | ||
| -- !query schema | ||
| struct<CAST(INTERVAL '10.005' SECOND AS DECIMAL(4,2)):decimal(4,2)> | ||
| -- !query output | ||
| 10.01 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @srielau I think the rounding behavior is correct. |
||
|
|
||
|
|
||
| -- !query | ||
| select cast(interval '10.123' second as decimal(5, 2)) | ||
| -- !query schema | ||
| struct<CAST(INTERVAL '10.123' SECOND AS DECIMAL(5,2)):decimal(5,2)> | ||
| -- !query output | ||
| 10.12 | ||
|
|
||
|
|
||
| -- !query | ||
| select cast(interval '10.123' second as decimal(1, 0)) | ||
| -- !query schema | ||
| struct<> | ||
| -- !query output | ||
| org.apache.spark.SparkArithmeticException | ||
| [CANNOT_CHANGE_DECIMAL_PRECISION] Decimal(compact, 10, 18, 6) cannot be represented as Decimal(1, 0). If necessary set "spark.sql.ansi.enabled" to "false" to bypass this error. | ||
| == SQL(line 1, position 8) == | ||
| select cast(interval '10.123' second as decimal(1, 0)) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
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 need examples with rounding:
INTERVAL '12.123' SECOND AS DECIMAL(3, 1) => 12.1
INTERVAL '12.005' SECOND AS DECIMAL(4, 2) => 12.01
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.
No, we don't round and don't loose info. See the last check that I added:
Both of your cases are the same, actually - total number of digits is greater than 3 or 4.
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.
This is different. My examples test sucess. Yours tests a failure. Obviously any number>= 10 and <100 cannot be even approximated in a single digit.
But any number between 0 and 9 can be approximated in a digit.
I’m curious why you see truncation rather than rounding given that decimal to decimal rounds.