Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions dataset/src/main/scala/frameless/TypedDataset.scala
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,26 @@ class TypedDataset[T] protected[frameless](val dataset: Dataset[T])(implicit val
*/
def toLocalIterator[F[_]]()(implicit F: SparkDelay[F]): F[util.Iterator[T]] =
F.delay(dataset.toLocalIterator())

/** Alias for firstOption().
*/
def headOption[F[_]]()(implicit F: SparkDelay[F]): F[Option[T]] = firstOption()

/** Alias for take().
*/
def head[F[_]](num: Int)(implicit F: SparkDelay[F]): F[Seq[T]] = take(num)

// $COVERAGE-OFF$
/** Alias for firstOption().
*/
@deprecated("Method may throw exception. Use headOption or firstOption instead.", "0.5.0")
def head: T = dataset.head()

/** Alias for firstOption().
*/
@deprecated("Method may throw exception. Use headOption or firstOption instead.", "0.5.0")
def first: T = dataset.head()
// $COVERAGE-ONN$

/** Displays the content of this [[TypedDataset]] in a tabular form. Strings more than 20 characters
* will be truncated, and all cells will be aligned right. For example:
Expand Down
31 changes: 31 additions & 0 deletions dataset/src/test/scala/frameless/forward/HeadTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package frameless.forward

import frameless.{TypedDataset, TypedDatasetSuite, TypedEncoder, TypedExpressionEncoder, X1}
import org.apache.spark.sql.SparkSession
import org.scalacheck.Prop
import org.scalacheck.Prop._
import org.scalatest.Matchers

import scala.reflect.ClassTag

class HeadTests extends TypedDatasetSuite with Matchers {
def propArray[A: TypedEncoder : ClassTag : Ordering](data: Vector[X1[A]])(implicit c: SparkSession): Prop = {
import c.implicits._
if(data.nonEmpty) {
val tds = TypedDataset.
create(c.createDataset(data)(
TypedExpressionEncoder.apply[X1[A]]
).orderBy($"a".desc))
(tds.headOption().run().get ?= data.max).
&&(tds.head(1).run().head ?= data.max).
&&(tds.head(4).run().toVector ?=
data.sortBy(_.a)(implicitly[Ordering[A]].reverse).take(4))
} else Prop.passed
}

test("headOption(), head(1), and head(4)") {
check(propArray[Int] _)
check(propArray[Char] _)
check(propArray[String] _)
}
}