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
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ package org.apache.spark.sql.execution

import java.util.concurrent.locks.ReentrantReadWriteLock

import org.apache.hadoop.fs.{FileSystem, Path}

import org.apache.spark.internal.Logging
import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan
import org.apache.spark.sql.execution.columnar.InMemoryRelation
import org.apache.spark.sql.Dataset
import org.apache.spark.sql.execution.datasources.{HadoopFsRelation, LogicalRelation}
import org.apache.spark.storage.StorageLevel
import org.apache.spark.storage.StorageLevel.MEMORY_AND_DISK

Expand Down Expand Up @@ -157,4 +160,25 @@ private[sql] class CacheManager extends Logging {
case _ =>
}
}

/**
* Invalidates the cache of any data that contains `qualifiedPath` in one or more
* `HadoopFsRelation` node(s) as part of its logical plan.
*/
private[sql] def invalidateCachedPath(fs: FileSystem, qualifiedPath: Path): Unit = writeLock {
cachedData.foreach {
case data if data.plan.find {
case lr: LogicalRelation => lr.relation match {
case hr: HadoopFsRelation =>
hr.location.paths
.map(_.makeQualified(fs.getUri, fs.getWorkingDirectory))
.contains(qualifiedPath)
}
}.isDefined =>
val dataIndex = cachedData.indexWhere(cd => data.plan.sameResult(cd.plan))
data.cachedRepresentation.uncache(blocking = false)
cachedData.remove(dataIndex)
case _ =>
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ private[sql] case class InsertIntoHadoopFsRelationCommand(
throw new IOException(s"Unable to clear output " +
s"directory $qualifiedOutputPath prior to writing to it")
}
// Invalidate all caches with this in path
sparkSession.sharedState.cacheManager.invalidateCachedPath(fs, qualifiedOutputPath)
true
case (SaveMode.Append, _) | (SaveMode.Overwrite, _) | (SaveMode.ErrorIfExists, false) =>
// Invalidate all caches with this in path
sparkSession.sharedState.cacheManager.invalidateCachedPath(fs, qualifiedOutputPath)
true
case (SaveMode.Ignore, exists) =>
!exists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ class ParquetQuerySuite extends QueryTest with ParquetTest with SharedSQLContext
TableIdentifier("tmp"), ignoreIfNotExists = true)
}

test("drop cache on overwrite") {
withTempDir { dir =>
val path = dir.toString
spark.range(1000).write.mode("overwrite").parquet(path)
val df = sqlContext.read.parquet(path).cache()
assert(df.count() == 1000)
sqlContext.range(10).write.mode("overwrite").parquet(path)

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.

sqlContext -> spark

assert(sqlContext.read.parquet(path).count() == 10)
}
}

test("drop cache on append") {
withTempDir { dir =>
val path = dir.toString
spark.range(1000).write.mode("append").parquet(path)
val df = sqlContext.read.parquet(path).cache()
assert(df.count() == 1000)
sqlContext.range(10).write.mode("append").parquet(path)

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.

sqlContext -> spark

assert(sqlContext.read.parquet(path).count() == 1010)
}
}

test("self-join") {
// 4 rows, cells of column 1 of row 2 and row 4 are null
val data = (1 to 4).map { i =>
Expand Down