-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-53547] [PYTHON] Add make_timestamp_ntz overload with date/time parameters #52329
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
Yicong-Huang
wants to merge
13
commits into
apache:master
from
Yicong-Huang:feat/SPARK-51162/add-make-datetime-ntz-from-date-and-time
Closed
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8a7bca3
feat: add make_timestamp_ntz overload with date/time parameters
Yicong-Huang 4bbadd3
revert: bring back the import
Yicong-Huang 1712127
refactor: simplify error handling and improve error messages in make_…
Yicong-Huang 579c45a
test
Yicong-Huang b52c2d5
Revert "test"
Yicong-Huang 747aaef
feat: add WRONG_NUM_ARGS error class and update make_timestamp_ntz er…
Yicong-Huang 3124668
refactor: make error messages more specific with parameter names
Yicong-Huang 96059d8
change order
Yicong-Huang 33821c8
feat: finalize make_timestamp_ntz overload implementation
Yicong-Huang bf2d24a
docs: restructure make_timestamp_ntz parameter documentation
Yicong-Huang 3f92037
reformat
Yicong-Huang 101a78c
fix test
Yicong-Huang a7ddbe5
Refactor docstring for make_timestamp_ntz function
Yicong-Huang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25126,17 +25126,37 @@ def try_make_timestamp_ltz( | |
| ) | ||
|
|
||
|
|
||
| @_try_remote_functions | ||
| @overload | ||
| def make_timestamp_ntz( | ||
| years: "ColumnOrName", | ||
| months: "ColumnOrName", | ||
| days: "ColumnOrName", | ||
| hours: "ColumnOrName", | ||
| mins: "ColumnOrName", | ||
| secs: "ColumnOrName", | ||
| ) -> Column: | ||
| ... | ||
|
|
||
|
|
||
| @overload | ||
| def make_timestamp_ntz(*, date: "ColumnOrName", time: "ColumnOrName") -> Column: | ||
| ... | ||
|
|
||
|
|
||
| @_try_remote_functions | ||
| def make_timestamp_ntz( | ||
| years: Optional["ColumnOrName"] = None, | ||
| months: Optional["ColumnOrName"] = None, | ||
| days: Optional["ColumnOrName"] = None, | ||
| hours: Optional["ColumnOrName"] = None, | ||
| mins: Optional["ColumnOrName"] = None, | ||
| secs: Optional["ColumnOrName"] = None, | ||
| *, | ||
| date: Optional["ColumnOrName"] = None, | ||
| time: Optional["ColumnOrName"] = None, | ||
| ) -> Column: | ||
| """ | ||
| Create local date-time from years, months, days, hours, mins, secs fields. | ||
| Create local date-time from years, months, days, hours, mins, secs fields, or from date and time fields. | ||
| If the configuration `spark.sql.ansi.enabled` is false, the function returns NULL | ||
| on invalid inputs. Otherwise, it will throw an error instead. | ||
|
|
||
|
|
@@ -25155,10 +25175,14 @@ def make_timestamp_ntz( | |
| mins : :class:`~pyspark.sql.Column` or column name | ||
| The minute-of-hour to represent, from 0 to 59 | ||
| secs : :class:`~pyspark.sql.Column` or column name | ||
| The second-of-minute and its micro-fraction to represent, from 0 to 60. | ||
| The second-of-minute and its micro-fraction to represent, from 0 to 60.. | ||
|
||
| The value can be either an integer like 13 , or a fraction like 13.123. | ||
| If the sec argument equals to 60, the seconds field is set | ||
| to 0 and 1 minute is added to the final timestamp. | ||
| date : :class:`~pyspark.sql.Column` or column name | ||
| A date to represent, from 0001-01-01 to 9999-12-31 | ||
| time : :class:`~pyspark.sql.Column` or column name | ||
| A local time to represent, from 00:00:00 to 23:59:59.999999 | ||
|
|
||
| Returns | ||
| ------- | ||
|
|
@@ -25179,6 +25203,8 @@ def make_timestamp_ntz( | |
| -------- | ||
| >>> spark.conf.set("spark.sql.session.timeZone", "America/Los_Angeles") | ||
|
|
||
| Example 1: Create local date-time from year, month, day, hour, min, sec fields. | ||
|
|
||
| >>> import pyspark.sql.functions as sf | ||
| >>> df = spark.createDataFrame([[2014, 12, 28, 6, 30, 45.887]], | ||
| ... ['year', 'month', 'day', 'hour', 'min', 'sec']) | ||
|
|
@@ -25191,11 +25217,42 @@ def make_timestamp_ntz( | |
| |2014-12-28 06:30:45.887 | | ||
| +----------------------------------------------------+ | ||
|
|
||
| Example 2: Create local date-time from date and time fields. | ||
|
|
||
| >>> import pyspark.sql.functions as sf | ||
| >>> df = spark.createDataFrame([['2014-12-28', '06:30:45.887']], | ||
| ... ['date_col', 'time_col']) | ||
| >>> df.select( | ||
| ... sf.make_timestamp_ntz(date=sf.to_date(df.date_col), time=sf.to_time(df.time_col)) | ||
| ... ).show(truncate=False) | ||
| +------------------------------------------------------------------+ | ||
| |make_timestamp_ntz(date=to_date(date_col), time=to_time(time_col))| | ||
| +------------------------------------------------------------------+ | ||
| |2014-12-28 06:30:45.887 | | ||
| +------------------------------------------------------------------+ | ||
|
|
||
| >>> spark.conf.unset("spark.sql.session.timeZone") | ||
| """ | ||
| return _invoke_function_over_columns( | ||
| "make_timestamp_ntz", years, months, days, hours, mins, secs | ||
| ) | ||
| # Check for mixed arguments (invalid) | ||
| if any(arg is not None for arg in [years, months, days, hours, mins, secs]) and (date is not None or time is not None): | ||
| raise PySparkValueError( | ||
| errorClass="WRONG_NUM_ARGS", | ||
| messageParameters={"func_name": "make_timestamp_ntz", "expected": "either (years, months, days, hours, mins, secs) or (date, time)", "actual": "cannot mix both approaches"} | ||
| ) | ||
|
|
||
| # Handle valid cases | ||
| if date is not None and time is not None: | ||
| # make_timestamp_ntz(date=..., time=...) | ||
| return _invoke_function_over_columns("make_timestamp_ntz", date, time) | ||
| elif all(arg is not None for arg in [years, months, days, hours, mins, secs]): | ||
| # make_timestamp_ntz(years, months, days, hours, mins, secs) | ||
| return _invoke_function_over_columns("make_timestamp_ntz", years, months, days, hours, mins, secs) | ||
| else: | ||
| # Invalid argument combination (partial arguments) | ||
| raise PySparkValueError( | ||
| errorClass="WRONG_NUM_ARGS", | ||
| messageParameters={"func_name": "make_timestamp_ntz", "expected": "either all 6 components (years, months, days, hours, mins, secs) or both date and time", "actual": "incomplete arguments"} | ||
| ) | ||
|
|
||
|
|
||
| @_try_remote_functions | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we add a
.. versionchanged::?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! Which version should I use? (4.1?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4.1.0