You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/06/13 17:06:31 UTC
[GitHub] [spark] MaxGekk opened a new pull request, #36857: [WIP][SQL] Support cast of ANSI intervals to decimals
MaxGekk opened a new pull request, #36857:
URL: https://github.com/apache/spark/pull/36857
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'core/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
<!--
Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
2. If you fix some SQL features, you can provide some references of other DBMSes.
3. If there is design documentation, please add the link.
4. If there is a discussion in the mailing list, please add the link.
-->
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
2. If you fix a bug, you can clarify why it is a bug.
-->
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as the documentation fix.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
### How was this patch tested?
By running new tests:
```
$ build/sbt "sql/testOnly org.apache.spark.sql.SQLQueryTestSuite -- -z cast.sql"
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898176537
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuiteBase.scala:
##########
@@ -1272,4 +1272,37 @@ abstract class CastSuiteBase extends SparkFunSuite with ExpressionEvalHelper {
"to restore the behavior before Spark 3.0."))
}
}
+
+ test("cast ANSI intervals to decimals") {
+ Seq(
Review Comment:
No, we don't round and don't loose info. See the last check that I added:
```
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))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
Both of your cases are the same, actually - total number of digits is greater than 3 or 4.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] srielau commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
srielau commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898093995
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1015,6 +1014,11 @@ case class Cast(
} catch {
case _: NumberFormatException => null
}
+ case x: DayTimeIntervalType =>
+ buildCast[Long](_, dt => changePrecision(dayTimeIntervalToDecimal(dt, x.endField), target))
+ case x: YearMonthIntervalType =>
+ buildCast[Int](_, ym =>
+ changePrecision(Decimal(yearMonthIntervalToInt(ym, x.startField, x.endField)), target))
Review Comment:
I will take confusion on what is scale and what is precision to my grave....
We can round/truncate, but not modulo/wrap.
So I see:
CAST(INTERVAL '1.234' SECOND AS DECIMAL(1,0)) as perfectly fine.
But:
CAST(INTERVAL('123.4' SECOND AS DECIMAL(4, 2)) is an overflow.
On trunc vs round the semantic is given in DECIMAL to DECIMAL:
select cast(1.5 AS DECIMAL(1, 0));
2
PS: I loathe that cannot-change-decimal-precision error. In most cases we should just call it an overflow.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898692249
##########
sql/core/src/test/resources/sql-tests/inputs/cast.sql:
##########
@@ -116,3 +116,11 @@ 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(1, 0));
Review Comment:
Don't see much different between your example and mine. In both cases, there are not enough digits to represent the value. I mean there is a problem with precision first of all, not w/ scale. But let me check.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] srielau commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
srielau commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898093995
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1015,6 +1014,11 @@ case class Cast(
} catch {
case _: NumberFormatException => null
}
+ case x: DayTimeIntervalType =>
+ buildCast[Long](_, dt => changePrecision(dayTimeIntervalToDecimal(dt, x.endField), target))
+ case x: YearMonthIntervalType =>
+ buildCast[Int](_, ym =>
+ changePrecision(Decimal(yearMonthIntervalToInt(ym, x.startField, x.endField)), target))
Review Comment:
I will take confusion on what is scale and what is precision to my grave....
We can round/truncate, but not modulo/wrap.
So I see:
CAST(INTERVAL '1.234' SECOND AS DECIMAL(1,0)) as perfectly fine.
But:
CAST(INTERVAL('123.4' SECOND AS DECIMAL(4, 2)) is an overflow.
On trunc vs round the semantic is given in DECIMAL to DECIMAL:
select cast(1.5 AS DECIMAL(1, 0));
2
PS: I loathe that cannot change decimal precision error. In most cases we should just call it an overflow.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk commented on pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk commented on PR #36857:
URL: https://github.com/apache/spark/pull/36857#issuecomment-1157418195
Merging to master. Thank you, @cloud-fan and @srielau for review.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] cloud-fan commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898735214
##########
sql/core/src/test/resources/sql-tests/results/ansi/cast.sql.out:
##########
@@ -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
Review Comment:
@srielau I think the rounding behavior is correct.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r897724324
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1015,6 +1014,11 @@ case class Cast(
} catch {
case _: NumberFormatException => null
}
+ case x: DayTimeIntervalType =>
+ buildCast[Long](_, dt => changePrecision(dayTimeIntervalToDecimal(dt, x.endField), target))
+ case x: YearMonthIntervalType =>
+ buildCast[Int](_, ym =>
+ changePrecision(Decimal(yearMonthIntervalToInt(ym, x.startField, x.endField)), target))
Review Comment:
As we did before, we don't consider non-ANSI mode for ANSI intervals. So, we are in ANSI mode always for ANSI intervals.
> what if the decimal precision is too small, e.g. decimal(1, 0)
I think `changePrecision()` should output an error. Let me double check this.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898174044
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1015,6 +1014,11 @@ case class Cast(
} catch {
case _: NumberFormatException => null
}
+ case x: DayTimeIntervalType =>
+ buildCast[Long](_, dt => changePrecision(dayTimeIntervalToDecimal(dt, x.endField), target))
+ case x: YearMonthIntervalType =>
+ buildCast[Int](_, ym =>
+ changePrecision(Decimal(yearMonthIntervalToInt(ym, x.startField, x.endField)), target))
Review Comment:
> PS: I loathe that cannot-change-decimal-precision error. In most cases we should just call it an overflow.
I plan to do that.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898179945
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1015,6 +1014,11 @@ case class Cast(
} catch {
case _: NumberFormatException => null
}
+ case x: DayTimeIntervalType =>
+ buildCast[Long](_, dt => changePrecision(dayTimeIntervalToDecimal(dt, x.endField), target))
+ case x: YearMonthIntervalType =>
+ buildCast[Int](_, ym =>
+ changePrecision(Decimal(yearMonthIntervalToInt(ym, x.startField, x.endField)), target))
Review Comment:
Though, changing of scale/precision while keeping the same value is different thing from changing the value and get overflow.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898699293
##########
sql/core/src/test/resources/sql-tests/inputs/cast.sql:
##########
@@ -116,3 +116,11 @@ 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(1, 0));
Review Comment:
I added a check. The result is truncated. Please, take a look.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898691634
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1015,6 +1014,11 @@ case class Cast(
} catch {
case _: NumberFormatException => null
}
+ case x: DayTimeIntervalType =>
+ buildCast[Long](_, dt => changePrecision(dayTimeIntervalToDecimal(dt, x.endField), target))
Review Comment:
I have added new parameter already.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk closed pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk closed pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
URL: https://github.com/apache/spark/pull/36857
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] cloud-fan commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r897700182
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1015,6 +1014,11 @@ case class Cast(
} catch {
case _: NumberFormatException => null
}
+ case x: DayTimeIntervalType =>
+ buildCast[Long](_, dt => changePrecision(dayTimeIntervalToDecimal(dt, x.endField), target))
+ case x: YearMonthIntervalType =>
+ buildCast[Int](_, ym =>
+ changePrecision(Decimal(yearMonthIntervalToInt(ym, x.startField, x.endField)), target))
Review Comment:
shall we consider ansi mode here? what if the decimal precision is too small?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r897724324
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1015,6 +1014,11 @@ case class Cast(
} catch {
case _: NumberFormatException => null
}
+ case x: DayTimeIntervalType =>
+ buildCast[Long](_, dt => changePrecision(dayTimeIntervalToDecimal(dt, x.endField), target))
+ case x: YearMonthIntervalType =>
+ buildCast[Int](_, ym =>
+ changePrecision(Decimal(yearMonthIntervalToInt(ym, x.startField, x.endField)), target))
Review Comment:
As we did before, we don't consider non-ANSI mode for ANSI intervals. So, we are in ANSI mode always for ANSI intervals.
> what if the decimal precision is too small, e.g. decimal(1, 0)
I think `changePrecision()` should output an error.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] gengliangwang commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
gengliangwang commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898226615
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1015,6 +1014,11 @@ case class Cast(
} catch {
case _: NumberFormatException => null
}
+ case x: DayTimeIntervalType =>
+ buildCast[Long](_, dt => changePrecision(dayTimeIntervalToDecimal(dt, x.endField), target))
Review Comment:
Yeah always go with ANSI behavior in new features. Either adding a new parameter on `changePrecision` or adding a new method are fine.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk commented on pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk commented on PR #36857:
URL: https://github.com/apache/spark/pull/36857#issuecomment-1155594824
cc @srielau
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898692249
##########
sql/core/src/test/resources/sql-tests/inputs/cast.sql:
##########
@@ -116,3 +116,11 @@ 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(1, 0));
Review Comment:
Don't see much different between your example and mine. In both cases, there are not enough digits to represent the value. I mean there is a problem with precision first of all, not w/ scale. But let me check.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk commented on pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk commented on PR #36857:
URL: https://github.com/apache/spark/pull/36857#issuecomment-1157241074
@cloud-fan @srielau I added more checks, please, take a look at this PR one more time.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] srielau commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
srielau commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898729331
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuiteBase.scala:
##########
@@ -1272,4 +1272,37 @@ abstract class CastSuiteBase extends SparkFunSuite with ExpressionEvalHelper {
"to restore the behavior before Spark 3.0."))
}
}
+
+ test("cast ANSI intervals to decimals") {
+ Seq(
Review Comment:
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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r897905825
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1015,6 +1014,11 @@ case class Cast(
} catch {
case _: NumberFormatException => null
}
+ case x: DayTimeIntervalType =>
+ buildCast[Long](_, dt => changePrecision(dayTimeIntervalToDecimal(dt, x.endField), target))
+ case x: YearMonthIntervalType =>
+ buildCast[Int](_, ym =>
+ changePrecision(Decimal(yearMonthIntervalToInt(ym, x.startField, x.endField)), target))
Review Comment:
Added a test
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] srielau commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
srielau commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898099564
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CastSuiteBase.scala:
##########
@@ -1272,4 +1272,37 @@ abstract class CastSuiteBase extends SparkFunSuite with ExpressionEvalHelper {
"to restore the behavior before Spark 3.0."))
}
}
+
+ test("cast ANSI intervals to decimals") {
+ Seq(
Review Comment:
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
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898179945
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1015,6 +1014,11 @@ case class Cast(
} catch {
case _: NumberFormatException => null
}
+ case x: DayTimeIntervalType =>
+ buildCast[Long](_, dt => changePrecision(dayTimeIntervalToDecimal(dt, x.endField), target))
+ case x: YearMonthIntervalType =>
+ buildCast[Int](_, ym =>
+ changePrecision(Decimal(yearMonthIntervalToInt(ym, x.startField, x.endField)), target))
Review Comment:
Though, changes scale/precision while keeping the same value is different thing from changing the value and get overflow.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] cloud-fan commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898617688
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1015,6 +1014,11 @@ case class Cast(
} catch {
case _: NumberFormatException => null
}
+ case x: DayTimeIntervalType =>
+ buildCast[Long](_, dt => changePrecision(dayTimeIntervalToDecimal(dt, x.endField), target))
Review Comment:
let's add a new parameter then.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] cloud-fan commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r897737115
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1015,6 +1014,11 @@ case class Cast(
} catch {
case _: NumberFormatException => null
}
+ case x: DayTimeIntervalType =>
+ buildCast[Long](_, dt => changePrecision(dayTimeIntervalToDecimal(dt, x.endField), target))
+ case x: YearMonthIntervalType =>
+ buildCast[Int](_, ym =>
+ changePrecision(Decimal(yearMonthIntervalToInt(ym, x.startField, x.endField)), target))
Review Comment:
yea let's add some negative tests
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898168474
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1015,6 +1014,11 @@ case class Cast(
} catch {
case _: NumberFormatException => null
}
+ case x: DayTimeIntervalType =>
+ buildCast[Long](_, dt => changePrecision(dayTimeIntervalToDecimal(dt, x.endField), target))
Review Comment:
> Does changePrecision take a bool flag like nullOnOverflow?
No, it checks the SQL config actually, passed to the `Cast` expression:
```scala
private[this] def changePrecision(value: Decimal, decimalType: DecimalType): Decimal = {
if (value.changePrecision(decimalType.precision, decimalType.scale)) {
value
} else {
if (!ansiEnabled) {
null
} else {
throw QueryExecutionErrors.cannotChangeDecimalPrecisionError(
value, decimalType.precision, decimalType.scale, queryContext)
}
}
}
```
Probably, it makes sense to incapsulate the logic:
https://github.com/apache/spark/blob/a4f96afdc147bfee3e0f195b7bcf3dfa882ad511/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala#L993-L1000
Also cc @gengliangwang
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] cloud-fan commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r897700182
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1015,6 +1014,11 @@ case class Cast(
} catch {
case _: NumberFormatException => null
}
+ case x: DayTimeIntervalType =>
+ buildCast[Long](_, dt => changePrecision(dayTimeIntervalToDecimal(dt, x.endField), target))
+ case x: YearMonthIntervalType =>
+ buildCast[Int](_, ym =>
+ changePrecision(Decimal(yearMonthIntervalToInt(ym, x.startField, x.endField)), target))
Review Comment:
shall we consider ansi mode here? what if the decimal precision is too small, e.g. `decimal(1, 0)`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk commented on pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk commented on PR #36857:
URL: https://github.com/apache/spark/pull/36857#issuecomment-1156166342
@cloud-fan @gengliangwang @HyukjinKwon @srielau Could you review this PR, please.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] cloud-fan commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898130592
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1015,6 +1014,11 @@ case class Cast(
} catch {
case _: NumberFormatException => null
}
+ case x: DayTimeIntervalType =>
+ buildCast[Long](_, dt => changePrecision(dayTimeIntervalToDecimal(dt, x.endField), target))
Review Comment:
@MaxGekk as you said I think it makes sense to always go with ANSI behavior in new features. Does `changePrecision` take a bool flag like `nullOnOverflow`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r897724324
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1015,6 +1014,11 @@ case class Cast(
} catch {
case _: NumberFormatException => null
}
+ case x: DayTimeIntervalType =>
+ buildCast[Long](_, dt => changePrecision(dayTimeIntervalToDecimal(dt, x.endField), target))
+ case x: YearMonthIntervalType =>
+ buildCast[Int](_, ym =>
+ changePrecision(Decimal(yearMonthIntervalToInt(ym, x.startField, x.endField)), target))
Review Comment:
As we did before, we don't consider non-ANSI mode for ANSI intervals.
> what if the decimal precision is too small, e.g. decimal(1, 0)
I think `changePrecision()` should output an error.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] MaxGekk commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
MaxGekk commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898179945
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -1015,6 +1014,11 @@ case class Cast(
} catch {
case _: NumberFormatException => null
}
+ case x: DayTimeIntervalType =>
+ buildCast[Long](_, dt => changePrecision(dayTimeIntervalToDecimal(dt, x.endField), target))
+ case x: YearMonthIntervalType =>
+ buildCast[Int](_, ym =>
+ changePrecision(Decimal(yearMonthIntervalToInt(ym, x.startField, x.endField)), target))
Review Comment:
Though, changing of scale/precision while keeping the same value is different thing from changing the value and get overflow.
In any case, I am not going to change decimal logic (errors) here in the PR.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
[GitHub] [spark] cloud-fan commented on a diff in pull request #36857: [SPARK-39470][SQL] Support cast of ANSI intervals to decimals
Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #36857:
URL: https://github.com/apache/spark/pull/36857#discussion_r898618909
##########
sql/core/src/test/resources/sql-tests/inputs/cast.sql:
##########
@@ -116,3 +116,11 @@ 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(1, 0));
Review Comment:
how about `decimal(4, 2)`? will we fail or truncate?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org