MaxGekk commented on code in PR #51575: URL: https://github.com/apache/spark/pull/51575#discussion_r2223265937
########## sql/api/src/main/scala/org/apache/spark/sql/functions.scala: ########## @@ -5731,6 +5764,39 @@ object functions { */ def to_timestamp(s: Column, fmt: String): Column = Column.fn("to_timestamp", s, lit(fmt)) + /** + * Parses a string value to a time value. + * + * @param str + * A string to be parsed to time + * @return + * A time, or null if the input is malformed. + * @group datetime_funcs + * @since 4.1.0 + */ + def try_to_time(str: Column): Column = { + Column.fn("try_to_time", str) + } + + /** + * Parses a string value to a time value. + * + * See <a href="https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html"> Datetime + * Patterns</a> for valid time format patterns. + * + * @param str + * A string to be parsed to time + * @param format + * Time format pattern to follow Review Comment: nit: ```suggestion * A time format pattern to follow. ``` ########## sql/core/src/test/scala/org/apache/spark/sql/TimeFunctionsSuiteBase.scala: ########## @@ -161,6 +161,150 @@ abstract class TimeFunctionsSuiteBase extends QueryTest with SharedSparkSession checkAnswer(result1, expected) checkAnswer(result2, expected) } + + test("SPARK-52883: to_time function without format") { + // Input data for the function. + val schema = StructType(Seq( + StructField("str", StringType, nullable = false) + )) + val data = Seq( + Row("00:00:00"), + Row("01:02:03.4"), + Row("23:59:59.999999") + ) + val df = spark.createDataFrame(spark.sparkContext.parallelize(data), schema) + + // Test the function using both `selectExpr` and `select`. + val result1 = df.selectExpr( + "to_time(str)" + ) + val result2 = df.select( + to_time(col("str")) + ) + // Check that both methods produce the same result. + checkAnswer(result1, result2) + + // Expected output of the function. + val expected = Seq( + "00:00:00", + "01:02:03.4", + "23:59:59.999999" + ).toDF("timeString").select(col("timeString").cast("time")) + // Check that the results match the expected output. + checkAnswer(result1, expected) + checkAnswer(result2, expected) + } + + test("SPARK-52883: to_time function with format") { Review Comment: Could you add a negative test w/ an error, please. ########## sql/api/src/main/scala/org/apache/spark/sql/functions.scala: ########## @@ -5699,6 +5699,39 @@ object functions { def unix_timestamp(s: Column, p: String): Column = Column.fn("unix_timestamp", s, lit(p)) + /** + * Parses a string value to a time value. + * + * @param str + * A string to be parsed to time + * @return + * A time, or raises an error if the input is malformed. + * @group datetime_funcs + * @since 4.1.0 + */ + def to_time(str: Column): Column = { + Column.fn("to_time", str) + } + + /** + * Parses a string value to a time value. + * + * See <a href="https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html"> Datetime + * Patterns</a> for valid time format patterns. + * + * @param str + * A string to be parsed to time + * @param format + * Time format pattern to follow Review Comment: ```suggestion * A time format pattern to follow. ``` ########## sql/api/src/main/scala/org/apache/spark/sql/functions.scala: ########## @@ -5731,6 +5764,39 @@ object functions { */ def to_timestamp(s: Column, fmt: String): Column = Column.fn("to_timestamp", s, lit(fmt)) + /** + * Parses a string value to a time value. + * + * @param str + * A string to be parsed to time Review Comment: nit: ```suggestion * A string to be parsed to time. ``` ########## sql/core/src/test/scala/org/apache/spark/sql/TimeFunctionsSuiteBase.scala: ########## @@ -161,6 +161,150 @@ abstract class TimeFunctionsSuiteBase extends QueryTest with SharedSparkSession checkAnswer(result1, expected) checkAnswer(result2, expected) } + + test("SPARK-52883: to_time function without format") { Review Comment: Please, add a negative test where the function raises an error. ########## sql/api/src/main/scala/org/apache/spark/sql/functions.scala: ########## @@ -5699,6 +5699,39 @@ object functions { def unix_timestamp(s: Column, p: String): Column = Column.fn("unix_timestamp", s, lit(p)) + /** + * Parses a string value to a time value. + * + * @param str + * A string to be parsed to time Review Comment: nit: ```suggestion * A string to be parsed to time. ``` ########## sql/api/src/main/scala/org/apache/spark/sql/functions.scala: ########## @@ -5699,6 +5699,39 @@ object functions { def unix_timestamp(s: Column, p: String): Column = Column.fn("unix_timestamp", s, lit(p)) + /** + * Parses a string value to a time value. + * + * @param str + * A string to be parsed to time + * @return + * A time, or raises an error if the input is malformed. + * @group datetime_funcs + * @since 4.1.0 + */ + def to_time(str: Column): Column = { + Column.fn("to_time", str) + } + + /** + * Parses a string value to a time value. + * + * See <a href="https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html"> Datetime + * Patterns</a> for valid time format patterns. + * + * @param str + * A string to be parsed to time Review Comment: nit: ```suggestion * A string to be parsed to time. ``` ########## sql/api/src/main/scala/org/apache/spark/sql/functions.scala: ########## @@ -5731,6 +5764,39 @@ object functions { */ def to_timestamp(s: Column, fmt: String): Column = Column.fn("to_timestamp", s, lit(fmt)) + /** + * Parses a string value to a time value. + * + * @param str + * A string to be parsed to time + * @return + * A time, or null if the input is malformed. + * @group datetime_funcs + * @since 4.1.0 + */ + def try_to_time(str: Column): Column = { + Column.fn("try_to_time", str) + } + + /** + * Parses a string value to a time value. + * + * See <a href="https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html"> Datetime + * Patterns</a> for valid time format patterns. + * + * @param str + * A string to be parsed to time Review Comment: ```suggestion * A string to be parsed to time. ``` -- 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: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org