Skip to content
Open
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 @@ -327,6 +327,7 @@ Date and Timestamp Functions
unix_date
unix_micros
unix_millis
unix_nanos
unix_seconds
unix_timestamp
weekday
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 @@ -3531,6 +3531,13 @@ def unix_millis(col: "ColumnOrName") -> Column:
unix_millis.__doc__ = pysparkfuncs.unix_millis.__doc__


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


unix_nanos.__doc__ = pysparkfuncs.unix_nanos.__doc__


def unix_seconds(col: "ColumnOrName") -> Column:
return _invoke_function_over_columns("unix_seconds", 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 @@ -276,6 +276,7 @@
"unix_date",
"unix_micros",
"unix_millis",
"unix_nanos",
"unix_seconds",
"unix_timestamp",
"weekday",
Expand Down
51 changes: 51 additions & 0 deletions python/pyspark/sql/functions/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11792,6 +11792,57 @@ def unix_millis(col: "ColumnOrName") -> Column:
return _invoke_function_over_columns("unix_millis", col)


@_try_remote_functions
def unix_nanos(col: "ColumnOrName") -> Column:
"""Returns the number of nanoseconds since 1970-01-01 00:00:00 UTC as ``DECIMAL(21, 0)``.
Only supported for ``TIMESTAMP_LTZ(p)`` and ``TIMESTAMP_NTZ(p)`` with precision ``p``
in ``[7, 9]``.

.. versionadded:: 4.3.0

Parameters
----------
col : :class:`~pyspark.sql.Column` or column name
input column of nanosecond-precision timestamp values to convert.

Returns
-------
:class:`~pyspark.sql.Column`
the number of nanoseconds since 1970-01-01 00:00:00 UTC as ``DECIMAL(21, 0)``.

See Also
--------
:meth:`pyspark.sql.functions.unix_date`
:meth:`pyspark.sql.functions.unix_seconds`
:meth:`pyspark.sql.functions.unix_millis`
:meth:`pyspark.sql.functions.unix_micros`

Examples
--------
>>> import pyspark.sql.functions as sf
>>> spark.conf.set("spark.sql.timestampNanosTypes.enabled", True)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: The new doctest uses a Python bool value (True), but every other doctest in builtin.py uses the string form, e.g. spark.conf.set("spark.sql.ansi.enabled", "true"). It works (the value is coerced to "True" and the boolean conf parser is case-insensitive), but "true" would be more consistent with the file's convention.

>>> df = spark.sql(
... "SELECT TIMESTAMP_NTZ '2020-01-01 13:24:35.123456789' AS ts"
... )
>>> df.select('*', sf.unix_nanos('ts')).show(truncate=False)
+-----------------------------+-------------------+
|ts |unix_nanos(ts) |
+-----------------------------+-------------------+
|2020-01-01 13:24:35.123456789|1577885075123456789|
+-----------------------------+-------------------+

>>> df.select(sf.unix_nanos(sf.lit(None).cast('timestamp_ntz(9)'))).show()
+------------------------------------------+
|unix_nanos(CAST(NULL AS TIMESTAMP_NTZ(9)))|
+------------------------------------------+
| NULL|
+------------------------------------------+

>>> spark.conf.unset("spark.sql.timestampNanosTypes.enabled")
"""
return _invoke_function_over_columns("unix_nanos", col)


@_try_remote_functions
def unix_seconds(col: "ColumnOrName") -> Column:
"""Returns the number of seconds since 1970-01-01 00:00:00 UTC.
Expand Down
4 changes: 1 addition & 3 deletions python/pyspark/sql/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,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 = {
"unix_nanos", # SPARK-57527: PySpark support tracked as a follow-up
}
expected_missing_in_py = set()

self.assertEqual(
expected_missing_in_py, missing_in_py, "Missing functions in pyspark not as expected"
Expand Down