Skip to content

Commit c4e5985

Browse files
SNOW-2398216: Add support for sort_values in faster pandas (#3862)
1 parent 0c077b7 commit c4e5985

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
- Set `cte_optimization_enabled` to True for all Snowpark pandas sessions.
4747
- Add support for `isna`, `isnull`, `notna`, `notnull` in faster pandas.
4848
- Add support for `str.contains`, `str.startswith`, `str.endswith`, and `str.slice` in faster pandas.
49+
- Add support for `sort_values` in faster pandas.
4950

5051
## 1.40.0 (2025-10-02)
5152

src/snowflake/snowpark/modin/plugin/compiler/snowflake_query_compiler.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4058,6 +4058,46 @@ def sort_rows_by_column_values(
40584058
key: Optional[IndexKeyFunc] = None,
40594059
include_indexer: bool = False,
40604060
include_index: bool = True,
4061+
) -> "SnowflakeQueryCompiler":
4062+
"""
4063+
Wrapper around _sort_rows_by_column_values_internal to be supported in faster pandas.
4064+
"""
4065+
relaxed_query_compiler = None
4066+
if self._relaxed_query_compiler is not None:
4067+
relaxed_query_compiler = (
4068+
self._relaxed_query_compiler._sort_rows_by_column_values_internal(
4069+
columns=columns,
4070+
ascending=ascending,
4071+
kind=kind,
4072+
na_position=na_position,
4073+
ignore_index=ignore_index,
4074+
key=key,
4075+
include_indexer=include_indexer,
4076+
include_index=include_index,
4077+
)
4078+
)
4079+
qc = self._sort_rows_by_column_values_internal(
4080+
columns=columns,
4081+
ascending=ascending,
4082+
kind=kind,
4083+
na_position=na_position,
4084+
ignore_index=ignore_index,
4085+
key=key,
4086+
include_indexer=include_indexer,
4087+
include_index=include_index,
4088+
)
4089+
return self._maybe_set_relaxed_qc(qc, relaxed_query_compiler)
4090+
4091+
def _sort_rows_by_column_values_internal(
4092+
self,
4093+
columns: list[Hashable],
4094+
ascending: list[bool],
4095+
kind: SortKind,
4096+
na_position: NaPosition,
4097+
ignore_index: bool,
4098+
key: Optional[IndexKeyFunc] = None,
4099+
include_indexer: bool = False,
4100+
include_index: bool = True,
40614101
) -> "SnowflakeQueryCompiler":
40624102
"""
40634103
Reorder the rows based on the lexicographic order of the given columns.

tests/integ/modin/test_faster_pandas.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,35 @@ def test_str_slice(session):
370370
assert_series_equal(snow_result, native_result)
371371

372372

373+
@sql_count_checker(query_count=3)
374+
def test_sort_values(session):
375+
# create tables
376+
table_name = Utils.random_name_for_temp_object(TempObjectType.TABLE)
377+
session.create_dataframe(
378+
native_pd.DataFrame([[2, 12], [1, 11], [3, 13]], columns=["A", "B"])
379+
).write.save_as_table(table_name, table_type="temp")
380+
381+
# create snow dataframes
382+
df = pd.read_snowflake(table_name)
383+
snow_result = df.sort_values(by="A")
384+
385+
# verify that the input dataframe has a populated relaxed query compiler
386+
assert df._query_compiler._relaxed_query_compiler is not None
387+
assert df._query_compiler._relaxed_query_compiler._dummy_row_pos_mode is True
388+
# verify that the output dataframe also has a populated relaxed query compiler
389+
assert snow_result._query_compiler._relaxed_query_compiler is not None
390+
assert (
391+
snow_result._query_compiler._relaxed_query_compiler._dummy_row_pos_mode is True
392+
)
393+
394+
# create pandas dataframes
395+
native_df = df.to_pandas()
396+
native_result = native_df.sort_values(by="A")
397+
398+
# compare results
399+
assert_frame_equal(snow_result, native_result)
400+
401+
373402
@sql_count_checker(query_count=0)
374403
def test_dummy_row_pos_optimization_enabled_on_session(db_parameters):
375404
with Session.builder.configs(db_parameters).create() as new_session:

0 commit comments

Comments
 (0)