Skip to content

[SPARK-53110][SQL][PYTHON][CONNECT] Implement the time_trunc function in PySpark #51825

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 6 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
1 change: 1 addition & 0 deletions python/docs/source/reference/pyspark.sql/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ Date and Timestamp Functions
timestamp_micros
timestamp_millis
timestamp_seconds
time_trunc
to_date
to_time
to_timestamp
Expand Down
7 changes: 7 additions & 0 deletions python/pyspark/sql/connect/functions/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3643,6 +3643,13 @@ def timestamp_seconds(col: "ColumnOrName") -> Column:
timestamp_seconds.__doc__ = pysparkfuncs.timestamp_seconds.__doc__


def time_trunc(unit: "ColumnOrName", time: "ColumnOrName") -> Column:
return _invoke_function_over_columns("time_trunc", unit, time)


time_trunc.__doc__ = pysparkfuncs.time_trunc.__doc__


def timestamp_millis(col: "ColumnOrName") -> Column:
return _invoke_function_over_columns("timestamp_millis", col)

Expand Down
1 change: 1 addition & 0 deletions python/pyspark/sql/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@
"timestamp_micros",
"timestamp_millis",
"timestamp_seconds",
"time_trunc",
"to_date",
"to_time",
"to_timestamp",
Expand Down
40 changes: 40 additions & 0 deletions python/pyspark/sql/functions/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12702,6 +12702,46 @@ def timestamp_seconds(col: "ColumnOrName") -> Column:
return _invoke_function_over_columns("timestamp_seconds", col)


@_try_remote_functions
def time_trunc(unit: "ColumnOrName", time: "ColumnOrName") -> Column:
"""
Returns `time` truncated to the `unit`.

.. versionadded:: 4.1.0

Parameters
----------
unit : :class:`~pyspark.sql.Column` or column name
The unit to truncate the time to. Supported units are: "HOUR", "MINUTE", "SECOND",
"MILLISECOND", and "MICROSECOND". The unit is case-insensitive.
time : :class:`~pyspark.sql.Column` or column name
A time to truncate.

Returns
-------
:class:`~pyspark.sql.Column`
A time truncated to the specified unit.

See Also
--------
:meth:`pyspark.sql.functions.date_trunc`

Examples
--------
>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame(
... [("HOUR", "13:08:15")],
... ['unit', 'time']).withColumn("time", sf.col("time").cast("time"))
>>> df.select('*', sf.time_trunc('unit', 'time')).show()
+----+--------+----------------------+
|unit| time|time_trunc(unit, time)|
+----+--------+----------------------+
|HOUR|13:08:15| 13:00:00|
+----+--------+----------------------+
"""
return _invoke_function_over_columns("time_trunc", unit, time)


@_try_remote_functions
def timestamp_millis(col: "ColumnOrName") -> Column:
"""
Expand Down
18 changes: 14 additions & 4 deletions python/pyspark/sql/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ 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(
# TODO(SPARK-53107): Implement the time_trunc function in Python
["time_trunc"]
)
expected_missing_in_py = set()

self.assertEqual(
expected_missing_in_py, missing_in_py, "Missing functions in pyspark not as expected"
Expand Down Expand Up @@ -403,6 +400,19 @@ def test_rand_functions(self):
rndn2 = df.select("key", F.randn(0)).collect()
self.assertEqual(sorted(rndn1), sorted(rndn2))

def test_time_trunc(self):
# SPARK-53110: test the time_trunc function.
df = self.spark.range(1).select(
F.lit("minute").alias("unit"), F.lit(datetime.time(1, 2, 3)).alias("time")
)
result = datetime.time(1, 2, 0)
row_from_col = df.select(F.time_trunc(df.unit, df.time)).first()
self.assertIsInstance(row_from_col[0], datetime.time)
self.assertEqual(row_from_col[0], result)
row_from_name = df.select(F.time_trunc("unit", "time")).first()
self.assertIsInstance(row_from_name[0], datetime.time)
self.assertEqual(row_from_name[0], result)

def test_try_parse_url(self):
df = self.spark.createDataFrame(
[("https://spark.apache.org/path?query=1", "QUERY", "query")],
Expand Down