cloud-fan commented on code in PR #55536: URL: https://github.com/apache/spark/pull/55536#discussion_r3310986846
########## sql/core/src/test/scala/org/apache/spark/sql/connector/DSv2CacheTableReadTests.scala: ########## @@ -0,0 +1,236 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql.connector + +import org.apache.spark.sql.{Row, SparkSession} +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.connector.catalog.{CachingInMemoryTableCatalog, Column, InMemoryTableCatalog, TableChange, TableInfo} +import org.apache.spark.sql.types.IntegerType + +/** + * Shared CACHE TABLE impact on reads tests for DSv2 tables. Write operations ignore the + * cache, so these tests verify how reads behave when a cached table is mutated by session + * SQL or external catalog API calls: + * + * - Scenario 1 (external write): cache pins the read, external write invisible until REFRESH. + * - Scenario 2 (session write then external write): session write rebuilds cache, + * subsequent external write invisible until REFRESH. + * - Scenario 3 (external schema change): cache pinned at original schema until REFRESH. + * - Scenario 4 (session schema change then external write): session ALTER rebuilds + * cache, subsequent external write invisible until REFRESH. + * - Scenario 5 (external drop and recreate table): query sees new empty table. + * + * Scenarios 1 through 4 do not include `cachingcat` (caching connector) variants because the + * CacheManager pins reads regardless of the connector, and REFRESH TABLE clears both layers, + * so behavior is the same. Scenario 5 (drop and recreate) includes a `cachingcat` variant + * because it differs: [[CachingInMemoryTableCatalog]] does not invalidate on drop/create, so + * `loadTable` still returns the old cached table object, CacheManager still matches, and stale + * data is served until REFRESH TABLE. + * Review Comment: Reconsidering the previous-round L36 suggestion now that I see the docs-only resolution: a `cachingcat` Scenario 1 test (~15 lines following the established peer-trait pattern — see `DSv2RepeatedTableAccessTests.scala` lines 75–93 and `DSv2TempViewWithStoredPlanTests.scala` lines 71–91) is genuinely the higher-value path here. It would self-verify the parity claim instead of asserting it in prose, and importantly it would catch the Scenario 5 edge case where the claim does **not** actually hold: - `CachingInMemoryTableCatalog` does not invalidate its `cachedTables` on `dropTable` / `createTable` (per its own Scaladoc: "[[dropTable]], [[createTable]], and [[alterTable]] do not invalidate the cache, matching the behavior of real caching connectors"). - So in a hypothetical cachingcat Scenario 5, `session.table(testTable)` post-drop would return the **stale old** `InMemoryTable` reference via `cachedTables.computeIfAbsent`. The resolved plan would still match the cached plan, so `session.catalog.isCached(testTable)` would return **true** (this trait's L192 currently asserts `false`), and the rows read pre-`REFRESH` would still be `Row(1, 100)` rather than `Seq.empty`. So the current Scaladoc rationale is overbroad — Scenarios 1–4 have parity with cachingcat, but Scenario 5 diverges. A cachingcat Scenario 1 test would also lock down the two-layer REFRESH behavior (`RefreshTableExec` calls both `TableCatalog#invalidateTable` and `refreshCache()`) against future regressions, which is the actually interesting interaction at this layer. Separately, the previous-round L36 also raised the question of a session-side counterpart for Scenario 5 (peer trait `DSv2TempViewWithStoredPlanTests` has both 4.1 session drop+recreate and 4.2 external; this trait has only the external version). That sub-question is currently unanswered — worth either adding the session counterpart or explicitly noting the scope decision alongside the cachingcat note in the Scaladoc. -- 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]
