Skip to content
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-47977] DateTimeUtils.timestampDiff and DateTimeUtils.timestampAdd should not throw INTERNAL_ERROR exception #46210

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ object DateTimeUtils extends SparkDateTimeUtils {
}
} catch {
case _: scala.MatchError =>
throw SparkException.internalError(s"Got the unexpected unit '$unit'.")
throw QueryExecutionErrors.invalidDatetimeUnitError("timestampAdd", unit)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: TIMESTAMPADD

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

case _: ArithmeticException | _: DateTimeException =>
throw QueryExecutionErrors.timestampAddOverflowError(micros, quantity, unit)
case e: Throwable =>
Expand Down Expand Up @@ -736,7 +736,7 @@ object DateTimeUtils extends SparkDateTimeUtils {
val endLocalTs = getLocalDateTime(endTs, zoneId)
timestampDiffMap(unitInUpperCase)(startLocalTs, endLocalTs)
} else {
throw SparkException.internalError(s"Got the unexpected unit '$unit'.")
throw QueryExecutionErrors.invalidDatetimeUnitError("timestampDiff", unit)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: TIMESTAMPDIFF

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2773,4 +2773,15 @@ private[sql] object QueryExecutionErrors extends QueryErrorsBase with ExecutionE
)
)
}

def invalidDatetimeUnitError(
functionName: String,
invalidValue: String): Throwable = {
new SparkIllegalArgumentException(
errorClass = "INVALID_PARAMETER_VALUE.DATETIME_UNIT",
messageParameters = Map(
"functionName" -> toSQLId(functionName),
"parameter" -> toSQLId("unit"),
"invalidValue" -> s"'$invalidValue'"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import java.util.concurrent.TimeUnit
import org.scalatest.matchers.must.Matchers
import org.scalatest.matchers.should.Matchers._

import org.apache.spark.{SparkException, SparkFunSuite, SparkIllegalArgumentException}
import org.apache.spark.{SparkFunSuite, SparkIllegalArgumentException}
import org.apache.spark.sql.catalyst.plans.SQLHelper
import org.apache.spark.sql.catalyst.util.DateTimeConstants._
import org.apache.spark.sql.catalyst.util.DateTimeTestUtils._
Expand Down Expand Up @@ -1040,11 +1040,14 @@ class DateTimeUtilsSuite extends SparkFunSuite with Matchers with SQLHelper {
}

checkError(
exception = intercept[SparkException] {
exception = intercept[SparkIllegalArgumentException] {
timestampAdd("SECS", 1, date(1969, 1, 1, 0, 0, 0, 1, getZoneId("UTC")), getZoneId("UTC"))
},
errorClass = "INTERNAL_ERROR",
parameters = Map("message" -> "Got the unexpected unit 'SECS'."))
errorClass = "INVALID_PARAMETER_VALUE.DATETIME_UNIT",
parameters = Map(
"functionName" -> "`timestampAdd`",
"parameter" -> "`unit`",
"invalidValue" -> "'SECS'"))
}

test("SPARK-38284: difference between two timestamps in units") {
Expand Down Expand Up @@ -1092,14 +1095,17 @@ class DateTimeUtilsSuite extends SparkFunSuite with Matchers with SQLHelper {
}

checkError(
exception = intercept[SparkException] {
exception = intercept[SparkIllegalArgumentException] {
timestampDiff(
"SECS",
date(1969, 1, 1, 0, 0, 0, 1, getZoneId("UTC")),
date(2022, 1, 1, 0, 0, 0, 1, getZoneId("UTC")),
getZoneId("UTC"))
},
errorClass = "INTERNAL_ERROR",
parameters = Map("message" -> "Got the unexpected unit 'SECS'."))
errorClass = "INVALID_PARAMETER_VALUE.DATETIME_UNIT",
parameters =
Map("functionName" -> "`timestampDiff`",
"parameter" -> "`unit`",
"invalidValue" -> "'SECS'"))
}
}