Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-11914] [SQL] Support coalesce and repartition in Dataset APIs #9899

Closed
wants to merge 2 commits into from
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
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
15 changes: 15 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,21 @@ 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