joelrobin18 commented on code in PR #59857:
URL: https://github.com/apache/spark/pull/59857#discussion_r4550597599
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/CacheManager.scala:
##########
@@ -620,56 +634,59 @@ class CacheManager extends Logging with
AdaptiveSparkPlanHelper {
val qualifiedPath = fs.makeQualified(resourcePath)
recacheByCondition(
spark,
- _.plan.exists(lookupAndRefresh(_, fs, qualifiedPath, includeTimeTravel)))
+ entry => lookupAndRefresh(entry.plan, fs, qualifiedPath,
includeTimeTravel))
}
/**
- * Traverses a given `plan` and searches for the occurrences of
`qualifiedPath` in the
- * [[org.apache.spark.sql.execution.datasources.FileIndex]] of any
[[HadoopFsRelation]] nodes
- * in the plan. If found, we refresh the metadata and return true.
Otherwise, this method returns
- * false.
+ * Searches `plan`, including its subqueries, for the
+ * [[org.apache.spark.sql.execution.datasources.FileIndex]] of file-backed
relations under
+ * `qualifiedPath`, refreshes each distinct matching index once, and returns
whether any was
+ * found. Every match is refreshed because the rebuilt cache entry reuses
these indexes.
*/
private def lookupAndRefresh(
plan: LogicalPlan,
fs: FileSystem,
qualifiedPath: Path,
includeTimeTravel: Boolean): Boolean = {
- plan match {
- case lr: LogicalRelation => lr.relation match {
- case hr: HadoopFsRelation =>
- refreshFileIndexIfNecessary(hr.location, fs, qualifiedPath,
includeTimeTravel)
- case _ => false
- }
+ def fileIndexToRefresh(node: LogicalPlan): Option[FileIndex] = {
+ val fileIndex: Option[FileIndex] = node match {
+ case lr: LogicalRelation => lr.relation match {
+ case hr: HadoopFsRelation => Some(hr.location)
+ case _ => None
+ }
- case relation @ ExtractV2Table(fileTable: FileTable)
- if includeTimeTravel || relation.timeTravelSpec.isEmpty =>
- refreshFileIndexIfNecessary(
- fileTable.fileIndex,
- fs,
- qualifiedPath,
- includeTimeTravel)
+ case relation @ ExtractV2Table(fileTable: FileTable)
+ if includeTimeTravel || relation.timeTravelSpec.isEmpty =>
+ Some(fileTable.fileIndex)
- case _ => false
+ case _ => None
+ }
+ fileIndex.filter(needToRefresh(_, fs, qualifiedPath, includeTimeTravel))
}
+
+ // Deduplicate by reference: InMemoryFileIndex equality compares root
paths only.
+ val fileIndexes = (plan +:
subqueryPlans(plan)).flatMap(_.flatMap(fileIndexToRefresh))
Review Comment:
I made this change in 1ee1eca.
`recacheByPath` now keeps one set of the file indexes that it refreshed
during the call. The set compares instances by reference, because
`InMemoryFileIndex` equality compares only root paths. `lookupAndRefresh`
refreshes only the indexes that are not in the set, and adds them to it. An
entry is still selected when it reads an index that an earlier entry refreshed.
The set exists only for one call.
New tests in `V1TimeTravelCacheSuite`:
- Entry A reads index X in the main plan and index Y in a subquery. Entry B
reads Y in the main plan. One write rebuilds both entries, and refreshes X and
Y one time each.
- Two cached queries read the same live index only in subqueries. One write
rebuilds both entries, and refreshes the index one time.
The existing path-route tests keep their refresh counts. With one set for
each entry, the two new tests fail.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/CacheManager.scala:
##########
@@ -369,11 +369,25 @@ class CacheManager extends Logging with
AdaptiveSparkPlanHelper {
includeTimeTravel: Boolean = true): Unit = {
val resolver = spark.sessionState.conf.resolver
def shouldInvalidate(entry: CachedData): Boolean = {
- entry.plan.exists(isMatchedTableOrView(_, name, resolver,
includeTimeTravel))
+ existsWithSubqueries(entry.plan)(isMatchedTableOrView(_, name, resolver,
includeTimeTravel))
}
recacheByCondition(spark, shouldInvalidate)
}
+ /**
+ * Returns whether `f` holds for any node of `plan`, including nodes inside
subquery expressions,
+ * so that an entry reading the target only inside a subquery is selected
like one reading it in
+ * the main plan. For example, write-driven invalidation skips time-travel
relations, so an entry
+ * that reads a table at a pinned version and live only inside a subquery
must still match
+ * through the subquery.
+ */
+ private def existsWithSubqueries(plan: LogicalPlan)(f: LogicalPlan =>
Boolean): Boolean =
+ plan.exists(f) || subqueryPlans(plan).exists(_.exists(f))
+
+ /** All subquery plans of `plan`, including nested ones, or none if it has
no subquery. */
+ private def subqueryPlans(plan: LogicalPlan): Seq[LogicalPlan] =
+ if (plan.containsPattern(PLAN_EXPRESSION)) plan.subqueriesAll else Nil
Review Comment:
I added tests for nested subqueries in 1ee1eca:
- `V1TimeTravelCacheSuite`: a query reads the live index in a subquery of a
subquery. A write-driven `recacheByPath` clears the cache and refreshes the
index one time.
- `CachedTableSuite`: the `REFRESH TABLE` test and the cascading
`uncacheTableOrView` test now also use a query that reads the table in a
subquery of a subquery.
If `subqueryPlans` uses `plan.flatMap(_.subqueries)`, these three nested
cases fail.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]