I would like to propose an API called DataFrame.tail.
Background & Motivation
Many other systems support the way to take data from the end, for instance, pandas and
Python. Scala collections APIs also have head and tail
On the other hand, in Spark, we only provide a way to take data from the start
(e.g., DataFrame.head). This has been requested multiple times here and there in Spark
user mailing list, StackOverFlow, JIRA and other third party projects such as
It seems we're missing non-trivial use case in Spark and this motivated me to propose this
I would like to propose an API against DataFrame called tail that collects rows from the
end in contrast with head.
Namely, as below:
scala> spark.range(10).head(5) res1: Array[Long] = Array(0, 1, 2, 3, 4) scala> spark.range(10).tail(5) res2: Array[Long] = Array(5, 6, 7, 8, 9)
Implementation details will be similar with head but it will be reversed:
Run the job against the last partition and collect rows. If this is enough, return as is.
If this is not enough, calculate the number of partitions to select more based upon
Run more jobs against more partitions (in a reversed order compared to head)
as many as the number calculated from 2.
Go to 2.
- is duplicated by
SPARK-26433 Tail method for spark DataFrame
- links to