Skip to content

Commit 324a130

Browse files
NO-SNOW: fix type check before AST in sort() (#3982)
1 parent 5adff56 commit 324a130

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

src/snowflake/snowpark/dataframe.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,7 +2153,8 @@ def sort(
21532153
-------------
21542154
<BLANKLINE>
21552155
2156-
>>> df.sort(ascending=False).show()
2156+
>>> # Sort by all columns (ORDER BY ALL) - no columns specified
2157+
>>> df.sort([], ascending=False).show()
21572158
-------------
21582159
|"A" |"B" |
21592160
-------------
@@ -2182,7 +2183,10 @@ def sort(
21822183
The aliases ``order_by()`` and ``orderBy()`` have the same behavior.
21832184
"""
21842185

2185-
is_order_by_all = not cols
2186+
# This code performs additional type checks, run first.
2187+
exprs = self._convert_cols_to_exprs("sort()", *cols)
2188+
is_order_by_all = not cols or not exprs
2189+
21862190
if (
21872191
is_order_by_all
21882192
and ascending is not None
@@ -2230,10 +2234,6 @@ def sort(
22302234
order = Ascending() if bool(asc_value) else Descending()
22312235
sort_exprs = [SortByAllOrder(order)]
22322236
else:
2233-
exprs = self._convert_cols_to_exprs("sort()", *cols)
2234-
if not exprs:
2235-
raise ValueError("sort() needs at least one sort expression.")
2236-
22372237
orders = []
22382238
if ascending is not None:
22392239
if isinstance(ascending, (list, tuple)):

tests/integ/scala/test_dataframe_suite.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,11 +1219,6 @@ def test_sort(session):
12191219
for i in range(len(sorted_rows) - 1)
12201220
]
12211221

1222-
# Negative test: sort() needs at least one sort expression
1223-
with pytest.raises(ValueError) as ex_info:
1224-
df.sort([])
1225-
assert "sort() needs at least one sort expression" in ex_info.value.args[0]
1226-
12271222

12281223
def test_select(session):
12291224
df = session.create_dataframe([(1, "a", 10), (2, "b", 20), (3, "c", 30)]).to_df(

tests/integ/test_simplifier_suite.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,10 @@ def test_sort_by_all_sql_simplifier(session, use_simplified_query_generation):
18071807
df2 = df.select("a", "b", "c").sort(ascending=False)
18081808
assert "ORDER BY" and "ALL" in df2.queries["queries"][0].upper()
18091809
Utils.check_answer(df2, [Row(3, "c", 30), Row(2, "b", 20), Row(1, "a", 10)])
1810+
1811+
df3 = df.select("a", "b").orderBy([], ascending=True)
1812+
assert "ORDER BY" and "ALL" in df3.queries["queries"][0].upper()
1813+
Utils.check_answer(df3, df1)
18101814
finally:
18111815
session.conf.set("use_simplified_query_generation", original)
18121816

0 commit comments

Comments
 (0)