Skip to content

Commit

Permalink
[SPARK-49637][SQL] Changed error message for INVALID_FRACTION_OF_SECOND
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

In this PR, the error message for INVALID_FRACTION_OF_SECOND is changed, such that it no longer suggests turning off ANSI flag. Now, the error suggests using try_ variants of functions for making timestamps. This PR will is done in conjunction with #48624, where the try_ variants of these functions are implemented.

### Why are the changes needed?

These changes are needed as part of the effort to move to ANSI by default.

### Does this PR introduce _any_ user-facing change?

Yes, the error message is changed.

### How was this patch tested?

Tests not needed.

### Was this patch authored or co-authored using generative AI tooling?

No

Closes #48656 from markonik-db/SPARK-49637-INVALID-FRACTION-OF-SECOND.

Authored-by: Marko Nikacevic <[email protected]>
Signed-off-by: Max Gekk <[email protected]>
  • Loading branch information
markonik-db authored and MaxGekk committed Oct 29, 2024
1 parent 9968a32 commit 56593f4
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 10 deletions.
3 changes: 2 additions & 1 deletion common/utils/src/main/resources/error/error-conditions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2413,7 +2413,8 @@
},
"INVALID_FRACTION_OF_SECOND" : {
"message" : [
"The fraction of sec must be zero. Valid range is [0, 60]. If necessary set <ansiConfig> to \"false\" to bypass this error."
"Valid range for seconds is [0, 60] (inclusive), but the provided value is <secAndMicros>. To avoid this error, use `try_make_timestamp`, which returns NULL on error.",
"If you do not want to use the session default timestamp version of this function, use `try_make_timestamp_ntz` or `try_make_timestamp_ltz`."
],
"sqlState" : "22023"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2808,7 +2808,7 @@ case class MakeTimestamp(
// This case of sec = 60 and nanos = 0 is supported for compatibility with PostgreSQL
LocalDateTime.of(year, month, day, hour, min, 0, 0).plusMinutes(1)
} else {
throw QueryExecutionErrors.invalidFractionOfSecondError()
throw QueryExecutionErrors.invalidFractionOfSecondError(secAndMicros)
}
} else {
LocalDateTime.of(year, month, day, hour, min, seconds, nanos)
Expand Down Expand Up @@ -2879,7 +2879,7 @@ case class MakeTimestamp(
ldt = java.time.LocalDateTime.of(
$year, $month, $day, $hour, $min, 0, 0).plusMinutes(1);
} else {
throw QueryExecutionErrors.invalidFractionOfSecondError();
throw QueryExecutionErrors.invalidFractionOfSecondError($secAndNanos);
}
} else {
ldt = java.time.LocalDateTime.of($year, $month, $day, $hour, $min, seconds, nanos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,11 @@ private[sql] object QueryExecutionErrors extends QueryErrorsBase with ExecutionE
summary = "")
}

def invalidFractionOfSecondError(): DateTimeException = {
def invalidFractionOfSecondError(secAndMicros: Decimal): DateTimeException = {
new SparkDateTimeException(
errorClass = "INVALID_FRACTION_OF_SECOND",
messageParameters = Map(
"ansiConfig" -> toSQLConf(SQLConf.ANSI_ENABLED.key)
"secAndMicros" -> s"$secAndMicros"
),
context = Array.empty,
summary = "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
Literal(23), Literal(59), Literal(Decimal(BigDecimal(60.0), 16, 6)))
if (ansi) {
checkExceptionInExpression[DateTimeException](makeTimestampExpr.copy(sec = Literal(
Decimal(BigDecimal(60.5), 16, 6))), EmptyRow, "The fraction of sec must be zero")
Decimal(BigDecimal(60.5), 16, 6))), EmptyRow, "Valid range for seconds is [0, 60]")
} else {
checkEvaluation(makeTimestampExpr, expectedAnswer("2019-07-01 00:00:00"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ org.apache.spark.SparkDateTimeException
"errorClass" : "INVALID_FRACTION_OF_SECOND",
"sqlState" : "22023",
"messageParameters" : {
"ansiConfig" : "\"spark.sql.ansi.enabled\""
"secAndMicros" : "60.007000"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ org.apache.spark.SparkDateTimeException
"errorClass" : "INVALID_FRACTION_OF_SECOND",
"sqlState" : "22023",
"messageParameters" : {
"ansiConfig" : "\"spark.sql.ansi.enabled\""
"secAndMicros" : "60.007000"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ class QueryExecutionAnsiErrorsSuite extends QueryTest
test("INVALID_FRACTION_OF_SECOND: in the function make_timestamp") {
checkError(
exception = intercept[SparkDateTimeException] {
sql("select make_timestamp(2012, 11, 30, 9, 19, 60.66666666)").collect()
sql("select make_timestamp(2012, 11, 30, 9, 19, 60.1)").collect()
},
condition = "INVALID_FRACTION_OF_SECOND",
sqlState = "22023",
parameters = Map("ansiConfig" -> ansiConf))
parameters = Map(
"secAndMicros" -> "60.100000"
))
}

test("NUMERIC_VALUE_OUT_OF_RANGE: cast string to decimal") {
Expand Down

0 comments on commit 56593f4

Please sign in to comment.