yadavay-amzn commented on code in PR #56932:
URL: https://github.com/apache/spark/pull/56932#discussion_r3589018125
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/timeExpressions.scala:
##########
@@ -623,6 +623,57 @@ case class MakeTime(
copy(hours = newChildren(0), minutes = newChildren(1), secsAndMicros =
newChildren(2))
}
+// scalastyle:off line.size.limit
+@ExpressionDescription(
+ usage = "_FUNC_(hour, minute, second) - Try to create time from hour, minute
and second fields. The function returns NULL on invalid inputs.",
+ arguments = """
+ Arguments:
+ * hour - the hour to represent, from 0 to 23
+ * minute - the minute to represent, from 0 to 59
+ * second - the second to represent, from 0 to 59.999999
+ """,
+ examples = """
+ Examples:
+ > SELECT _FUNC_(6, 30, 45.887);
+ 06:30:45.887
+ > SELECT _FUNC_(NULL, 30, 0);
+ NULL
+ > SELECT _FUNC_(25, 30, 0);
+ NULL
+ """,
+ group = "datetime_funcs",
+ since = "4.3.0")
+// scalastyle:on line.size.limit
+object TryMakeTimeExpressionBuilder extends ExpressionBuilder {
+ override def build(funcName: String, expressions: Seq[Expression]):
Expression = {
+ val numArgs = expressions.length
+ if (numArgs == 3) {
+ new TryMakeTime(expressions(0), expressions(1), expressions(2))
+ } else {
+ throw QueryCompilationErrors.wrongNumArgsError(funcName, Seq(3), numArgs)
Review Comment:
Yes - TimeExpressionsSuite covers it in "creating values of TimeType via
try_make_time"; the invalid-input case goes through TryEval and returns null.
##########
sql/core/src/test/scala/org/apache/spark/sql/TimeFunctionsSuiteBase.scala:
##########
@@ -164,6 +164,49 @@ abstract class TimeFunctionsSuiteBase extends
SharedSparkSession {
checkAnswer(result2, expected)
}
+ test("SPARK-57846: try_make_time function") {
+ // Valid input data.
+ val schema = StructType(Seq(
+ StructField("hour", IntegerType, nullable = false),
+ StructField("minute", IntegerType, nullable = false),
+ StructField("second", DecimalType(16, 6), nullable = false)
+ ))
+ val data = Seq(
+ Row(0, 0, BigDecimal(0.0)),
+ Row(1, 2, BigDecimal(3.4)),
+ Row(23, 59, BigDecimal(59.999999))
+ )
+ val df = spark.createDataFrame(spark.sparkContext.parallelize(data),
schema)
+
+ // Test valid inputs using both selectExpr and select.
+ val result1 = df.selectExpr("try_make_time(hour, minute, second)")
+ val result2 = df.select(try_make_time(col("hour"), col("minute"),
col("second")))
+ checkAnswer(result1, result2)
+
+ val expected = Seq(
+ "00:00:00",
+ "01:02:03.4",
+ "23:59:59.999999"
+ ).toDF("timeString").select(col("timeString").cast("time"))
+ checkAnswer(result1, expected)
+
+ // Test invalid inputs return null instead of throwing.
+ val invalidSchema = StructType(Seq(
+ StructField("hour", IntegerType, nullable = false),
+ StructField("minute", IntegerType, nullable = false),
+ StructField("second", DecimalType(16, 6), nullable = false)
+ ))
Review Comment:
Done - dropped the duplicate schema and reused the existing `schema` val.
##########
sql/core/src/test/scala/org/apache/spark/sql/TimeFunctionsSuiteBase.scala:
##########
@@ -164,6 +164,49 @@ abstract class TimeFunctionsSuiteBase extends
SharedSparkSession {
checkAnswer(result2, expected)
}
+ test("SPARK-57846: try_make_time function") {
+ // Valid input data.
+ val schema = StructType(Seq(
+ StructField("hour", IntegerType, nullable = false),
+ StructField("minute", IntegerType, nullable = false),
+ StructField("second", DecimalType(16, 6), nullable = false)
+ ))
+ val data = Seq(
+ Row(0, 0, BigDecimal(0.0)),
+ Row(1, 2, BigDecimal(3.4)),
+ Row(23, 59, BigDecimal(59.999999))
+ )
+ val df = spark.createDataFrame(spark.sparkContext.parallelize(data),
schema)
+
+ // Test valid inputs using both selectExpr and select.
+ val result1 = df.selectExpr("try_make_time(hour, minute, second)")
+ val result2 = df.select(try_make_time(col("hour"), col("minute"),
col("second")))
+ checkAnswer(result1, result2)
+
+ val expected = Seq(
+ "00:00:00",
+ "01:02:03.4",
+ "23:59:59.999999"
+ ).toDF("timeString").select(col("timeString").cast("time"))
+ checkAnswer(result1, expected)
+
+ // Test invalid inputs return null instead of throwing.
+ val invalidSchema = StructType(Seq(
+ StructField("hour", IntegerType, nullable = false),
+ StructField("minute", IntegerType, nullable = false),
+ StructField("second", DecimalType(16, 6), nullable = false)
+ ))
+ val invalidData = Seq(
+ Row(25, 0, BigDecimal(0.0)),
+ Row(1, 60, BigDecimal(0.0)),
+ Row(1, 2, BigDecimal(60.0))
+ )
+ val invalidDf = spark.createDataFrame(
+ spark.sparkContext.parallelize(invalidData), invalidSchema)
+ val invalidResult = invalidDf.selectExpr("try_make_time(hour, minute,
second)")
+ checkAnswer(invalidResult, Seq(Row(null), Row(null), Row(null)))
+ }
+
Review Comment:
Added a NULL-column case through the DataFrame API that asserts
try_make_time returns null.
##########
sql/core/src/test/scala/org/apache/spark/sql/TimeFunctionsSuiteBase.scala:
##########
@@ -164,6 +164,49 @@ abstract class TimeFunctionsSuiteBase extends
SharedSparkSession {
checkAnswer(result2, expected)
}
+ test("SPARK-57846: try_make_time function") {
+ // Valid input data.
+ val schema = StructType(Seq(
+ StructField("hour", IntegerType, nullable = false),
+ StructField("minute", IntegerType, nullable = false),
+ StructField("second", DecimalType(16, 6), nullable = false)
+ ))
+ val data = Seq(
+ Row(0, 0, BigDecimal(0.0)),
+ Row(1, 2, BigDecimal(3.4)),
+ Row(23, 59, BigDecimal(59.999999))
+ )
+ val df = spark.createDataFrame(spark.sparkContext.parallelize(data),
schema)
+
+ // Test valid inputs using both selectExpr and select.
+ val result1 = df.selectExpr("try_make_time(hour, minute, second)")
+ val result2 = df.select(try_make_time(col("hour"), col("minute"),
col("second")))
+ checkAnswer(result1, result2)
+
+ val expected = Seq(
+ "00:00:00",
+ "01:02:03.4",
+ "23:59:59.999999"
+ ).toDF("timeString").select(col("timeString").cast("time"))
+ checkAnswer(result1, expected)
Review Comment:
Switched the valid-input assertion to checkAnswer against make_time
directly, matching the try_make_timestamp test.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]