Skip to content

Commit

Permalink
SNOW-1873676: Fix date_format bug (#2854)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-aalam authored Jan 14, 2025
1 parent 412f348 commit 66dc14d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
#### Bug Fixes

- Fixed a bug in local testing mode that caused a column to contain None when it should contain 0
- Fixed a bug in StructField.from_json that prevented TimestampTypes with tzinfo from being parsed correctly.
- Fixed a bug in `StructField.from_json` that prevented TimestampTypes with tzinfo from being parsed correctly.
- Fixed a bug in function `date_format` that caused an error when the input column was date type or timestamp type.

### Snowpark pandas API Updates

Expand Down
19 changes: 18 additions & 1 deletion src/snowflake/snowpark/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3876,6 +3876,19 @@ def date_format(
|2022/05/15 10:45:00 |
-----------------------
<BLANKLINE>
Example::
>>> df = session.sql("select '2023-10-10'::DATE as date_col, '2023-10-10 15:30:00'::TIMESTAMP as timestamp_col")
>>> df.select(
... date_format('date_col', 'YYYY/MM/DD').as_('formatted_dt'),
... date_format('timestamp_col', 'YYYY/MM/DD HH:mi:ss').as_('formatted_ts')
... ).show()
----------------------------------------
|"FORMATTED_DT" |"FORMATTED_TS" |
----------------------------------------
|2023/10/10 |2023/10/10 15:30:00 |
----------------------------------------
<BLANKLINE>
"""

# AST.
Expand All @@ -3884,7 +3897,11 @@ def date_format(
ast = proto.Expr()
build_builtin_fn_apply(ast, "date_format", c, fmt)

ans = to_char(try_cast(c, TimestampType(), _emit_ast=False), fmt, _emit_ast=False)
ans = to_char(
try_cast(to_char(c, _emit_ast=False), TimestampType(), _emit_ast=False),
fmt,
_emit_ast=False,
)
ans._ast = ast
return ans

Expand Down

0 comments on commit 66dc14d

Please sign in to comment.