Skip to content

Commit 6ef9a9d

Browse files
uros-dbcloud-fan
authored andcommitted
[SPARK-51555][SQL][FOLLOWUP] Fix the error message for invalid unit in the time_diff() function
### What changes were proposed in this pull request? Following up on the original PR that introduces the `time_diff` function (apache#51715), this PR fixes the error message to include the correct SQL function name. ### Why are the changes needed? Fix error messages for the `time_diff` function. ### Does this PR introduce _any_ user-facing change? Changes the error message for the new SQL function. ### How was this patch tested? Updated relevant scala tests and golden files. ### Was this patch authored or co-authored using generative AI tooling? No. Closes apache#51777 from uros-db/time_diff_FOLLOWUP. Authored-by: Uros Bojanic <[email protected]> Signed-off-by: Wenchen Fan <[email protected]>
1 parent 09d2765 commit 6ef9a9d

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ object DateTimeUtils extends SparkDateTimeUtils {
719719
// Helper method to get the number of nanoseconds per the given time unit, used for calculating
720720
// the difference between two time values (timediff function). Supported units are: MICROSECOND,
721721
// MILLISECOND, SECOND, MINUTE, HOUR.
722-
private def getNanosPerTimeUnit(unit: UTF8String): Long = {
722+
private def getNanosPerTimeUnit(unit: UTF8String, functionName: String): Long = {
723723
val unitStr = unit.toString
724724
unitStr.toUpperCase(Locale.ROOT) match {
725725
case "MICROSECOND" =>
@@ -733,7 +733,7 @@ object DateTimeUtils extends SparkDateTimeUtils {
733733
case "HOUR" =>
734734
NANOS_PER_SECOND * SECONDS_PER_MINUTE * MINUTES_PER_HOUR
735735
case _ =>
736-
throw QueryExecutionErrors.invalidTimeUnitError("timediff", unitStr)
736+
throw QueryExecutionErrors.invalidTimeUnitError(functionName, unitStr)
737737
}
738738
}
739739

@@ -750,7 +750,7 @@ object DateTimeUtils extends SparkDateTimeUtils {
750750
* @return The time span between two time values, in the units specified.
751751
*/
752752
def timeDiff(unit: UTF8String, startNanos: Long, endNanos: Long): Long = {
753-
(endNanos - startNanos) / getNanosPerTimeUnit(unit)
753+
(endNanos - startNanos) / getNanosPerTimeUnit(unit, "time_diff")
754754
}
755755

756756
/**

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/TimeExpressionsSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ class TimeExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
423423
TimeDiff(Literal(unit), startTimeLit, endTimeLit),
424424
condition = "INVALID_PARAMETER_VALUE.TIME_UNIT",
425425
parameters = Map(
426-
"functionName" -> "`timediff`",
426+
"functionName" -> "`time_diff`",
427427
"parameter" -> "`unit`",
428428
"invalidValue" -> s"'$unit'"
429429
)

sql/core/src/test/resources/sql-tests/results/time.sql.out

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,7 +1623,7 @@ org.apache.spark.SparkIllegalArgumentException
16231623
"errorClass" : "INVALID_PARAMETER_VALUE.TIME_UNIT",
16241624
"sqlState" : "22023",
16251625
"messageParameters" : {
1626-
"functionName" : "`timediff`",
1626+
"functionName" : "`time_diff`",
16271627
"invalidValue" : "''",
16281628
"parameter" : "`unit`"
16291629
}
@@ -1640,7 +1640,7 @@ org.apache.spark.SparkIllegalArgumentException
16401640
"errorClass" : "INVALID_PARAMETER_VALUE.TIME_UNIT",
16411641
"sqlState" : "22023",
16421642
"messageParameters" : {
1643-
"functionName" : "`timediff`",
1643+
"functionName" : "`time_diff`",
16441644
"invalidValue" : "' '",
16451645
"parameter" : "`unit`"
16461646
}
@@ -1657,7 +1657,7 @@ org.apache.spark.SparkIllegalArgumentException
16571657
"errorClass" : "INVALID_PARAMETER_VALUE.TIME_UNIT",
16581658
"sqlState" : "22023",
16591659
"messageParameters" : {
1660-
"functionName" : "`timediff`",
1660+
"functionName" : "`time_diff`",
16611661
"invalidValue" : "'MS'",
16621662
"parameter" : "`unit`"
16631663
}
@@ -1674,7 +1674,7 @@ org.apache.spark.SparkIllegalArgumentException
16741674
"errorClass" : "INVALID_PARAMETER_VALUE.TIME_UNIT",
16751675
"sqlState" : "22023",
16761676
"messageParameters" : {
1677-
"functionName" : "`timediff`",
1677+
"functionName" : "`time_diff`",
16781678
"invalidValue" : "'DAY'",
16791679
"parameter" : "`unit`"
16801680
}
@@ -1691,7 +1691,7 @@ org.apache.spark.SparkIllegalArgumentException
16911691
"errorClass" : "INVALID_PARAMETER_VALUE.TIME_UNIT",
16921692
"sqlState" : "22023",
16931693
"messageParameters" : {
1694-
"functionName" : "`timediff`",
1694+
"functionName" : "`time_diff`",
16951695
"invalidValue" : "'WEEK'",
16961696
"parameter" : "`unit`"
16971697
}
@@ -1708,7 +1708,7 @@ org.apache.spark.SparkIllegalArgumentException
17081708
"errorClass" : "INVALID_PARAMETER_VALUE.TIME_UNIT",
17091709
"sqlState" : "22023",
17101710
"messageParameters" : {
1711-
"functionName" : "`timediff`",
1711+
"functionName" : "`time_diff`",
17121712
"invalidValue" : "'ABCD'",
17131713
"parameter" : "`unit`"
17141714
}
@@ -1725,7 +1725,7 @@ org.apache.spark.SparkIllegalArgumentException
17251725
"errorClass" : "INVALID_PARAMETER_VALUE.TIME_UNIT",
17261726
"sqlState" : "22023",
17271727
"messageParameters" : {
1728-
"functionName" : "`timediff`",
1728+
"functionName" : "`time_diff`",
17291729
"invalidValue" : "'QUARTER'",
17301730
"parameter" : "`unit`"
17311731
}
@@ -1742,7 +1742,7 @@ org.apache.spark.SparkIllegalArgumentException
17421742
"errorClass" : "INVALID_PARAMETER_VALUE.TIME_UNIT",
17431743
"sqlState" : "22023",
17441744
"messageParameters" : {
1745-
"functionName" : "`timediff`",
1745+
"functionName" : "`time_diff`",
17461746
"invalidValue" : "'INVALID'",
17471747
"parameter" : "`unit`"
17481748
}
@@ -1759,7 +1759,7 @@ org.apache.spark.SparkIllegalArgumentException
17591759
"errorClass" : "INVALID_PARAMETER_VALUE.TIME_UNIT",
17601760
"sqlState" : "22023",
17611761
"messageParameters" : {
1762-
"functionName" : "`timediff`",
1762+
"functionName" : "`time_diff`",
17631763
"invalidValue" : "'INVALID_UNIT'",
17641764
"parameter" : "`unit`"
17651765
}

0 commit comments

Comments
 (0)