Skip to content

Commit 4fc3b8c

Browse files
Davies Liurxin
authored andcommitted
[SPARK-9978] [PYSPARK] [SQL] fix Window.orderBy and doc of ntile()
Author: Davies Liu <[email protected]> Closes #8213 from davies/fix_window. (cherry picked from commit 11ed2b1) Signed-off-by: Reynold Xin <[email protected]>
1 parent 969e8b3 commit 4fc3b8c

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

python/pyspark/sql/functions.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,10 @@ def lead(col, count=1, default=None):
440440
@since(1.4)
441441
def ntile(n):
442442
"""
443-
Window function: returns a group id from 1 to `n` (inclusive) in a round-robin fashion in
444-
a window partition. Fow example, if `n` is 3, the first row will get 1, the second row will
445-
get 2, the third row will get 3, and the fourth row will get 1...
443+
Window function: returns the ntile group id (from 1 to `n` inclusive)
444+
in an ordered window partition. Fow example, if `n` is 4, the first
445+
quarter of the rows will get value 1, the second quarter will get 2,
446+
the third quarter will get 3, and the last quarter will get 4.
446447
447448
This is equivalent to the NTILE function in SQL.
448449

python/pyspark/sql/tests.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,5 +900,28 @@ def test_window_functions(self):
900900
for r, ex in zip(rs, expected):
901901
self.assertEqual(tuple(r), ex[:len(r)])
902902

903+
def test_window_functions_without_partitionBy(self):
904+
df = self.sqlCtx.createDataFrame([(1, "1"), (2, "2"), (1, "2"), (1, "2")], ["key", "value"])
905+
w = Window.orderBy("key", df.value)
906+
from pyspark.sql import functions as F
907+
sel = df.select(df.value, df.key,
908+
F.max("key").over(w.rowsBetween(0, 1)),
909+
F.min("key").over(w.rowsBetween(0, 1)),
910+
F.count("key").over(w.rowsBetween(float('-inf'), float('inf'))),
911+
F.rowNumber().over(w),
912+
F.rank().over(w),
913+
F.denseRank().over(w),
914+
F.ntile(2).over(w))
915+
rs = sorted(sel.collect())
916+
expected = [
917+
("1", 1, 1, 1, 4, 1, 1, 1, 1),
918+
("2", 1, 1, 1, 4, 2, 2, 2, 1),
919+
("2", 1, 2, 1, 4, 3, 2, 2, 2),
920+
("2", 2, 2, 2, 4, 4, 4, 3, 2)
921+
]
922+
for r, ex in zip(rs, expected):
923+
self.assertEqual(tuple(r), ex[:len(r)])
924+
925+
903926
if __name__ == "__main__":
904927
unittest.main()

python/pyspark/sql/window.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def orderBy(*cols):
6464
Creates a :class:`WindowSpec` with the partitioning defined.
6565
"""
6666
sc = SparkContext._active_spark_context
67-
jspec = sc._jvm.org.apache.spark.sql.expressions.Window.partitionBy(_to_java_cols(cols))
67+
jspec = sc._jvm.org.apache.spark.sql.expressions.Window.orderBy(_to_java_cols(cols))
6868
return WindowSpec(jspec)
6969

7070

0 commit comments

Comments
 (0)