-
Notifications
You must be signed in to change notification settings - Fork 28.2k
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
[SPARK-36920][SQL] Support ANSI intervals by ABS()
#34169
Conversation
Kubernetes integration test starting |
Kubernetes integration test status failure |
ABS()
ABS()
@beliefer @AngersZhuuuu @Peng-Lei @cloud-fan Could you review this PR, please. |
Test build #143814 has finished for PR 34169 at commit
|
Merged to master. |
@@ -160,11 +162,15 @@ case class Abs(child: Expression, failOnError: Boolean = SQLConf.get.ansiEnabled | |||
|
|||
def this(child: Expression) = this(child, SQLConf.get.ansiEnabled) | |||
|
|||
override def inputTypes: Seq[AbstractDataType] = Seq(NumericType) | |||
override def inputTypes: Seq[AbstractDataType] = Seq(TypeCollection.NumericAndInterval) |
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 shouldn't use TypeCollection.NumericAndInterval
, as it includes the legacy interval. We will hit runtime exception if we use legacy interval as input, but analysis exception is preferred.
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 Thanks for the comment. Here is the fix #34183
…ANSI intervals ### What changes were proposed in this pull request? Change allowed input types of `Abs()` from: ``` NumericType + CalendarIntervalType + YearMonthIntervalType + DayTimeIntervalType ``` to ``` NumericType + YearMonthIntervalType + DayTimeIntervalType ``` ### Why are the changes needed? The changes make the error message more clear. Before changes: ```sql spark-sql> set spark.sql.legacy.interval.enabled=true; spark.sql.legacy.interval.enabled true spark-sql> select abs(interval -10 days -20 minutes); 21/10/05 09:11:30 ERROR SparkSQLDriver: Failed in [select abs(interval -10 days -20 minutes)] java.lang.ClassCastException: org.apache.spark.sql.types.CalendarIntervalType$ cannot be cast to org.apache.spark.sql.types.NumericType at org.apache.spark.sql.catalyst.util.TypeUtils$.getNumeric(TypeUtils.scala:77) at org.apache.spark.sql.catalyst.expressions.Abs.numeric$lzycompute(arithmetic.scala:172) at org.apache.spark.sql.catalyst.expressions.Abs.numeric(arithmetic.scala:169) ``` After: ```sql spark.sql.legacy.interval.enabled true spark-sql> select abs(interval -10 days -20 minutes); Error in query: cannot resolve 'abs(INTERVAL '-10 days -20 minutes')' due to data type mismatch: argument 1 requires (numeric or interval day to second or interval year to month) type, however, 'INTERVAL '-10 days -20 minutes'' is of interval type.; line 1 pos 7; 'Project [unresolvedalias(abs(-10 days -20 minutes, false), None)] +- OneRowRelation ``` ### Does this PR introduce _any_ user-facing change? No, because the original changes of #34169 haven't released yet. ### How was this patch tested? Manually checked in the command line, see examples above. Closes #34183 from MaxGekk/fix-abs-input-types. Authored-by: Max Gekk <max.gekk@gmail.com> Signed-off-by: Max Gekk <max.gekk@gmail.com>
…ANSI intervals ### What changes were proposed in this pull request? Change allowed input types of `Abs()` from: ``` NumericType + CalendarIntervalType + YearMonthIntervalType + DayTimeIntervalType ``` to ``` NumericType + YearMonthIntervalType + DayTimeIntervalType ``` ### Why are the changes needed? The changes make the error message more clear. Before changes: ```sql spark-sql> set spark.sql.legacy.interval.enabled=true; spark.sql.legacy.interval.enabled true spark-sql> select abs(interval -10 days -20 minutes); 21/10/05 09:11:30 ERROR SparkSQLDriver: Failed in [select abs(interval -10 days -20 minutes)] java.lang.ClassCastException: org.apache.spark.sql.types.CalendarIntervalType$ cannot be cast to org.apache.spark.sql.types.NumericType at org.apache.spark.sql.catalyst.util.TypeUtils$.getNumeric(TypeUtils.scala:77) at org.apache.spark.sql.catalyst.expressions.Abs.numeric$lzycompute(arithmetic.scala:172) at org.apache.spark.sql.catalyst.expressions.Abs.numeric(arithmetic.scala:169) ``` After: ```sql spark.sql.legacy.interval.enabled true spark-sql> select abs(interval -10 days -20 minutes); Error in query: cannot resolve 'abs(INTERVAL '-10 days -20 minutes')' due to data type mismatch: argument 1 requires (numeric or interval day to second or interval year to month) type, however, 'INTERVAL '-10 days -20 minutes'' is of interval type.; line 1 pos 7; 'Project [unresolvedalias(abs(-10 days -20 minutes, false), None)] +- OneRowRelation ``` ### Does this PR introduce _any_ user-facing change? No, because the original changes of apache/spark#34169 haven't released yet. ### How was this patch tested? Manually checked in the command line, see examples above. Closes #34183 from MaxGekk/fix-abs-input-types. Authored-by: Max Gekk <max.gekk@gmail.com> Signed-off-by: Max Gekk <max.gekk@gmail.com>
What changes were proposed in this pull request?
In the PR, I propose to handle ANSI interval types by the
Abs
expression, and theabs()
function as a consequence of that:ABS()
returns the same input value,Int.MinValue
months for year-month interval andLong.MinValue
microseconds for day-time interval),ABS()
throws the arithmetic overflow exception.ABS()
negate its input and returns a positive interval.For example:
Why are the changes needed?
To improve user experience with Spark SQL.
Does this PR introduce any user-facing change?
No, this PR just extends
ABS()
by supporting new types.How was this patch tested?
By running new tests: