Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
12 changes: 12 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 @@ -2307,6 +2307,18 @@ class Dataset[T] private[sql](
this
}

/**
* Get the Dataset's current storage level, or StorageLevel.NONE if not persisted.
*
* @group basic
* @since 2.0.0
*/
def storageLevel(): StorageLevel = {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

remove parenthesis?

sparkSession.sharedState.cacheManager.lookupCachedData(this).map { cachedData =>
cachedData.cachedRepresentation.storageLevel
}.getOrElse(StorageLevel.NONE)
}

/**
* Mark the Dataset as non-persistent, and remove all blocks for it from memory and disk.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,32 @@ import scala.language.postfixOps

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


class DatasetCacheSuite extends QueryTest with SharedSQLContext {
import testImplicits._

test("get storage level") {
val ds1 = Seq("1", "2").toDS().as("a")
val ds2 = Seq(2, 3).toDS().as("b")

// default storage level
ds1.persist()
ds2.cache()
assert(ds1.storageLevel() == StorageLevel.MEMORY_AND_DISK)
assert(ds2.storageLevel() == StorageLevel.MEMORY_AND_DISK)
// unpersist
ds1.unpersist()
assert(ds1.storageLevel() == StorageLevel.NONE)
// non-default storage level
ds1.persist(StorageLevel.MEMORY_ONLY_2)

@gatorsmile gatorsmile Jun 20, 2016

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

When writing black-box testing, I might just try all the levels in the test case. Even we can include some customized StorageLevel, which is different from the defined one.

    import org.apache.spark.storage.StorageLevel._
    Seq(NONE, DISK_ONLY, DISK_ONLY_2, MEMORY_ONLY, MEMORY_ONLY_2, MEMORY_ONLY_SER,
      MEMORY_ONLY_SER_2, MEMORY_AND_DISK, MEMORY_AND_DISK_2, MEMORY_AND_DISK_SER,
      MEMORY_AND_DISK_SER_2, OFF_HEAP).foreach { level =>
      ds1.persist(level)
      assert(ds1.storageLevel() == level)
      ds1.unpersist()
      assert(ds1.storageLevel() == StorageLevel.NONE)
    }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm kinda neutral on this - it doesn't really seem necessary to me, since pretty much by definition if one storage level works then they all do.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I knew. : ) That is white box testing. Normally, writing test cases should not be done by the same person who wrote the code.

assert(ds1.storageLevel() == StorageLevel.MEMORY_ONLY_2)
// joined Dataset should not be persisted
val joined = ds1.joinWith(ds2, $"a.value" === $"b.value")
assert(joined.storageLevel() == StorageLevel.NONE)
}

test("persist and unpersist") {
val ds = Seq(("a", 1), ("b", 2), ("c", 3)).toDS().select(expr("_2 + 1").as[Int])
val cached = ds.cache()
Expand All @@ -39,8 +60,7 @@ class DatasetCacheSuite extends QueryTest with SharedSQLContext {
2, 3, 4)
// Drop the cache.
cached.unpersist()
assert(spark.sharedState.cacheManager.lookupCachedData(cached).isEmpty,
"The Dataset should not be cached.")
assert(cached.storageLevel() == StorageLevel.NONE, "The Dataset should not be cached.")
}

test("persist and then rebind right encoder when join 2 datasets") {
Expand All @@ -57,11 +77,9 @@ class DatasetCacheSuite extends QueryTest with SharedSQLContext {
assertCached(joined, 2)

ds1.unpersist()
assert(spark.sharedState.cacheManager.lookupCachedData(ds1).isEmpty,
"The Dataset ds1 should not be cached.")
assert(ds1.storageLevel() == StorageLevel.NONE, "The Dataset ds1 should not be cached.")
ds2.unpersist()
assert(spark.sharedState.cacheManager.lookupCachedData(ds2).isEmpty,
"The Dataset ds2 should not be cached.")
assert(ds2.storageLevel() == StorageLevel.NONE, "The Dataset ds2 should not be cached.")
}

test("persist and then groupBy columns asKey, map") {
Expand All @@ -76,10 +94,8 @@ class DatasetCacheSuite extends QueryTest with SharedSQLContext {
assertCached(agged.filter(_._1 == "b"))

ds.unpersist()
assert(spark.sharedState.cacheManager.lookupCachedData(ds).isEmpty,
"The Dataset ds should not be cached.")
assert(ds.storageLevel() == StorageLevel.NONE, "The Dataset ds should not be cached.")
agged.unpersist()
assert(spark.sharedState.cacheManager.lookupCachedData(agged).isEmpty,
"The Dataset agged should not be cached.")
assert(agged.storageLevel() == StorageLevel.NONE, "The Dataset agged should not be cached.")
}
}