Skip to content

Commit

Permalink
[SPARK-46284][PYTHON][CONNECT] Add session_user function to Python
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?
`session_user` function was added in Scala in #42549, this PR adds it to Python

### Why are the changes needed?
for parity

### Does this PR introduce _any_ user-facing change?
yes
```
    >>> import pyspark.sql.functions as sf
    >>> spark.range(1).select(sf.session_user()).show() # doctest: +SKIP
    +--------------+
    |current_user()|
    +--------------+
    | ruifeng.zheng|
    +--------------+
```

### How was this patch tested?
ci

### Was this patch authored or co-authored using generative AI tooling?
no

Closes #44205 from zhengruifeng/connect_session_user.

Authored-by: Ruifeng Zheng <ruifengz@apache.org>
Signed-off-by: Ruifeng Zheng <ruifengz@apache.org>
  • Loading branch information
zhengruifeng committed Dec 6, 2023
1 parent b9cc813 commit ea8b139
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
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 @@ -585,6 +585,7 @@ Misc Functions
monotonically_increasing_id
raise_error
reflect
session_user
spark_partition_id
try_aes_decrypt
try_reflect
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 @@ -3613,6 +3613,13 @@ def user() -> Column:
user.__doc__ = pysparkfuncs.user.__doc__


def session_user() -> Column:
return _invoke_function("session_user")


session_user.__doc__ = pysparkfuncs.session_user.__doc__


def assert_true(col: "ColumnOrName", errMsg: Optional[Union[Column, str]] = None) -> Column:
if errMsg is None:
return _invoke_function_over_columns("assert_true", col)
Expand Down
19 changes: 19 additions & 0 deletions python/pyspark/sql/functions/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8914,6 +8914,25 @@ def user() -> Column:
return _invoke_function("user")


@_try_remote_functions
def session_user() -> Column:
"""Returns the user name of current execution context.

.. versionadded:: 4.0.0

Examples
--------
>>> import pyspark.sql.functions as sf
>>> spark.range(1).select(sf.session_user()).show() # doctest: +SKIP
+--------------+
|current_user()|
+--------------+
| ruifeng.zheng|
+--------------+
"""
return _invoke_function("session_user")


@_try_remote_functions
def crc32(col: "ColumnOrName") -> Column:
"""
Expand Down
1 change: 0 additions & 1 deletion python/pyspark/sql/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def test_function_parity(self):
"random", # namespace conflict with python built-in module
"uuid", # namespace conflict with python built-in module
"chr", # namespace conflict with python built-in function
"session_user", # Scala only for now, needs implementation
"partitioning$", # partitioning expressions for DSv2
]

Expand Down

0 comments on commit ea8b139

Please sign in to comment.