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
Support coalesce and repartition in Dataset APIs
  • Loading branch information
gatorsmile committed Nov 23, 2015
commit 41d3adecfa1dbbab8165587d38f99036e443b465
19 changes: 19 additions & 0 deletions sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,25 @@ class Dataset[T] private[sql](
*/
def count(): Long = toDF().count()

/**
* Returns a new [[Dataset]] that has exactly `numPartitions` partitions.
* @since 1.6.0
*/
def repartition(numPartitions: Int): Dataset[T] = withPlan {
Repartition(numPartitions, shuffle = true, _)
}

/**
* Returns a new [[Dataset]] that has exactly `numPartitions` partitions.
* Similar to coalesce defined on an [[RDD]], this operation results in a narrow dependency, e.g.
* if you go from 1000 partitions to 100 partitions, there will not be a shuffle, instead each of
* the 100 new partitions will claim 10 of the current partitions.
* @since 1.6.0
*/
def coalesce(numPartitions: Int): Dataset[T] = withPlan {
Repartition(numPartitions, shuffle = false, _)
}

/* *********************** *
* Functional Operations *
* *********************** */
Expand Down
13 changes: 13 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/DatasetSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ class DatasetSuite extends QueryTest with SharedSQLContext {
assert(ds.takeAsList(1).get(0) == item)
}

test("coalesce, repartition") {
val data = (1 to 100).map(i => ClassData(i.toString, i))
val ds = data.toDS()

assert(ds.repartition(10).rdd.partitions.length == 10)
checkAnswer( ds.repartition(10),
data: _*)

assert(ds.coalesce(1).rdd.partitions.length == 1)
checkAnswer( ds.coalesce(1),
data: _*)
}

test("as tuple") {
val data = Seq(("a", 1), ("b", 2)).toDF("a", "b")
checkAnswer(
Expand Down