-
Notifications
You must be signed in to change notification settings - Fork 153
SNOW-3746497 Snowpark Python SDK: add interval type support for UDFs & Sprocs #4291
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
Changes from all commits
3807ef5
a56f266
3a4026b
1b5413f
6fde586
e96dd28
6238803
045d18a
003afee
558e4be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -501,6 +501,31 @@ def to_sql( | |
| if isinstance(value, str) and isinstance(datatype, DayTimeIntervalType): | ||
| return f"{str_to_sql_for_day_time_interval(value, datatype)} :: {convert_sp_to_sf_type(datatype)}" | ||
|
|
||
| if isinstance(value, timedelta) and isinstance(datatype, DayTimeIntervalType): | ||
| # Serialize timedelta as an INTERVAL DAY TO SECOND literal. | ||
| # timedelta stores (days, seconds, microseconds) all non-negative after normalization. | ||
| sign = "-" if value < timedelta(0) else "+" | ||
| abs_val = abs(value) | ||
| d = abs_val.days | ||
| total_us = abs_val.seconds * 1_000_000 + abs_val.microseconds | ||
| h = total_us // 3_600_000_000 | ||
| total_us %= 3_600_000_000 | ||
| m = total_us // 60_000_000 | ||
| total_us %= 60_000_000 | ||
| s = total_us // 1_000_000 | ||
| us = total_us % 1_000_000 | ||
| interval_str = f"{sign}{d} {h:02d}:{m:02d}:{s:02d}.{us:06d}" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you check if we can reuse format_day_time_interval() or format_day_time_interval_for_display() in type_utils.py?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The display functions ( For the suffix, you are right. I have added the suffix for consistency. |
||
| return f"INTERVAL '{interval_str}' DAY TO SECOND :: {convert_sp_to_sf_type(datatype)}" | ||
|
|
||
| if isinstance(value, int) and isinstance(datatype, YearMonthIntervalType): | ||
| # Serialize int (total months) as an INTERVAL YEAR TO MONTH literal. | ||
| sign = "-" if value < 0 else "+" | ||
| abs_months = abs(value) | ||
| years = abs_months // 12 | ||
| months = abs_months % 12 | ||
| interval_str = f"{sign}{years}-{months:02d}" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similar here, is it possible we reuse
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return f"INTERVAL '{interval_str}' YEAR TO MONTH :: {convert_sp_to_sf_type(datatype)}" | ||
|
|
||
| raise TypeError(f"Unsupported datatype {datatype}, value {value} by to_sql()") | ||
|
|
||
|
|
||
|
|
||
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.
Can we unit test this conversion to SQL string? The math is pretty complicated so some tests would be ideal