Skip to content
Closed
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
17 changes: 12 additions & 5 deletions sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import scala.collection.JavaConverters._
import org.apache.spark.annotation.Experimental
import org.apache.spark.rdd.RDD
import org.apache.spark.api.java.function._
import org.apache.spark.sql.catalyst.InternalRow

import org.apache.spark.sql.catalyst.encoders._
import org.apache.spark.sql.catalyst.expressions._
Expand Down Expand Up @@ -199,7 +200,6 @@ class Dataset[T] private[sql](
* @since 1.6.0
*/
def mapPartitions[U : Encoder](func: Iterator[T] => Iterator[U]): Dataset[U] = {
encoderFor[T].assertUnresolved()
new Dataset[U](
sqlContext,
MapPartitions[T, U](
Expand Down Expand Up @@ -519,7 +519,7 @@ class Dataset[T] private[sql](
* Returns the first element in this [[Dataset]].
* @since 1.6.0
*/
def first(): T = rdd.first()
def first(): T = take(1).head

/**
* Returns an array that contains all the elements in this [[Dataset]].
Expand All @@ -530,7 +530,14 @@ class Dataset[T] private[sql](
* For Java API, use [[collectAsList]].
* @since 1.6.0
*/
def collect(): Array[T] = rdd.collect()
def collect(): Array[T] = {
// This is different from Dataset.rdd in that it collects Rows, and then runs the encoders
// to convert the rows into objects of type T.
val tEnc = resolvedTEncoder
val input = queryExecution.analyzed.output
val bound = tEnc.bind(input)
queryExecution.toRdd.map(_.copy()).collect().map(bound.fromRow)
}

/**
* Returns an array that contains all the elements in this [[Dataset]].
Expand All @@ -541,7 +548,7 @@ class Dataset[T] private[sql](
* For Java API, use [[collectAsList]].
* @since 1.6.0
*/
def collectAsList(): java.util.List[T] = rdd.collect().toSeq.asJava
def collectAsList(): java.util.List[T] = collect().toSeq.asJava

/**
* Returns the first `num` elements of this [[Dataset]] as an array.
Expand All @@ -551,7 +558,7 @@ class Dataset[T] private[sql](
*
* @since 1.6.0
*/
def take(num: Int): Array[T] = rdd.take(num)
def take(num: Int): Array[T] = withPlan(Limit(Literal(num), _)).collect()

/**
* Returns the first `num` elements of this [[Dataset]] as an array.
Expand Down
30 changes: 29 additions & 1 deletion sql/core/src/test/scala/org/apache/spark/sql/DatasetSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,29 @@

package org.apache.spark.sql

import java.io.{ObjectInput, ObjectOutput, Externalizable}

import scala.language.postfixOps

import org.apache.spark.sql.functions._
import org.apache.spark.sql.test.SharedSQLContext

case class ClassData(a: String, b: Int)

/**
* A class used to test serialization using encoders. This class throws exceptions when using
* Java serialization -- so the only way it can be "serialized" is through our encoders.
*/
case class NonSerializableCaseClass(value: String) extends Externalizable {
override def readExternal(in: ObjectInput): Unit = {
throw new UnsupportedOperationException
}

override def writeExternal(out: ObjectOutput): Unit = {
throw new UnsupportedOperationException
}
}

class DatasetSuite extends QueryTest with SharedSQLContext {
import testImplicits._

Expand All @@ -41,6 +57,16 @@ class DatasetSuite extends QueryTest with SharedSQLContext {
1, 1, 1)
}

test("collect, first, and take should use encoders for serialization") {
val item = NonSerializableCaseClass("abcd")
val ds = Seq(item).toDS()
assert(ds.collect().head == item)
assert(ds.collectAsList().get(0) == item)
assert(ds.first() == item)
assert(ds.take(1).head == item)
assert(ds.takeAsList(1).get(0) == item)
}

test("as tuple") {
val data = Seq(("a", 1), ("b", 2)).toDF("a", "b")
checkAnswer(
Expand Down Expand Up @@ -75,6 +101,8 @@ class DatasetSuite extends QueryTest with SharedSQLContext {

ignore("Dataset should set the resolved encoders internally for maps") {
// TODO: Enable this once we fix SPARK-11793.
// We inject a group by here to make sure this test case is future proof
// when we implement better pipelining and local execution mode.
val ds: Dataset[(ClassData, Long)] = Seq(ClassData("one", 1), ClassData("two", 2)).toDS()
.map(c => ClassData(c.a, c.b + 1))
.groupBy(p => p).count()
Expand Down Expand Up @@ -219,7 +247,7 @@ class DatasetSuite extends QueryTest with SharedSQLContext {
("a", 30), ("b", 3), ("c", 1))
}

test("groupBy function, fatMap") {
test("groupBy function, flatMap") {
val ds = Seq(("a", 10), ("a", 20), ("b", 1), ("b", 2), ("c", 1)).toDS()
val grouped = ds.groupBy(v => (v._1, "word"))
val agged = grouped.flatMap { case (g, iter) => Iterator(g._1, iter.map(_._2).sum.toString) }
Expand Down