Using rowsBetween and rangeBetweenΒΆ

We can get cumulative aggregations using rowsBetween or rangeBetween.

  • We can use rowsBetween to include particular set of rows to perform aggregations.

  • We can use rangeBetween to include particular range of values on a given column.

Let us start spark context for this Notebook so that we can execute the code provided. You can sign up for our 10 node state of the art cluster/labs to learn Spark SQL using our unique integrated LMS.

from pyspark.sql import SparkSession

import getpass
username = getpass.getuser()

spark = SparkSession. \
    builder. \
    config('spark.ui.port', '0'). \
    config("spark.sql.warehouse.dir", f"/user/{username}/warehouse"). \
    enableHiveSupport(). \
    appName(f'{username} | Python - Windowing Functions'). \
    master('yarn'). \
    getOrCreate()

If you are going to use CLIs, you can use Spark SQL using one of the 3 approaches.

Using Spark SQL

spark2-sql \
    --master yarn \
    --conf spark.ui.port=0 \
    --conf spark.sql.warehouse.dir=/user/${USER}/warehouse

Using Scala

spark2-shell \
    --master yarn \
    --conf spark.ui.port=0 \
    --conf spark.sql.warehouse.dir=/user/${USER}/warehouse

Using Pyspark

pyspark2 \
    --master yarn \
    --conf spark.ui.port=0 \
    --conf spark.sql.warehouse.dir=/user/${USER}/warehouse
spark.conf.set('spark.sql.shuffle.partitions', '2')
airtraffic_path = "/public/airtraffic_all/airtraffic-part/flightmonth=200801"
airtraffic = spark. \
  read. \
  parquet(airtraffic_path)
from pyspark.sql.window import Window
  • Let us get cumulative delay at each airport using scheduled departure time as sorting criteria.

spec = Window. \
    partitionBy("FlightDate", "Origin"). \
    orderBy("CRSDepTime"). \
    rowsBetween(Window.unboundedPreceding, Window.currentRow)
from pyspark.sql.functions import lpad, concat, col, sum
airtraffic. \
    filter("IsDepDelayed = 'YES' and Cancelled = 0"). \
    select(concat("Year", 
                  lpad("Month", 2, "0"), 
                  lpad("DayOfMonth", 2, "0")
                 ).alias("FlightDate"),
           "Origin",
           "UniqueCarrier",
           "FlightNum",
           "CRSDepTime",
           "IsDepDelayed",
           col("DepDelay").cast("int").alias("DepDelay")
          ). \
    withColumn("DepDelaySum", sum("DepDelay").over(spec)). \
    orderBy("FlightDate", "Origin", "CRSDepTime"). \
    show()
+----------+------+-------------+---------+----------+------------+--------+-----------+
|FlightDate|Origin|UniqueCarrier|FlightNum|CRSDepTime|IsDepDelayed|DepDelay|DepDelaySum|
+----------+------+-------------+---------+----------+------------+--------+-----------+
|  20080101|   ABE|           OO|     5873|       720|         YES|       1|          1|
|  20080101|   ABE|           9E|     2940|      1215|         YES|      70|         71|
|  20080101|   ABE|           YV|     7263|      1230|         YES|     137|        208|
|  20080101|   ABE|           XE|     2578|      1410|         YES|      22|        230|
|  20080101|   ABE|           9E|     2936|      1615|         YES|      34|        264|
|  20080101|   ABE|           OH|     5457|      1720|         YES|      14|        278|
|  20080101|   ABE|           XE|     2594|      1740|         YES|      34|        312|
|  20080101|   ABE|           YV|     7138|      1741|         YES|     175|        487|
|  20080101|   ABI|           MQ|     3214|      1735|         YES|       3|          3|
|  20080101|   ABQ|           DL|     1601|       740|         YES|      65|         65|
|  20080101|   ABQ|           WN|       88|       755|         YES|       2|         67|
|  20080101|   ABQ|           UA|      782|       805|         YES|      48|        115|
|  20080101|   ABQ|           AA|     1814|       925|         YES|     100|        215|
|  20080101|   ABQ|           WN|     2976|      1040|         YES|       1|        216|
|  20080101|   ABQ|           WN|     1185|      1140|         YES|       9|        225|
|  20080101|   ABQ|           WN|     1297|      1155|         YES|      12|        237|
|  20080101|   ABQ|           WN|     2480|      1200|         YES|      16|        253|
|  20080101|   ABQ|           WN|      137|      1220|         YES|      10|        263|
|  20080101|   ABQ|           WN|     1328|      1245|         YES|      78|        341|
|  20080101|   ABQ|           WN|     3245|      1250|         YES|      24|        365|
+----------+------+-------------+---------+----------+------------+--------+-----------+
only showing top 20 rows
  • We can also get cumulative aggregations based up on moving window. In this case we are getting cumulative aggregation using previous 3 records and current record.

spec = Window. \
    partitionBy("FlightDate", "Origin"). \
    orderBy("CRSDepTime"). \
    rowsBetween(-3, Window.currentRow)
airtraffic. \
    filter("IsDepDelayed = 'YES' and Cancelled = 0"). \
    select(concat("Year", 
                  lpad("Month", 2, "0"), 
                  lpad("DayOfMonth", 2, "0")
                 ).alias("FlightDate"),
           "Origin",
           "UniqueCarrier",
           "FlightNum",
           "CRSDepTime",
           "IsDepDelayed",
           col("DepDelay").cast("int").alias("DepDelay")
          ). \
    withColumn("DepDelaySum", sum("DepDelay").over(spec)). \
    orderBy("FlightDate", "Origin", "CRSDepTime"). \
    show()
+----------+------+-------------+---------+----------+------------+--------+-----------+
|FlightDate|Origin|UniqueCarrier|FlightNum|CRSDepTime|IsDepDelayed|DepDelay|DepDelaySum|
+----------+------+-------------+---------+----------+------------+--------+-----------+
|  20080101|   ABE|           OO|     5873|       720|         YES|       1|          1|
|  20080101|   ABE|           9E|     2940|      1215|         YES|      70|         71|
|  20080101|   ABE|           YV|     7263|      1230|         YES|     137|        208|
|  20080101|   ABE|           XE|     2578|      1410|         YES|      22|        230|
|  20080101|   ABE|           9E|     2936|      1615|         YES|      34|        263|
|  20080101|   ABE|           OH|     5457|      1720|         YES|      14|        207|
|  20080101|   ABE|           XE|     2594|      1740|         YES|      34|        104|
|  20080101|   ABE|           YV|     7138|      1741|         YES|     175|        257|
|  20080101|   ABI|           MQ|     3214|      1735|         YES|       3|          3|
|  20080101|   ABQ|           DL|     1601|       740|         YES|      65|         65|
|  20080101|   ABQ|           WN|       88|       755|         YES|       2|         67|
|  20080101|   ABQ|           UA|      782|       805|         YES|      48|        115|
|  20080101|   ABQ|           AA|     1814|       925|         YES|     100|        215|
|  20080101|   ABQ|           WN|     2976|      1040|         YES|       1|        151|
|  20080101|   ABQ|           WN|     1185|      1140|         YES|       9|        158|
|  20080101|   ABQ|           WN|     1297|      1155|         YES|      12|        122|
|  20080101|   ABQ|           WN|     2480|      1200|         YES|      16|         38|
|  20080101|   ABQ|           WN|      137|      1220|         YES|      10|         47|
|  20080101|   ABQ|           WN|     1328|      1245|         YES|      78|        116|
|  20080101|   ABQ|           WN|     3245|      1250|         YES|      24|        128|
+----------+------+-------------+---------+----------+------------+--------+-----------+
only showing top 20 rows
  • We can also use rangeBetween to get cumulative delay at each airport using scheduled departure time as sorting criteria. With Window.unboundedPreceding and Window.currentRow, the behavior is same as rowsBetween.

spec = Window. \
    partitionBy("FlightDate", "Origin"). \
    orderBy("CRSDepTime"). \
    rangeBetween(Window.unboundedPreceding, Window.currentRow)
from pyspark.sql.functions import lpad, concat, col, sum
airtraffic. \
    filter("IsDepDelayed = 'YES' and Cancelled = 0"). \
    select(concat("Year", 
                  lpad("Month", 2, "0"), 
                  lpad("DayOfMonth", 2, "0")
                 ).alias("FlightDate"),
           "Origin",
           "UniqueCarrier",
           "FlightNum",
           "CRSDepTime",
           "IsDepDelayed",
           col("DepDelay").cast("int").alias("DepDelay")
          ). \
    withColumn("DepDelaySum", sum("DepDelay").over(spec)). \
    orderBy("FlightDate", "Origin", "CRSDepTime"). \
    show()
+----------+------+-------------+---------+----------+------------+--------+-----------+
|FlightDate|Origin|UniqueCarrier|FlightNum|CRSDepTime|IsDepDelayed|DepDelay|DepDelaySum|
+----------+------+-------------+---------+----------+------------+--------+-----------+
|  20080101|   ABE|           OO|     5873|       720|         YES|       1|          1|
|  20080101|   ABE|           9E|     2940|      1215|         YES|      70|         71|
|  20080101|   ABE|           YV|     7263|      1230|         YES|     137|        208|
|  20080101|   ABE|           XE|     2578|      1410|         YES|      22|        230|
|  20080101|   ABE|           9E|     2936|      1615|         YES|      34|        264|
|  20080101|   ABE|           OH|     5457|      1720|         YES|      14|        278|
|  20080101|   ABE|           XE|     2594|      1740|         YES|      34|        312|
|  20080101|   ABE|           YV|     7138|      1741|         YES|     175|        487|
|  20080101|   ABI|           MQ|     3214|      1735|         YES|       3|          3|
|  20080101|   ABQ|           DL|     1601|       740|         YES|      65|         65|
|  20080101|   ABQ|           WN|       88|       755|         YES|       2|         67|
|  20080101|   ABQ|           UA|      782|       805|         YES|      48|        115|
|  20080101|   ABQ|           AA|     1814|       925|         YES|     100|        215|
|  20080101|   ABQ|           WN|     2976|      1040|         YES|       1|        216|
|  20080101|   ABQ|           WN|     1185|      1140|         YES|       9|        225|
|  20080101|   ABQ|           WN|     1297|      1155|         YES|      12|        237|
|  20080101|   ABQ|           WN|     2480|      1200|         YES|      16|        253|
|  20080101|   ABQ|           WN|      137|      1220|         YES|      10|        263|
|  20080101|   ABQ|           WN|     1328|      1245|         YES|      78|        341|
|  20080101|   ABQ|           WN|     3245|      1250|         YES|      24|        365|
+----------+------+-------------+---------+----------+------------+--------+-----------+
only showing top 20 rows
  • rangeBetween considers the values rather than rows. With the logic below, it only consider those values which are between current row value and current row - 100 values based up on the CRSDepTime.

spec = Window. \
    partitionBy("FlightDate", "Origin"). \
    orderBy("CRSDepTime"). \
    rangeBetween(-100, Window.currentRow)
airtraffic. \
    filter("IsDepDelayed = 'YES' and Cancelled = 0"). \
    select(concat("Year", 
                  lpad("Month", 2, "0"), 
                  lpad("DayOfMonth", 2, "0")
                 ).alias("FlightDate"),
           "Origin",
           "UniqueCarrier",
           "FlightNum",
           "CRSDepTime",
           "IsDepDelayed",
           col("DepDelay").cast("int").alias("DepDelay")
          ). \
    withColumn("DepDelaySum", sum("DepDelay").over(spec)). \
    orderBy("FlightDate", "Origin", "CRSDepTime"). \
    show(50)
+----------+------+-------------+---------+----------+------------+--------+-----------+
|FlightDate|Origin|UniqueCarrier|FlightNum|CRSDepTime|IsDepDelayed|DepDelay|DepDelaySum|
+----------+------+-------------+---------+----------+------------+--------+-----------+
|  20080101|   ABE|           OO|     5873|       720|         YES|       1|          1|
|  20080101|   ABE|           9E|     2940|      1215|         YES|      70|         70|
|  20080101|   ABE|           YV|     7263|      1230|         YES|     137|        207|
|  20080101|   ABE|           XE|     2578|      1410|         YES|      22|         22|
|  20080101|   ABE|           9E|     2936|      1615|         YES|      34|         34|
|  20080101|   ABE|           OH|     5457|      1720|         YES|      14|         14|
|  20080101|   ABE|           XE|     2594|      1740|         YES|      34|         48|
|  20080101|   ABE|           YV|     7138|      1741|         YES|     175|        223|
|  20080101|   ABI|           MQ|     3214|      1735|         YES|       3|          3|
|  20080101|   ABQ|           DL|     1601|       740|         YES|      65|         65|
|  20080101|   ABQ|           WN|       88|       755|         YES|       2|         67|
|  20080101|   ABQ|           UA|      782|       805|         YES|      48|        115|
|  20080101|   ABQ|           AA|     1814|       925|         YES|     100|        100|
|  20080101|   ABQ|           WN|     2976|      1040|         YES|       1|          1|
|  20080101|   ABQ|           WN|     1185|      1140|         YES|       9|         10|
|  20080101|   ABQ|           WN|     1297|      1155|         YES|      12|         21|
|  20080101|   ABQ|           WN|     2480|      1200|         YES|      16|         37|
|  20080101|   ABQ|           WN|      137|      1220|         YES|      10|         47|
|  20080101|   ABQ|           WN|     1328|      1245|         YES|      78|        116|
|  20080101|   ABQ|           WN|     3245|      1250|         YES|      24|        140|
|  20080101|   ABQ|           WN|       61|      1320|         YES|       1|        113|
|  20080101|   ABQ|           DL|      940|      1345|         YES|       9|        112|
|  20080101|   ABQ|           WN|      734|      1400|         YES|      42|         52|
|  20080101|   ABQ|           XE|     2771|      1430|         YES|       3|         54|
|  20080101|   ABQ|           WN|       45|      1435|         YES|      64|        118|
|  20080101|   ABQ|           WN|     3425|      1440|         YES|       2|        120|
|  20080101|   ABQ|           WN|     3481|      1500|         YES|      78|        210|
|  20080101|   ABQ|           WN|     1135|      1500|         YES|      21|        210|
|  20080101|   ABQ|           WN|      901|      1520|         YES|      18|        189|
|  20080101|   ABQ|           WN|     2284|      1520|         YES|       3|        189|
|  20080101|   ABQ|           WN|      357|      1525|         YES|     171|        360|
|  20080101|   ABQ|           XE|     2922|      1530|         YES|      16|        383|
|  20080101|   ABQ|           WN|      729|      1530|         YES|       7|        383|
|  20080101|   ABQ|           WN|     1123|      1600|         YES|      15|        346|
|  20080101|   ABQ|           AA|     1486|      1600|         YES|      17|        346|
|  20080101|   ABQ|           OO|     6416|      1610|         YES|       8|        255|
|  20080101|   ABQ|           WN|      397|      1630|         YES|       9|         72|
|  20080101|   ABQ|           WN|      294|      1645|         YES|      11|         60|
|  20080101|   ABQ|           XE|     2018|      1705|         YES|      10|         38|
|  20080101|   ABQ|           WN|     2882|      1710|         YES|      16|         54|
|  20080101|   ABQ|           WN|      644|      1725|         YES|       5|         51|
|  20080101|   ABQ|           WN|     2788|      1755|         YES|      53|         84|
|  20080101|   ABQ|           WN|      360|      1800|         YES|       5|         89|
|  20080101|   ABQ|           WN|      972|      1810|         YES|       1|         80|
|  20080101|   ABQ|           WN|     2641|      1815|         YES|      19|         83|
|  20080101|   ABQ|           WN|     2497|      1825|         YES|      93|        176|
|  20080101|   ABQ|           WN|     1114|      1845|         YES|      14|        279|
|  20080101|   ABQ|           WN|     1178|      1845|         YES|      94|        279|
|  20080101|   ABQ|           XE|      221|      1850|         YES|      68|        347|
|  20080101|   ABQ|           WN|     2010|      1910|         YES|      15|        304|
+----------+------+-------------+---------+----------+------------+--------+-----------+
only showing top 50 rows