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

Support hour minute second for non-UTC time zone #10024

Merged
merged 3 commits into from
Dec 13, 2023
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
17 changes: 11 additions & 6 deletions integration_tests/src/main/python/date_time_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,25 @@ def test_datediff(data_gen):
'datediff(a, date(null))',
'datediff(a, \'2016-03-02\')'))

@allow_non_gpu(*non_utc_allow)
hms_fallback = ['ProjectExec'] if not is_supported_time_zone() else []

@allow_non_gpu(*hms_fallback)
def test_hour():
assert_gpu_and_cpu_are_equal_collect(
lambda spark : unary_op_df(spark, timestamp_gen).selectExpr('hour(a)'))
lambda spark : unary_op_df(spark, timestamp_gen).selectExpr('hour(a)'),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: we can consider merging these two cases together.

non_utc_allow = ['ProjectExec'] if is_supported_time_zone() else []

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done for all three cases.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The one downside of this change is that we don't truly verify that we fell back to the CPU for the operator we care about. Getting the right answer is probably good enough though.

conf = {'spark.rapids.sql.nonUTC.enabled': True})

@allow_non_gpu(*non_utc_allow)
@allow_non_gpu(*hms_fallback)
def test_minute():
assert_gpu_and_cpu_are_equal_collect(
lambda spark : unary_op_df(spark, timestamp_gen).selectExpr('minute(a)'))
lambda spark : unary_op_df(spark, timestamp_gen).selectExpr('minute(a)'),
conf = {'spark.rapids.sql.nonUTC.enabled': True})

@allow_non_gpu(*non_utc_allow)
@allow_non_gpu(*hms_fallback)
def test_second():
assert_gpu_and_cpu_are_equal_collect(
lambda spark : unary_op_df(spark, timestamp_gen).selectExpr('second(a)'))
lambda spark : unary_op_df(spark, timestamp_gen).selectExpr('second(a)'),
conf = {'spark.rapids.sql.nonUTC.enabled': True})

def test_quarter():
assert_gpu_and_cpu_are_equal_collect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1728,26 +1728,26 @@ object GpuOverrides extends Logging {
ExprChecks.unaryProject(TypeSig.INT, TypeSig.INT,
TypeSig.TIMESTAMP, TypeSig.TIMESTAMP),
(hour, conf, p, r) => new UnaryExprMeta[Hour](hour, conf, p, r) {

override def convertToGpu(expr: Expression): GpuExpression = GpuHour(expr)
override def isTimeZoneSupported = true
override def convertToGpu(expr: Expression): GpuExpression = GpuHour(expr, hour.timeZoneId)
}),
expr[Minute](
"Returns the minute component of the string/timestamp",
ExprChecks.unaryProject(TypeSig.INT, TypeSig.INT,
TypeSig.TIMESTAMP, TypeSig.TIMESTAMP),
(minute, conf, p, r) => new UnaryExprMeta[Minute](minute, conf, p, r) {

override def isTimeZoneSupported = true
override def convertToGpu(expr: Expression): GpuExpression =
GpuMinute(expr)
GpuMinute(expr, minute.timeZoneId)
}),
expr[Second](
"Returns the second component of the string/timestamp",
ExprChecks.unaryProject(TypeSig.INT, TypeSig.INT,
TypeSig.TIMESTAMP, TypeSig.TIMESTAMP),
(second, conf, p, r) => new UnaryExprMeta[Second](second, conf, p, r) {

override def isTimeZoneSupported = true
override def convertToGpu(expr: Expression): GpuExpression =
GpuSecond(expr)
GpuSecond(expr, second.timeZoneId)
}),
expr[WeekDay](
"Returns the day of the week (0 = Monday...6=Sunday)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ case class GpuMinute(child: Expression, timeZoneId: Option[String] = None)
copy(timeZoneId = Option(timeZoneId))

override protected def doColumnar(input: GpuColumnVector): ColumnVector =
input.getBase.minute()
if (GpuOverrides.isUTCTimezone(zoneId)) {
input.getBase.minute()
} else {
// Non-UTC time zone
withResource(GpuTimeZoneDB.fromUtcTimestampToTimestamp(input.getBase, zoneId)) {
shifted => shifted.minute()
}
}
}

case class GpuSecond(child: Expression, timeZoneId: Option[String] = None)
Expand All @@ -101,7 +108,14 @@ case class GpuSecond(child: Expression, timeZoneId: Option[String] = None)
copy(timeZoneId = Option(timeZoneId))

override protected def doColumnar(input: GpuColumnVector): ColumnVector =
input.getBase.second()
if (GpuOverrides.isUTCTimezone(zoneId)) {
input.getBase.second()
} else {
// Non-UTC time zone
withResource(GpuTimeZoneDB.fromUtcTimestampToTimestamp(input.getBase, zoneId)) {
shifted => shifted.second()
}
}
}

case class GpuHour(child: Expression, timeZoneId: Option[String] = None)
Expand All @@ -111,7 +125,14 @@ case class GpuHour(child: Expression, timeZoneId: Option[String] = None)
copy(timeZoneId = Option(timeZoneId))

override protected def doColumnar(input: GpuColumnVector): ColumnVector =
input.getBase.hour()
if (GpuOverrides.isUTCTimezone(zoneId)) {
input.getBase.hour()
} else {
// Non-UTC time zone
withResource(GpuTimeZoneDB.fromUtcTimestampToTimestamp(input.getBase, zoneId)) {
shifted => shifted.hour()
}
}
}

case class GpuYear(child: Expression) extends GpuDateUnaryExpression {
Expand Down