Skip to content

[SPARK-53107][SQL] Implement the time_trunc function in Scala #51823

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

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 4 additions & 1 deletion python/pyspark/sql/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def test_function_parity(self):
missing_in_py = jvm_fn_set.difference(py_fn_set)

# Functions that we expect to be missing in python until they are added to pyspark
expected_missing_in_py = set()
expected_missing_in_py = set(
# TODO(SPARK-53107): Implement the time_trunc function in Python
["time_trunc"]
)

self.assertEqual(
expected_missing_in_py, missing_in_py, "Missing functions in pyspark not as expected"
Expand Down
21 changes: 21 additions & 0 deletions sql/api/src/main/scala/org/apache/spark/sql/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6292,6 +6292,27 @@ object functions {
def timestamp_add(unit: String, quantity: Column, ts: Column): Column =
Column.internalFn("timestampadd", lit(unit), quantity, ts)

/**
* Returns `time` truncated to the `unit`.
*
* @param unit
* A STRING representing the unit to truncate the time to. Supported units are: "HOUR",
* "MINUTE", "SECOND", "MILLISECOND", and "MICROSECOND". The unit is case-insensitive.
* @param time
* A TIME to truncate.
* @return
* A TIME truncated to the specified unit.
* @note
* If any of the inputs is `NULL`, the result is `NULL`.
* @throws IllegalArgumentException
* If the `unit` is not supported.
* @group datetime_funcs
* @since 4.1.0
*/
def time_trunc(unit: Column, time: Column): Column = {
Column.fn("time_trunc", unit, time)
}

/**
* Parses the `timestamp` expression with the `format` expression to a timestamp without time
* zone. Returns null with invalid input.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package org.apache.spark.sql
import java.time.LocalTime
import java.time.temporal.ChronoUnit

import org.apache.spark.{SparkConf, SparkDateTimeException}
import org.apache.spark.{SparkConf, SparkDateTimeException, SparkIllegalArgumentException}
import org.apache.spark.sql.functions._
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.test.SharedSparkSession
Expand Down Expand Up @@ -241,6 +241,60 @@ abstract class TimeFunctionsSuiteBase extends QueryTest with SharedSparkSession
checkAnswer(result2, expected)
}

test("SPARK-53107: time_trunc function") {
// Input data for the function (including null values).
val schema = StructType(Seq(
StructField("unit", StringType),
StructField("time", TimeType())
))
val data = Seq(
Row("HOUR", LocalTime.parse("00:00:00")),
Row("second", LocalTime.parse("01:02:03.4")),
Row("MicroSecond", LocalTime.parse("23:59:59.999999")),
Row(null, LocalTime.parse("01:02:03")),
Row("MiNuTe", null),
Row(null, null)
)
val df = spark.createDataFrame(spark.sparkContext.parallelize(data), schema)

// Test the function using both `selectExpr` and `select`.
val result1 = df.selectExpr(
"time_trunc(unit, time)"
)
val result2 = df.select(
time_trunc(col("unit"), col("time"))
)
// 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",
"23:59:59.999999",
null,
null,
null
).toDF("timeString").select(col("timeString").cast("time"))
// Check that the results match the expected output.
checkAnswer(result1, expected)
checkAnswer(result2, expected)

// Error is thrown for malformed input.
val invalidUnitDF = Seq(("invalid_unit", LocalTime.parse("01:02:03"))).toDF("unit", "time")
checkError(
exception = intercept[SparkIllegalArgumentException] {
invalidUnitDF.select(time_trunc(col("unit"), col("time"))).collect()
},
condition = "INVALID_PARAMETER_VALUE.TIME_UNIT",
parameters = Map(
"functionName" -> "`time_trunc`",
"parameter" -> "`unit`",
"invalidValue" -> "'invalid_unit'"
)
)
}

test("SPARK-52883: to_time function without format") {
// Input data for the function.
val schema = StructType(Seq(
Expand Down