Skip to content

Commit

Permalink
SNOW-1348700 Fix to_char's implementation when incoming column has in…
Browse files Browse the repository at this point in the history
…consecutive row index (#1702)
  • Loading branch information
sfc-gh-stan authored Jun 5, 2024
1 parent 40a534e commit f8b4b89
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#### Improvements


#### Bug Fixes

- Fixed a bug where python stored procedure with table return type fails when run in a task.
Expand All @@ -22,6 +21,7 @@
- Fixed a bug in convert_timezone that made the setting the source_timezone parameter return an error.
- Fixed a bug where creating DataFrame with empty data of type `DateType` raises `AttributeError`.
- Fixed a bug that table merge fails when update clause exists but no update takes place.
- Fixed a bug in mock implementation of `to_char` that raises `IndexError` when incoming column has inconsecutive row index.

### Snowpark pandas API Updates

Expand Down
11 changes: 4 additions & 7 deletions src/snowflake/snowpark/mock/_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,12 +906,10 @@ def mock_to_char(
"""
source_datatype = column.sf_type.datatype

fmt = [fmt] * len(column) if not isinstance(fmt, ColumnEmulator) else fmt

def convert_char(row):
_fmt = fmt[row.name]
data = row[0]
if not isinstance(fmt, ColumnEmulator):
fmt = ColumnEmulator([fmt] * len(column), index=column.index)

def convert_char(data, _fmt):
if isinstance(source_datatype, _NumericType):
if _fmt:
# SNOW-1372863 to support https://docs.snowflake.com/en/sql-reference/sql-format-models
Expand Down Expand Up @@ -1025,8 +1023,7 @@ def convert_char(row):
raise_error=NotImplementedError,
)

# row index information is needed to retrieve format information in another pd series, thus calling to_frame here
res = column.to_frame().apply(convert_char, axis=1)
res = column.combine(fmt, convert_char)
res.sf_type = ColumnType(StringType(), column.sf_type.nullable)
return res

Expand Down
8 changes: 8 additions & 0 deletions tests/mock/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
lit,
max,
min,
to_char,
to_date,
)

Expand Down Expand Up @@ -281,3 +282,10 @@ def test_show(session):
|3....|4 |de...|
----------------\n""".lstrip()
)


def test_to_char_is_row_index_agnostic(session):
df = session.create_dataframe([[1, 2], [3, 4], [5, 6]], schema=["a", "b"])
assert df.filter(col("a") > 3).select(to_char(col("a")), col("b")).collect() == [
Row("5", 6)
]

0 comments on commit f8b4b89

Please sign in to comment.