You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "yyanyy (via GitHub)" <gi...@apache.org> on 2026/09/02 01:40:04 UTC
[PR] [WIP][SPARK-59163][SQL] Reuse projected table-state options [spark]
yyanyy opened a new pull request, #58462:
URL: https://github.com/apache/spark/pull/58462
### What changes were proposed in this pull request?
This is tracked by [SPARK-59163](https://issues.apache.org/jira/browse/SPARK-59163). It follows
#57585 and completes the table-state option reuse requested in
[this review comment](https://github.com/apache/spark/pull/57585#discussion_r3819899362). The
earlier PR introduced the projection and has already landed; this PR is its subsequent internal
cleanup.
`RelationResolution` already computed the projected table-state options for `TableCacheKey`, but
table loading projected the full option map again. The projected map passed to
`CatalogV2Util.lookupCachedRelation` was also projected a second time inside that method.
This PR:
- as a minor refactoring, adds consistently named `*WithStateOptions` entry points and makes the
existing full-option helpers project once before delegating to them;
- reuses `TableCacheKey.stateOptions` for shared relation cache lookup in both persistent relation
and `V2TableReference` resolution, without repeating the projection for table loading;
- reuses the single projection in `V2TableRefreshUtil` for refresh deduplication, shared cache
lookup, and catalog loading.
### Why are the changes needed?
The previous flow repeatedly normalized the catalog's state-option key set and materialized a full
projection during an uncached relation resolution or execution refresh. The new flow computes that
projection once and reuses the same map for Spark's table pin, catalog load, and shared cache
lookup.
### Does this PR introduce _any_ user-facing change?
No.
### How was this patch tested?
The existing `CatalogV2UtilSuite` and `DataSourceV2OptionSuite` cover table-state option filtering,
persistent relation resolution, `V2TableReference` resolution, shared relation cache matching, and
execution refresh. No call-count assertions were added for this implementation-only cleanup.
```bash
XDG_RUNTIME_DIR=/tmp/yan-sbt-runtime \
JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 \
DEFAULT_ARTIFACT_REPOSITORY=https://maven-proxy.cloud.databricks.com \
MAVEN_MIRROR_URL=https://maven-proxy.cloud.databricks.com \
build/sbt \
'catalyst/testOnly org.apache.spark.sql.connector.catalog.CatalogV2UtilSuite' \
'sql/testOnly org.apache.spark.sql.connector.DataSourceV2OptionSuite'
```
`CatalogV2UtilSuite` ran 19/19 tests and `DataSourceV2OptionSuite` ran 56/56 tests, with no failures,
cancellations, ignores, or pending tests. No test-case filters were used within the suites; the
run used SBT's incremental compilation cache. `DataSourceV2OptionSuite` emitted its existing
non-failing possible thread-leak warning.
```bash
XDG_RUNTIME_DIR=/tmp/yan-sbt-runtime \
JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 \
DEFAULT_ARTIFACT_REPOSITORY=https://maven-proxy.cloud.databricks.com \
MAVEN_MIRROR_URL=https://maven-proxy.cloud.databricks.com \
build/sbt \
'catalyst / Compile / scalastyle' \
'catalyst / Test / scalastyle' \
'sql / Compile / scalastyle' \
'sql / Test / scalastyle' \
'catalyst / Compile / checkstyle'
```
The four scalastyle tasks processed 718 Catalyst main files, 440 Catalyst test files, 826 SQL core
main files, and 1,106 SQL core test files with no errors or warnings. Catalyst main checkstyle also
completed with no issues.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: OpenAI Codex
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-59163][SQL] Reuse projected table-state options [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #58462:
URL: https://github.com/apache/spark/pull/58462#discussion_r3910472199
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogV2Util.scala:
##########
@@ -510,13 +525,26 @@ private[sql] object CatalogV2Util {
timeTravelSpec: Option[TimeTravelSpec] = None,
writePrivilegesString: Option[String] = None,
options: CaseInsensitiveStringMap = CaseInsensitiveStringMap.empty()): Table = {
+ val stateOptions = extractTableStateOptions(catalog, options)
+ getTableWithStateOptions(
+ catalog, ident, stateOptions, timeTravelSpec, writePrivilegesString)
+ }
+
+ /**
+ * Loads a table using table-state options already projected by the caller.
+ */
+ def getTableWithStateOptions(
+ catalog: CatalogPlugin,
+ ident: Identifier,
+ stateOptions: CaseInsensitiveStringMap,
+ timeTravelSpec: Option[TimeTravelSpec] = None,
+ writePrivilegesString: Option[String] = None): Table = {
val timeTravel: TimeTravel = timeTravelSpec match {
case Some(v: AsOfVersion) => new TimeTravel.AsOfVersion(v.version)
case Some(ts: AsOfTimestamp) => new TimeTravel.AsOfTimestamp(ts.timestamp)
case None => null
}
val context = new TableContext(timeTravel, parseWritePrivileges(writePrivilegesString))
- val stateOptions = extractTableStateOptions(catalog, options)
catalog.asTableCatalog.loadTable(ident, context, stateOptions)
Review Comment:
**Blocking (P1):** This passes the same `stateOptions` instance that Spark retains in `TableCacheKey`/`currentTables` to arbitrary catalog code. Although direct mutators throw, `CaseInsensitiveStringMap` exposes mutable live `keySet`, `values`, and `entrySet` views, so a connector can change the map during or after loading and corrupt the hash/key Spark relies on. That can produce cache misses or let a table loaded for one branch be reused for another. Please keep Spark's projected map private and pass `loadTable` a defensive copy.
**Recommended change:** Keep the caller-projected map as Spark-private state and construct a defensive CaseInsensitiveStringMap copy immediately before invoking TableCatalog.loadTable; add a focused mutation regression test.
**Why this works:** External catalog code receives an equivalent copy, so mutation through a collection view cannot alter the map used for table pinning, refresh deduplication, or shared-cache lookup.
**Scope:** CatalogV2Util.getTableWithStateOptions and a focused Catalyst catalog test.
**Compatibility:** Catalogs receive the same option contents and case-insensitive lookup behavior, public signatures remain unchanged, and callers still perform only one projection.
**Risks:** Adds one small map copy at the external catalog boundary.
**Constraints:** Retain the original projected map for every Spark-owned cache and deduplication key. Do not expose that original instance to the connector callback.
**Success:** A catalog that mutates the supplied options through a collection view cannot change Spark's retained state key, and subsequent same-state and different-state lookups remain correct.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogV2Util.scala:
##########
@@ -604,18 +632,22 @@ private[sql] object CatalogV2Util {
loadTable(catalog, ident).map(DataSourceV2Relation.create(_, Some(catalog), Some(ident)))
}
- def lookupCachedRelation(
+ /**
+ * Looks up a cached relation using table-state options already projected by the caller.
+ * Reusing the projection keeps table pinning and shared-cache lookup on the same state key.
+ */
+ def lookupCachedRelationWithStateOptions(
cache: RelationCache,
catalog: CatalogPlugin,
ident: Identifier,
table: Table,
- options: CaseInsensitiveStringMap,
+ stateOptions: CaseInsensitiveStringMap,
conf: SQLConf): Option[DataSourceV2Relation] = {
cache.lookup(
catalog,
ident,
Some(table.id),
- extractTableStateOptions(catalog, options),
+ stateOptions,
Review Comment:
**Non-blocking (P2):** The existing tests verify the projected contents and final cache/load results, but none counts `tableStateOptionKeys` calls. Replacing this already-projected path with a full-options wrapper would therefore keep every assertion green while reintroducing the repeated projection this PR is meant to remove. Please use a resettable counting catalog in `PlanResolutionSuite` and `DataSourceV2OptionSuite` to assert one projection per resolution or refresh boundary alongside the existing semantic checks.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-59163][SQL] Reuse projected options and make CaseInsensitiveStringMap read-only [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #58462: [SPARK-59163][SQL] Reuse projected options and make CaseInsensitiveStringMap read-only
URL: https://github.com/apache/spark/pull/58462
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-59163][SQL] Reuse projected options and make CaseInsensitiveStringMap read-only [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #58462:
URL: https://github.com/apache/spark/pull/58462#issuecomment-5570911667
**Merge Summary:**
- merged into master https://github.com/apache/spark/commit/4dc04afa85a97daa1b8e12ee3f222687fd25f82d
- merged into branch-4.x https://github.com/apache/spark/commit/64a7dca770479b7943d36745e9d06d6cd704e2f8
- merged into branch-4.3 https://github.com/apache/spark/commit/74197c98452d677eb90551c93a25e2cd2d3acba4
*Posted by `merge_spark_pr.py`*
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-59163][SQL] Reuse projected options and make CaseInsensitiveStringMap read-only [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #58462:
URL: https://github.com/apache/spark/pull/58462#discussion_r3945868089
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogV2Util.scala:
##########
@@ -510,13 +525,26 @@ private[sql] object CatalogV2Util {
timeTravelSpec: Option[TimeTravelSpec] = None,
writePrivilegesString: Option[String] = None,
options: CaseInsensitiveStringMap = CaseInsensitiveStringMap.empty()): Table = {
+ val stateOptions = extractTableStateOptions(catalog, options)
+ getTableWithStateOptions(
+ catalog, ident, stateOptions, timeTravelSpec, writePrivilegesString)
+ }
+
+ /**
+ * Loads a table using table-state options already projected by the caller.
+ */
+ def getTableWithStateOptions(
+ catalog: CatalogPlugin,
+ ident: Identifier,
+ stateOptions: CaseInsensitiveStringMap,
+ timeTravelSpec: Option[TimeTravelSpec] = None,
+ writePrivilegesString: Option[String] = None): Table = {
val timeTravel: TimeTravel = timeTravelSpec match {
case Some(v: AsOfVersion) => new TimeTravel.AsOfVersion(v.version)
case Some(ts: AsOfTimestamp) => new TimeTravel.AsOfTimestamp(ts.timestamp)
case None => null
}
val context = new TableContext(timeTravel, parseWritePrivileges(writePrivilegesString))
- val stateOptions = extractTableStateOptions(catalog, options)
catalog.asTableCatalog.loadTable(ident, context, stateOptions)
Review Comment:
The current approach is fine. Making CaseInsensitiveStringMap's normalized delegate unmodifiable closes the mutation channel at the owning abstraction, so the same projected instance can safely serve catalog loading and Spark's retained keys without a defensive copy.
<!-- SPARK_DEV_REVIEW_REPLY {"feedback_id":"inline:3910472199","thread_id":"inline:3910472199","verdict_sha256":"e9606abc098fbfd7cc7591eebb4e2ba0e62a1d013580a55ead37a58bcc7ffc64"} -->
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-59163][SQL] Reuse projected table-state options [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #58462:
URL: https://github.com/apache/spark/pull/58462#discussion_r3920864969
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogV2Util.scala:
##########
@@ -510,14 +525,31 @@ private[sql] object CatalogV2Util {
timeTravelSpec: Option[TimeTravelSpec] = None,
writePrivilegesString: Option[String] = None,
options: CaseInsensitiveStringMap = CaseInsensitiveStringMap.empty()): Table = {
+ val stateOptions = extractTableStateOptions(catalog, options)
+ getTableWithStateOptions(
+ catalog, ident, stateOptions, timeTravelSpec, writePrivilegesString)
+ }
+
+ /**
+ * Loads a table using table-state options already projected by the caller.
+ */
+ def getTableWithStateOptions(
+ catalog: CatalogPlugin,
+ ident: Identifier,
+ stateOptions: CaseInsensitiveStringMap,
+ timeTravelSpec: Option[TimeTravelSpec] = None,
+ writePrivilegesString: Option[String] = None): Table = {
val timeTravel: TimeTravel = timeTravelSpec match {
case Some(v: AsOfVersion) => new TimeTravel.AsOfVersion(v.version)
case Some(ts: AsOfTimestamp) => new TimeTravel.AsOfTimestamp(ts.timestamp)
case None => null
}
val context = new TableContext(timeTravel, parseWritePrivileges(writePrivilegesString))
- val stateOptions = extractTableStateOptions(catalog, options)
- catalog.asTableCatalog.loadTable(ident, context, stateOptions)
+ // Callers may retain stateOptions as a cache key. CaseInsensitiveStringMap exposes mutable
+ // collection views, so do not let catalog code mutate the retained instance.
+ val catalogStateOptions =
+ new CaseInsensitiveStringMap(stateOptions.asCaseSensitiveMap())
Review Comment:
**Blocking (P1):** `stateOptions.asCaseSensitiveMap()` can contain both original spellings for a case-insensitive key, while lookup and equality use one normalized delegate value. Reconstructing from the case-sensitive map replays those duplicates in a fresh map order, so the copy can choose a different effective value. A Connect named-table read can carry both `snapshot` and `SNAPSHOT`; Spark may then load one snapshot but cache it under the other state key, allowing a later unambiguous reference to reuse the wrong table. Please copy from the effective entries (for example, `new CaseInsensitiveStringMap(stateOptions)`) and add a duplicate-case regression.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-59163][SQL] Reuse projected table-state options [spark]
Posted by "yyanyy (via GitHub)" <gi...@apache.org>.
yyanyy commented on code in PR #58462:
URL: https://github.com/apache/spark/pull/58462#discussion_r3919424923
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogV2Util.scala:
##########
@@ -510,13 +525,26 @@ private[sql] object CatalogV2Util {
timeTravelSpec: Option[TimeTravelSpec] = None,
writePrivilegesString: Option[String] = None,
options: CaseInsensitiveStringMap = CaseInsensitiveStringMap.empty()): Table = {
+ val stateOptions = extractTableStateOptions(catalog, options)
+ getTableWithStateOptions(
+ catalog, ident, stateOptions, timeTravelSpec, writePrivilegesString)
+ }
+
+ /**
+ * Loads a table using table-state options already projected by the caller.
+ */
+ def getTableWithStateOptions(
+ catalog: CatalogPlugin,
+ ident: Identifier,
+ stateOptions: CaseInsensitiveStringMap,
+ timeTravelSpec: Option[TimeTravelSpec] = None,
+ writePrivilegesString: Option[String] = None): Table = {
val timeTravel: TimeTravel = timeTravelSpec match {
case Some(v: AsOfVersion) => new TimeTravel.AsOfVersion(v.version)
case Some(ts: AsOfTimestamp) => new TimeTravel.AsOfTimestamp(ts.timestamp)
case None => null
}
val context = new TableContext(timeTravel, parseWritePrivileges(writePrivilegesString))
- val stateOptions = extractTableStateOptions(catalog, options)
catalog.asTableCatalog.loadTable(ident, context, stateOptions)
Review Comment:
Thanks for calling this out! I noticed this earlier but initially decided not to add a copy since it's not very easy to mutate `CaseInsensitiveStringMap` without some explicit tricks, as the direct mutators throw as called out, and also other callers passing maps to connectors do not do this defensive copy either. However, this path indeed is more delicate since Spark retains the map as a cache for deduplication key, so I agree it should be protected.
I now create a defensive copy immediately before `TableCatalog.loadTable` and added a test.
I also considered limiting the copy only to callers that retain the map, but that would require splitting or adding plumbing to the loading path merely to avoid a small map copy, which kind of defeat half of the purpose of this PR; so I decided to keep the uniform copy in the common helper for readability, despite that there are code paths that would perform an unnecessary copy; please let me know if this is fine, or a more limited copy approach is still preferred; or I can even follow up to make `CaseInsensitiveStringMap` to be truly immutable in all possible cases.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-59163][SQL] Reuse projected options and make CaseInsensitiveStringMap read-only [spark]
Posted by "yyanyy (via GitHub)" <gi...@apache.org>.
yyanyy commented on code in PR #58462:
URL: https://github.com/apache/spark/pull/58462#discussion_r3929063618
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogV2Util.scala:
##########
@@ -510,14 +525,31 @@ private[sql] object CatalogV2Util {
timeTravelSpec: Option[TimeTravelSpec] = None,
writePrivilegesString: Option[String] = None,
options: CaseInsensitiveStringMap = CaseInsensitiveStringMap.empty()): Table = {
+ val stateOptions = extractTableStateOptions(catalog, options)
+ getTableWithStateOptions(
+ catalog, ident, stateOptions, timeTravelSpec, writePrivilegesString)
+ }
+
+ /**
+ * Loads a table using table-state options already projected by the caller.
+ */
+ def getTableWithStateOptions(
+ catalog: CatalogPlugin,
+ ident: Identifier,
+ stateOptions: CaseInsensitiveStringMap,
+ timeTravelSpec: Option[TimeTravelSpec] = None,
+ writePrivilegesString: Option[String] = None): Table = {
val timeTravel: TimeTravel = timeTravelSpec match {
case Some(v: AsOfVersion) => new TimeTravel.AsOfVersion(v.version)
case Some(ts: AsOfTimestamp) => new TimeTravel.AsOfTimestamp(ts.timestamp)
case None => null
}
val context = new TableContext(timeTravel, parseWritePrivileges(writePrivilegesString))
- val stateOptions = extractTableStateOptions(catalog, options)
- catalog.asTableCatalog.loadTable(ident, context, stateOptions)
+ // Callers may retain stateOptions as a cache key. CaseInsensitiveStringMap exposes mutable
+ // collection views, so do not let catalog code mutate the retained instance.
+ val catalogStateOptions =
+ new CaseInsensitiveStringMap(stateOptions.asCaseSensitiveMap())
Review Comment:
Thanks, good catch. I first switched the defensive copy to `new CaseInsensitiveStringMap(stateOptions)` and added duplicate-case coverage. That preserves the effective normalized value, but revealed another regression: iterating `stateOptions` exposes lowercased keys, so rebuilding from it loses the original spellings that `asCaseSensitiveMap()` should retain. In short, copying from `asCaseSensitiveMap()` preserves casing but can change the effective duplicate-case value, while copying from `stateOptions` preserves the value but loses casing.
Rather than introduce special copy machinery to reconstruct both representations, I decided to fix the underlying mutability gap for`CaseInsensitiveStringMap`, so that we can safely pass the original `stateOptions` instance without copying, since the change for that seems to not be a lot.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org