Pyspark Window orderBy

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Pyspark Window orderBy

mhussain
Hi,

I have a dataframe which looks like

+--------+---+------+----+
|group_id| id|  text|type|
+--------+---+------+----+
|       1|  1|   one|   a|
|       1|  1|   two|   t|
|       1|  2| three|   a|
|       1|  2|  four|   t|
|       1|  5|  five|   a|
|       1|  6|   six|   t|
|       1|  7| seven|   a|
|       1|  9| eight|   t|
|       1|  9|  nine|   a|
|       1| 10|   ten|   t|
|       1| 11|eleven|   a|
+--------+---+------+----+
If I do Window operation by partitioning it on group_id and ordering it by
id then will orderby make sure that already ordered(sorted) rows retain the
same order?

e.g.

window_spec = Window.partitionBy(df.group_id).orderBy(df.id)
df = df.withColumn("row_number", row_number().over(window_spec))
Will the result always be as bellow?

+--------+---+------+----+------+
|group_id| id|  text|type|row_number|
+--------+---+------+----+------+
|       1|  1|   one|   a|     1|
|       1|  1|   two|   t|     2|
|       1|  2| three|   a|     3|
|       1|  2|  four|   t|     4|
|       1|  5|  five|   a|     5|
|       1|  6|   six|   t|     6|
|       1|  7| seven|   a|     7|
|       1|  9| eight|   t|     8|
|       1|  9|  nine|   a|     9|
|       1| 10|   ten|   t|    10|
|       1| 11|eleven|   a|    11|
+--------+---+------+----+------+
In the nutshell my question is, how spark Window's orderBy handles already
ordered(sorted) rows? My assumption is it is stable i.e. it doesn't change
the order of already ordered rows but I couldn't find anything related to
this in the documentation. How can I make sure that my assumption is
correct?

I am using python 3.5 and pyspark 2.3.1.

Thanks.
Muddasser



--
Sent from: http://apache-spark-user-list.1001560.n3.nabble.com/

---------------------------------------------------------------------
To unsubscribe e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [External Sender] Pyspark Window orderBy

Anthony, Olufemi
I think that’s how it should behave. Did you try it out and see ?

On Tue, Oct 16, 2018 at 5:11 AM mhussain <[hidden email]> wrote:
Hi,

I have a dataframe which looks like

+--------+---+------+----+
|group_id| id|  text|type|
+--------+---+------+----+
|       1|  1|   one|   a|
|       1|  1|   two|   t|
|       1|  2| three|   a|
|       1|  2|  four|   t|
|       1|  5|  five|   a|
|       1|  6|   six|   t|
|       1|  7| seven|   a|
|       1|  9| eight|   t|
|       1|  9|  nine|   a|
|       1| 10|   ten|   t|
|       1| 11|eleven|   a|
+--------+---+------+----+
If I do Window operation by partitioning it on group_id and ordering it by
id then will orderby make sure that already ordered(sorted) rows retain the
same order?

e.g.

window_spec = Window.partitionBy(df.group_id).orderBy(df.id)
df = df.withColumn("row_number", row_number().over(window_spec))
Will the result always be as bellow?

+--------+---+------+----+------+
|group_id| id|  text|type|row_number|
+--------+---+------+----+------+
|       1|  1|   one|   a|     1|
|       1|  1|   two|   t|     2|
|       1|  2| three|   a|     3|
|       1|  2|  four|   t|     4|
|       1|  5|  five|   a|     5|
|       1|  6|   six|   t|     6|
|       1|  7| seven|   a|     7|
|       1|  9| eight|   t|     8|
|       1|  9|  nine|   a|     9|
|       1| 10|   ten|   t|    10|
|       1| 11|eleven|   a|    11|
+--------+---+------+----+------+
In the nutshell my question is, how spark Window's orderBy handles already
ordered(sorted) rows? My assumption is it is stable i.e. it doesn't change
the order of already ordered rows but I couldn't find anything related to
this in the documentation. How can I make sure that my assumption is
correct?

I am using python 3.5 and pyspark 2.3.1.

Thanks.
Muddasser



--
Sent from: https://urldefense.proofpoint.com/v2/url?u=http-3A__apache-2Dspark-2Duser-2Dlist.1001560.n3.nabble.com_&d=DwICAg&c=pLULRYW__RtkwsQUPxJVDGboCTdgji3AcHNJU0BpTJE&r=yGeUxkUZBNPLfjlLWOxq5_p1UIOy_S4ghJsg2_iDHFY&m=r4NFmE8gXjw_dq6qvMLTr4IiW1lhjnwNuGIYMbRRvTM&s=1BpwwJdIDw42r_6EBfuGZXFytg37mxb_YZ18HXM5RtU&e=

---------------------------------------------------------------------
To unsubscribe e-mail: [hidden email]



The information contained in this e-mail is confidential and/or proprietary to Capital One and/or its affiliates and may only be used solely in performance of work or services for Capital One. The information transmitted herewith is intended only for use by the individual or entity to which it is addressed. If the reader of this message is not the intended recipient, you are hereby notified that any review, retransmission, dissemination, distribution, copying or other use of, or taking of any action in reliance upon this information is strictly prohibited. If you have received this communication in error, please contact the sender and delete the material from your computer.

Reply | Threaded
Open this post in threaded view
|

Re: [External Sender] Pyspark Window orderBy

mhussain
Yes, I did try it and you are right it behaves the same so far. I am not sure
how its gonna behave for large data sets though. I don't see anything in the
documentation confirming this behavior.



--
Sent from: http://apache-spark-user-list.1001560.n3.nabble.com/

---------------------------------------------------------------------
To unsubscribe e-mail: [hidden email]