Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[SPARK-11797][SQL] collect, first, and take should use encoders for s…
…erialization.
  • Loading branch information
rxin committed Nov 18, 2015
commit 9dec76fbd37376970bbb6a3f894ddb9cc48a8f43
14 changes: 10 additions & 4 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 @@ -519,7 +520,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 +531,12 @@ class Dataset[T] private[sql](
* For Java API, use [[collectAsList]].
* @since 1.6.0
*/
def collect(): Array[T] = rdd.collect()
def collect(): Array[T] = {
val tEnc = resolvedTEncoder
val input = queryExecution.analyzed.output
val bound = tEnc.bind(input)
queryExecution.toRdd.map(_.copy()).collect().map(bound.fromRow)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, it looks to me that the only difference between this and Dataset.rdd is you add a copy here, should we remove Dataset.rdd?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the main difference is that we are collecting the rows and then running the encoders to do conversions here.

}

/**
* Returns an array that contains all the elements in this [[Dataset]].
Expand All @@ -541,7 +547,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 +557,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
25 changes: 24 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,26 @@

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. */
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 +54,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 @@ -219,7 +242,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