cloud-fan commented on code in PR #59857:
URL: https://github.com/apache/spark/pull/59857#discussion_r4508843536
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/CacheManager.scala:
##########
@@ -377,11 +377,19 @@ 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.
+ * 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.collectFirstWithSubqueries { case p if f(p) => () }.isDefined
Review Comment:
**Nit (P3):** When the main-plan walk misses, `collectFirstWithSubqueries`
builds `subqueriesAll`, a second walk over every node, even for a plan with no
subquery expression. Most cache entries don't reference the written table or
path, so each write through these two routes now walks those entries' plans
about twice on the driver. Tree-pattern bits propagate from expressions and
children, so a root `PLAN_EXPRESSION` check proves there are no subqueries, the
same pruning `useCachedData` already uses for its subquery pass. This keeps the
result and the visit order, including the single `lookupAndRefresh` side effect:
```suggestion
plan.exists(f) ||
(plan.containsPattern(PLAN_EXPRESSION) &&
plan.subqueriesAll.exists(_.exists(f)))
```
**Verification:**
- **Inspection:** The helper checks the root's PLAN_EXPRESSION bit before
collecting subquery plans, and its main-plan phase is a plain short-circuiting
walk.
- **Compatibility:** Name-route and path-route cache tests, including those
for a pinned main read with a live subquery read and for mixed pinned and live
main-plan reads, keep their invalidation outcomes and refresh counts.
--
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]