joelrobin18 commented on code in PR #59857:
URL: https://github.com/apache/spark/pull/59857#discussion_r4541092534


##########
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 =

Review Comment:
   I made this change in fa4bfcb.
   
   - `recacheByPlan` now uses `existsWithSubqueries` for each cache entry.
   - The cascade branch of `uncacheByCondition` also uses it.
   - The non-cascade recompile branch did not change, as you recommended.
   
   The scaladoc of the helper now gives the general reason. An entry that reads 
the target only in a subquery must match like an entry that reads it in the 
main plan. The time-travel case is one example.
   
   I added two tests to `CachedTableSuite`:
   
   - `REFRESH TABLE` on a V1 table rebuilds a cached query that reads the table 
only in a subquery.
   - An `uncacheTableOrView` call with `cascade = true` removes a cached query 
that reads the table only in a subquery.
   
   Both tests pass in CI, in the "sql - slow tests" job.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/CacheManager.scala:
##########
@@ -636,7 +644,9 @@ class CacheManager extends Logging with 
AdaptiveSparkPlanHelper {
     val qualifiedPath = fs.makeQualified(resourcePath)
     recacheByCondition(
       spark,
-      _.plan.exists(lookupAndRefresh(_, fs, qualifiedPath, includeTimeTravel)))
+      entry => existsWithSubqueries(entry.plan) {

Review Comment:
   I made this change in fa4bfcb.
   
   `lookupAndRefresh` now examines all nodes of the main plan and of each 
subquery plan. It does not stop at the first match. It collects each distinct 
live `FileIndex` under the path and refreshes each index one time. It returns 
true if it finds one or more indexes.
   
   - The time-travel skip is still a rule for each node.
   - The walk goes into subquery plans only if the root has `PLAN_EXPRESSION`.
   - The code removes duplicate indexes by reference, because 
`InMemoryFileIndex` equality compares only the root paths.
   
   I added these tests to `V1TimeTravelCacheSuite`:
   
   - A live index in the main plan and a live index in a subquery: each index 
refreshes one time.
   - Live indexes in two subqueries and a pinned index in the main plan: each 
live index refreshes one time. The pinned index does not refresh.
   - One index in the main plan and in a subquery: the index refreshes one time.
   - Two path reads over Parquet files, one of them in an `IN` subquery: after 
a V1 append and a V1 overwrite, the cached query shows the new data.
   
   All of these tests pass in CI.



##########
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:
   I made this change in fa4bfcb.
   
   A new `subqueryPlans` helper returns `plan.subqueriesAll` only if the root 
has the `PLAN_EXPRESSION` bit. If the root does not have the bit, the helper 
returns no plans. `existsWithSubqueries` is now `plan.exists(f) || 
subqueryPlans(plan).exists(_.exists(f))`, as you recommended. The path route 
uses the same helper. A plan without a subquery now gets only one walk on both 
routes.
   
   The name-route and path-route tests keep the same results and refresh counts.



##########
sql/core/src/test/scala/org/apache/spark/sql/V1TimeTravelCacheSuite.scala:
##########
@@ -305,6 +331,33 @@ class V1TimeTravelCacheSuite extends QueryTest with 
SharedSparkSession {
     }
   }
 
+  /**
+   * Caches a query that reads the table at a pinned version and reads it live 
only inside an IN
+   * subquery, then passes the materialized cache and both file indexes to `f`.

Review Comment:
   I made this change in fa4bfcb. The scaladoc now lists the cached Dataset, 
the root path, and the two file indexes. This is the same order as the 
parameters of the callback.



-- 
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]

Reply via email to