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

CAST string to temporal type now calls isTimestamp #1718

Merged
merged 8 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
81 changes: 67 additions & 14 deletions sql-plugin/src/main/scala/com/nvidia/spark/rapids/GpuCast.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2020, NVIDIA CORPORATION.
* Copyright (c) 2019-2021, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -637,10 +637,16 @@ case class GpuCast(
regex: String,
cudfFormat: String): ColumnVector = {

withResource(Scalar.fromNull(DType.TIMESTAMP_DAYS)) { nullScalar =>
withResource(input.matchesRe(regex)) { isMatch =>
withResource(input.asTimestampDays(cudfFormat)) { asDays =>
isMatch.ifElse(asDays, nullScalar)
val isValidDate = withResource(input.matchesRe(regex)) { isMatch =>
withResource(input.isTimestamp(cudfFormat)) { isTimestamp =>
isMatch.and(isTimestamp)
}
}

withResource(isValidDate) { isValidDate =>
withResource(input.asTimestampDays(cudfFormat)) { asDays =>
withResource(Scalar.fromNull(DType.TIMESTAMP_DAYS)) { nullScalar =>
isValidDate.ifElse(asDays, nullScalar)
}
}
}
Expand All @@ -653,10 +659,16 @@ case class GpuCast(
cudfFormat: String,
orElse: ColumnVector): ColumnVector = {

withResource(input.matchesRe(regex)) { isMatch =>
val isValidDate = withResource(input.matchesRe(regex)) { isMatch =>
withResource(input.isTimestamp(cudfFormat)) { isTimestamp =>
isMatch.and(isTimestamp)
}
}

withResource(isValidDate) { isValidDate =>
withResource(input.asTimestampDays(cudfFormat)) { asDays =>
withResource(orElse) { orElse =>
isMatch.ifElse(asDays, orElse)
isValidDate.ifElse(asDays, orElse)
}
}
}
Expand Down Expand Up @@ -721,10 +733,16 @@ case class GpuCast(
regex: String,
cudfFormat: String): ColumnVector = {

withResource(Scalar.fromNull(DType.TIMESTAMP_MICROSECONDS)) { nullScalar =>
withResource(input.matchesRe(regex)) { isMatch =>
val isValidTimestamp = withResource(input.matchesRe(regex)) { isMatch =>
withResource(input.isTimestamp(cudfFormat)) { isTimestamp =>
isMatch.and(isTimestamp)
}
}

withResource(isValidTimestamp) { isValidTimestamp =>
withResource(Scalar.fromNull(DType.TIMESTAMP_MICROSECONDS)) { nullScalar =>
withResource(input.asTimestampMicroseconds(cudfFormat)) { asDays =>
isMatch.ifElse(asDays, nullScalar)
isValidTimestamp.ifElse(asDays, nullScalar)
}
}
}
Expand All @@ -737,10 +755,46 @@ case class GpuCast(
cudfFormat: String,
orElse: ColumnVector): ColumnVector = {

withResource(input.matchesRe(regex)) { isMatch =>
val isValidTimestamp = withResource(input.matchesRe(regex)) { isMatch =>
withResource(input.isTimestamp(cudfFormat)) { isTimestamp =>
isMatch.and(isTimestamp)
}
}
withResource(isValidTimestamp) { isValidTimestamp =>
withResource(input.asTimestampMicroseconds(cudfFormat)) { asDays =>
withResource(orElse) { orElse =>
isMatch.ifElse(asDays, orElse)
isValidTimestamp.ifElse(asDays, orElse)
}
}
}
}

/** This method does not close the `input` ColumnVector. */
def convertTimestampFullOr(
input: ColumnVector,
orElse: ColumnVector): ColumnVector = {

val cudfFormat1 = "%Y-%m-%d %H:%M:%S.%f"
val cudfFormat2 = "%Y-%m-%dT%H:%M:%S.%f"

// valid dates must match the regex and either of the cuDF formats
val isCudfMatch = withResource(input.isTimestamp(cudfFormat1)) { isTimestamp1 =>
withResource(input.isTimestamp(cudfFormat2)) { isTimestamp2 =>
isTimestamp1.or(isTimestamp2)
}
}
val isValidTimestamp = withResource(isCudfMatch) { isCudfMatch =>
withResource(input.matchesRe(TIMESTAMP_REGEX_FULL)) { isRegexMatch =>
isCudfMatch.and(isRegexMatch)
}
}

// we only need to parse with one of the cuDF formats because the parsing code ignores
// the ' ' or 'T' between the date and time components
withResource(isValidTimestamp) { isValidTimestamp =>
withResource(input.asTimestampMicroseconds(cudfFormat1)) { asDays =>
withResource(orElse) { orElse =>
isValidTimestamp.ifElse(asDays, orElse)
}
}
}
Expand Down Expand Up @@ -776,10 +830,9 @@ case class GpuCast(
}

withResource(sanitizedInput) { sanitizedInput =>

// convert dates that are in valid timestamp formats
val converted =
convertTimestampOr(sanitizedInput, TIMESTAMP_REGEX_FULL, "%Y-%m-%dT%H:%M:%SZ%f",
convertTimestampFullOr(sanitizedInput,
convertTimestampOr(sanitizedInput, TIMESTAMP_REGEX_YYYY_MM_DD, "%Y-%m-%d",
convertTimestampOr(sanitizedInput, TIMESTAMP_REGEX_YYYY_MM, "%Y-%m",
convertTimestampOrNull(sanitizedInput, TIMESTAMP_REGEX_YYYY, "%Y"))))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2020, NVIDIA CORPORATION.
* Copyright (c) 2019-2021, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -934,6 +934,7 @@ object CastOpSuite {
"2010-01-6T 12:34:56.000111Z",
"2010-01-6 T 12:34:56.000111Z",
"2010-01-6 T12:34:56.000111Z",
"2030-11-11 12:02:03.012345Z TRAILING TEXT",
"2010-01-6 ",
"2010-01-6 T",
"2010-01-6 T\n",
Expand All @@ -943,6 +944,12 @@ object CastOpSuite {
"2018-1random_text",
jlowe marked this conversation as resolved.
Show resolved Hide resolved
"2018-11-08random_text",
"2018-11-9random_text",
// date component out of range
"2020-13-01",
"2020-12-32",
"2020-02-30",
"2030-00-11 12:02:03.012345Z",
"2030-00-11T12:02:03.012345Z",
// `yyyy-[m]m-[d]dT*` in Spark 3.1+ these no longer work for AnsiCast, but did before
"2010-1-01T!@#$%",
"2010-1-02T,",
Expand Down