Skip to content

Commit

Permalink
[MINOR][PYTHON] Fix type hint for from_utc_timestamp and `to_utc_ti…
Browse files Browse the repository at this point in the history
…mestamp`

### What changes were proposed in this pull request?
Fix type hint for `from_utc_timestamp` and `to_utc_timestamp`

### Why are the changes needed?
the str type input should be treated as literal string, instead of column name

### Does this PR introduce _any_ user-facing change?
doc change

### How was this patch tested?
CI

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

Closes apache#47429 from zhengruifeng/py_fix_hint_202407.

Authored-by: Ruifeng Zheng <ruifengz@apache.org>
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
  • Loading branch information
zhengruifeng authored and jingz-db committed Jul 22, 2024
1 parent 8a13602 commit 039148f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
12 changes: 4 additions & 8 deletions python/pyspark/sql/connect/functions/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3349,19 +3349,15 @@ def unix_timestamp(
unix_timestamp.__doc__ = pysparkfuncs.unix_timestamp.__doc__


def from_utc_timestamp(timestamp: "ColumnOrName", tz: "ColumnOrName") -> Column:
if isinstance(tz, str):
tz = lit(tz)
return _invoke_function_over_columns("from_utc_timestamp", timestamp, tz)
def from_utc_timestamp(timestamp: "ColumnOrName", tz: Union[Column, str]) -> Column:
return _invoke_function_over_columns("from_utc_timestamp", timestamp, lit(tz))


from_utc_timestamp.__doc__ = pysparkfuncs.from_utc_timestamp.__doc__


def to_utc_timestamp(timestamp: "ColumnOrName", tz: "ColumnOrName") -> Column:
if isinstance(tz, str):
tz = lit(tz)
return _invoke_function_over_columns("to_utc_timestamp", timestamp, tz)
def to_utc_timestamp(timestamp: "ColumnOrName", tz: Union[Column, str]) -> Column:
return _invoke_function_over_columns("to_utc_timestamp", timestamp, lit(tz))


to_utc_timestamp.__doc__ = pysparkfuncs.to_utc_timestamp.__doc__
Expand Down
14 changes: 3 additions & 11 deletions python/pyspark/sql/functions/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9144,7 +9144,7 @@ def unix_timestamp(


@_try_remote_functions
def from_utc_timestamp(timestamp: "ColumnOrName", tz: "ColumnOrName") -> Column:
def from_utc_timestamp(timestamp: "ColumnOrName", tz: Union[Column, str]) -> Column:
"""
This is a common function for databases supporting TIMESTAMP WITHOUT TIMEZONE. This function
takes a timestamp which is timezone-agnostic, and interprets it as a timestamp in UTC, and
Expand Down Expand Up @@ -9192,11 +9192,7 @@ def from_utc_timestamp(timestamp: "ColumnOrName", tz: "ColumnOrName") -> Column:
>>> df.select(from_utc_timestamp(df.ts, df.tz).alias('local_time')).collect()
[Row(local_time=datetime.datetime(1997, 2, 28, 19, 30))]
"""
from pyspark.sql.classic.column import _to_java_column

if isinstance(tz, Column):
tz = _to_java_column(tz)
return _invoke_function("from_utc_timestamp", _to_java_column(timestamp), tz)
return _invoke_function_over_columns("from_utc_timestamp", timestamp, lit(tz))


@_try_remote_functions
Expand Down Expand Up @@ -9248,11 +9244,7 @@ def to_utc_timestamp(timestamp: "ColumnOrName", tz: "ColumnOrName") -> Column:
>>> df.select(to_utc_timestamp(df.ts, df.tz).alias('utc_time')).collect()
[Row(utc_time=datetime.datetime(1997, 2, 28, 1, 30))]
"""
from pyspark.sql.classic.column import _to_java_column

if isinstance(tz, Column):
tz = _to_java_column(tz)
return _invoke_function("to_utc_timestamp", _to_java_column(timestamp), tz)
return _invoke_function_over_columns("to_utc_timestamp", timestamp, lit(tz))


@_try_remote_functions
Expand Down

0 comments on commit 039148f

Please sign in to comment.