Skip to content

Commit b9dbf8b

Browse files
senthhMaxGekk
authored andcommitted
[SPARK-51419][SQL][FOLLOWUP] Making hour function to accept any precision of TIME type
### What changes were proposed in this pull request? This is followup PR of [SPARK-51419 ](#50355) ### Why are the changes needed? This Followup PR allows any precision in the range of [0,6] for the hour function ### Does this PR introduce _any_ user-facing change? Yes. This changes allows User to execute below query ``` spark.sql("select hour(cast('12:00:01.123' as time(3)))").show(false) ``` ### How was this patch tested? We tested by running sample query as below ``` spark.sql("select hour(cast('12:00:01.123' as time(3)))").show(false) ``` Output: ``` +-----------------------------------+ |hour(CAST(12:00:01.123 AS TIME(3)))| +-----------------------------------+ |12 | +-----------------------------------+ ``` ### Was this patch authored or co-authored using generative AI tooling? No Closes #50554 from senthh/SPARK-51419_followup. Authored-by: senthh <[email protected]> Signed-off-by: Max Gekk <[email protected]>
1 parent adc42b4 commit b9dbf8b

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/timeExpressions.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ case class HoursOfTime(child: Expression)
251251
Seq(child.dataType)
252252
)
253253

254-
override def inputTypes: Seq[AbstractDataType] = Seq(TimeType())
254+
override def inputTypes: Seq[AbstractDataType] =
255+
Seq(TypeCollection(TimeType.MIN_PRECISION to TimeType.MAX_PRECISION map TimeType: _*))
255256

256257
override def children: Seq[Expression] = Seq(child)
257258

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ class TimeExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
7575
assert(builtExprForTime.isInstanceOf[HoursOfTime])
7676
assert(builtExprForTime.asInstanceOf[HoursOfTime].child eq timeExpr)
7777

78+
assert(builtExprForTime.checkInputDataTypes().isSuccess)
79+
80+
// test TIME-typed child should build HoursOfTime for all allowed custom precision values
81+
(TimeType.MIN_PRECISION to TimeType.MICROS_PRECISION).foreach { precision =>
82+
val timeExpr = Literal(localTime(12, 58, 59), TimeType(precision))
83+
val builtExpr = HourExpressionBuilder.build("hour", Seq(timeExpr))
84+
85+
assert(builtExpr.isInstanceOf[HoursOfTime])
86+
assert(builtExpr.asInstanceOf[HoursOfTime].child eq timeExpr)
87+
assert(builtExpr.checkInputDataTypes().isSuccess)
88+
}
89+
7890
// test non TIME-typed child should build hour
7991
val tsExpr = Literal("2007-09-03 10:45:23")
8092
val builtExprForTs = HourExpressionBuilder.build("hour", Seq(tsExpr))

0 commit comments

Comments
 (0)