forked from apache/spark
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-11905][SQL] Support Persist/Cache and Unpersist in Dataset APIs
Persist and Unpersist exist in both RDD and Dataframe APIs. I think they are still very critical in Dataset APIs. Not sure if my understanding is correct? If so, could you help me check if the implementation is acceptable? Please provide your opinions. marmbrus rxin cloud-fan Thank you very much! Author: gatorsmile <gatorsmile@gmail.com> Author: xiaoli <lixiao1983@gmail.com> Author: Xiao Li <xiaoli@Xiaos-MacBook-Pro.local> Closes apache#9889 from gatorsmile/persistDS.
- Loading branch information
1 parent
fd95eea
commit 0a7bca2
Showing
6 changed files
with
162 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
sql/core/src/test/scala/org/apache/spark/sql/DatasetCacheSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql | ||
|
||
import scala.language.postfixOps | ||
|
||
import org.apache.spark.sql.functions._ | ||
import org.apache.spark.sql.test.SharedSQLContext | ||
|
||
|
||
class DatasetCacheSuite extends QueryTest with SharedSQLContext { | ||
import testImplicits._ | ||
|
||
test("persist and unpersist") { | ||
val ds = Seq(("a", 1) , ("b", 2), ("c", 3)).toDS().select(expr("_2 + 1").as[Int]) | ||
val cached = ds.cache() | ||
// count triggers the caching action. It should not throw. | ||
cached.count() | ||
// Make sure, the Dataset is indeed cached. | ||
assertCached(cached) | ||
// Check result. | ||
checkAnswer( | ||
cached, | ||
2, 3, 4) | ||
// Drop the cache. | ||
cached.unpersist() | ||
assert(!sqlContext.isCached(cached), "The Dataset should not be cached.") | ||
} | ||
|
||
test("persist and then rebind right encoder when join 2 datasets") { | ||
val ds1 = Seq("1", "2").toDS().as("a") | ||
val ds2 = Seq(2, 3).toDS().as("b") | ||
|
||
ds1.persist() | ||
assertCached(ds1) | ||
ds2.persist() | ||
assertCached(ds2) | ||
|
||
val joined = ds1.joinWith(ds2, $"a.value" === $"b.value") | ||
checkAnswer(joined, ("2", 2)) | ||
assertCached(joined, 2) | ||
|
||
ds1.unpersist() | ||
assert(!sqlContext.isCached(ds1), "The Dataset ds1 should not be cached.") | ||
ds2.unpersist() | ||
assert(!sqlContext.isCached(ds2), "The Dataset ds2 should not be cached.") | ||
} | ||
|
||
test("persist and then groupBy columns asKey, map") { | ||
val ds = Seq(("a", 10), ("a", 20), ("b", 1), ("b", 2), ("c", 1)).toDS() | ||
val grouped = ds.groupBy($"_1").keyAs[String] | ||
val agged = grouped.mapGroups { case (g, iter) => (g, iter.map(_._2).sum) } | ||
agged.persist() | ||
|
||
checkAnswer( | ||
agged.filter(_._1 == "b"), | ||
("b", 3)) | ||
assertCached(agged.filter(_._1 == "b")) | ||
|
||
ds.unpersist() | ||
assert(!sqlContext.isCached(ds), "The Dataset ds should not be cached.") | ||
agged.unpersist() | ||
assert(!sqlContext.isCached(agged), "The Dataset agged should not be cached.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters