Skip to content

Commit adc42b4

Browse files
the-sakthiMaxGekk
authored andcommitted
[SPARK-51420][SQL][FOLLOWUP] Support all valid TIME precisions in the minute function
### What changes were proposed in this pull request? - Followup to the original PR: #50296 - Extend the minute(...) function (MinutesOfTime) to handle TIME types of any precision from 0 to 6. - Add tests verifying that minute(...) works for all valid TIME precisions. ### Why are the changes needed? - Previously, minute(...) did not consistently support TIME type inputs with arbitrary precision. - Users need the minute function to handle TIME(0) through TIME(6). ### Does this PR introduce _any_ user-facing change? - Yes. Users can now call minute(...) on TIME(p) columns or literals with any valid precision. ### How was this patch tested? By running new tests: ``` $ build/sbt "test:testOnly *TimeExpressionsSuite.scala" ``` By manual tests: ``` scala> spark.sql("select minute(cast('12:30' as time(0)));").show() +------------------------------+ |minute(CAST(12:30 AS TIME(0)))| +------------------------------+ | 30| +------------------------------+ scala> spark.sql("select minute(cast('12:30' as time(2)));").show() +------------------------------+ |minute(CAST(12:30 AS TIME(2)))| +------------------------------+ | 30| +------------------------------+ scala> spark.sql("select minute(cast('12:30' as time(5)));").show() +------------------------------+ |minute(CAST(12:30 AS TIME(5)))| +------------------------------+ | 30| +------------------------------+ ``` ### Was this patch authored or co-authored using generative AI tooling? No Closes #50551 from the-sakthi/SPARK-51420-FOLLOWUP. Authored-by: Sakthi <[email protected]> Signed-off-by: Max Gekk <[email protected]>
1 parent c577ae7 commit adc42b4

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
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
@@ -191,7 +191,8 @@ case class MinutesOfTime(child: Expression)
191191
Seq(child.dataType)
192192
)
193193

194-
override def inputTypes: Seq[AbstractDataType] = Seq(TimeType())
194+
override def inputTypes: Seq[AbstractDataType] =
195+
Seq(TypeCollection(TimeType.MIN_PRECISION to TimeType.MAX_PRECISION map TimeType: _*))
195196

196197
override def children: Seq[Expression] = Seq(child)
197198

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,22 @@ class TimeExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
127127
"docroot" -> SPARK_DOC_ROOT)
128128
)
129129

130-
// test TIME-typed child should build MinutesOfTime
130+
// test TIME-typed child should build MinutesOfTime for default precision value
131131
val timeExpr = Literal(localTime(12, 58, 59), TimeType())
132132
val builtExprForTime = MinuteExpressionBuilder.build("minute", Seq(timeExpr))
133133
assert(builtExprForTime.isInstanceOf[MinutesOfTime])
134134
assert(builtExprForTime.asInstanceOf[MinutesOfTime].child eq timeExpr)
135+
assert(builtExprForTime.checkInputDataTypes().isSuccess)
136+
137+
// test TIME-typed child should build MinutesOfTime for all allowed custom precision values
138+
(TimeType.MIN_PRECISION to TimeType.MICROS_PRECISION).foreach { precision =>
139+
val timeExpr = Literal(localTime(12, 58, 59), TimeType(precision))
140+
val builtExpr = MinuteExpressionBuilder.build("minute", Seq(timeExpr))
141+
142+
assert(builtExpr.isInstanceOf[MinutesOfTime])
143+
assert(builtExpr.asInstanceOf[MinutesOfTime].child eq timeExpr)
144+
assert(builtExpr.checkInputDataTypes().isSuccess)
145+
}
135146

136147
// test non TIME-typed child should build Minute
137148
val tsExpr = Literal("2009-07-30 12:58:59")

0 commit comments

Comments
 (0)