aokolnychyi commented on code in PR #57799:
URL: https://github.com/apache/spark/pull/57799#discussion_r3732385093


##########
sql/catalyst/src/main/java/org/apache/spark/sql/connector/catalog/SupportsTableStateOptions.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.catalog;
+
+import java.util.Set;
+
+import org.apache.spark.annotation.Evolving;
+
+/**
+ * A catalog capability for identifying options that select a table's state.
+ * <p>
+ * Spark may resolve the same table more than once while analyzing or 
refreshing one query. A
+ * catalog can implement this interface to declare which raw read options may 
cause
+ * {@link TableCatalog#loadTable(Identifier, TableContext,
+ * org.apache.spark.sql.util.CaseInsensitiveStringMap)} to select a different 
table state, such as
+ * a branch, tag, snapshot, or version. Spark can then reuse one concrete 
{@link Table} instance
+ * for references whose table-state options match while preserving every 
reference's complete
+ * option map for scan planning.
+ * <p>
+ * Option key matching is case-insensitive. Option values remain 
case-sensitive. Parsed Spark time
+ * travel is handled independently and must not be included in the returned 
set.
+ * <p>
+ * Catalogs that do not implement this capability are handled conservatively: 
Spark treats every

Review Comment:
   What about this description?
   
   ```
   /**
    * A catalog capability for identifying options that affect table state such 
as branch or tag.
    * <p>
    * Spark may need to resolve the same table more than once while analyzing 
or refreshing a query.
    * By default, Spark reuses a single table instance for all references to 
the same identifier,
    * treating options as unable to select a different table state. A catalog 
can implement this
    * interface to declare which raw options may cause the catalog to select a 
different table state,
    * such as a branch, tag, snapshot, or version. Spark then reuses one table 
instance only for
    * references whose table-state options match, while preserving every 
reference's complete option
    * map for scan planning.
    * <p>
    * Catalogs that do not implement this interface are assumed to have no 
state-affecting options, so
    * all references to the same identifier continue to share a single table 
instance regardless of
    * their read or write options.
    * <p>
    * Option key matching is case-insensitive. Option values remain 
case-sensitive. State that Spark
    * parses and handles independently, such as time travel, must not be 
included in the returned set.
    *
    * @since 4.3.0
    */
   @Evolving
   public interface SupportsTableStateOptions extends CatalogPlugin {
   
     /**
      * Returns the raw option keys that may affect the table state selected by 
{@code loadTable}. An
      * empty set is equivalent to not implementing this interface: no option 
affects table state.
      *
      * @return a non-null set of case-insensitive option keys
      */
     Set<String> tableStateOptionKeys();
   }
   ```



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogV2Util.scala:
##########
@@ -481,6 +481,29 @@ private[sql] object CatalogV2Util {
       case _: NoSuchDatabaseException => None
     }
 
+  /**
+   * Projects a complete read option map to the options that may select table 
state.
+   *
+   * Catalogs must explicitly opt in to projection. For all other catalogs, 
every option is kept
+   * so that reusing a concrete table cannot silently combine states the 
catalog considers
+   * different.
+   */
+  def tableStateOptions(
+      catalog: CatalogPlugin,
+      options: CaseInsensitiveStringMap): CaseInsensitiveStringMap = catalog 
match {
+    case supports: SupportsTableStateOptions =>
+      val stateKeys = supports.tableStateOptionKeys().asScala
+        .map(_.toLowerCase(Locale.ROOT))
+        .toSet
+      val projected = options.entrySet().asScala.collect {
+        case entry if 
stateKeys.contains(entry.getKey.toLowerCase(Locale.ROOT)) =>
+          entry.getKey -> entry.getValue
+      }.toMap
+      new CaseInsensitiveStringMap(projected.asJava)
+    case _ =>
+      options

Review Comment:
   I think this needs to be flipped to return an empty map to preserve the 
original 4.2 behavior.



##########
sql/catalyst/src/main/java/org/apache/spark/sql/connector/catalog/SupportsTableStateOptions.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.catalog;
+
+import java.util.Set;
+
+import org.apache.spark.annotation.Evolving;
+
+/**
+ * A catalog capability for identifying options that select a table's state.
+ * <p>
+ * Spark may resolve the same table more than once while analyzing or 
refreshing one query. A
+ * catalog can implement this interface to declare which raw read options may 
cause
+ * {@link TableCatalog#loadTable(Identifier, TableContext,
+ * org.apache.spark.sql.util.CaseInsensitiveStringMap)} to select a different 
table state, such as
+ * a branch, tag, snapshot, or version. Spark can then reuse one concrete 
{@link Table} instance
+ * for references whose table-state options match while preserving every 
reference's complete
+ * option map for scan planning.
+ * <p>
+ * Option key matching is case-insensitive. Option values remain 
case-sensitive. Parsed Spark time
+ * travel is handled independently and must not be included in the returned 
set.
+ * <p>
+ * Catalogs that do not implement this capability are handled conservatively: 
Spark treats every

Review Comment:
   Well, I think we historically treated it the opposite way. Specifically, we 
didn't support any read options that defined the state. Therefore, we shared 
the table instance across all of them. We can't silently switch this behavior 
in 4.3. In fact, I think if the catalog doesn't implement this method, it 
should mean it does NOT support any state defining options. Otherwise, we will 
UNPIN versions within one query UNLESS the catalog implements the new mix-in 
interface that it may not know about. For example, we will break Iceberg in 4.3 
unless we also change the connector code.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogV2Util.scala:
##########
@@ -481,6 +481,29 @@ private[sql] object CatalogV2Util {
       case _: NoSuchDatabaseException => None
     }
 
+  /**
+   * Projects a complete read option map to the options that may select table 
state.
+   *
+   * Catalogs must explicitly opt in to projection. For all other catalogs, 
every option is kept
+   * so that reusing a concrete table cannot silently combine states the 
catalog considers
+   * different.
+   */
+  def tableStateOptions(

Review Comment:
   Can we call it `extractTableStateOptions`?



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/RelationResolution.scala:
##########
@@ -312,12 +320,14 @@ class RelationResolution(
             // `Table`.
             val sharedRelationCacheMatch = for {
               t <- table
-              if finalTimeTravelSpec.isEmpty && writePrivileges == null && 
!u.isStreaming
+              if pinnedTable.isEmpty && finalTimeTravelSpec.isEmpty &&
+                writePrivileges == null && !u.isStreaming
               cached <- lookupSharedRelationCache(catalog, ident, t)

Review Comment:
   I think the logic here needs to be lookup using state keys only, if found, 
copy with the final options.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/RelationResolution.scala:
##########
@@ -312,12 +320,14 @@ class RelationResolution(
             // `Table`.
             val sharedRelationCacheMatch = for {
               t <- table
-              if finalTimeTravelSpec.isEmpty && writePrivileges == null && 
!u.isStreaming
+              if pinnedTable.isEmpty && finalTimeTravelSpec.isEmpty &&
+                writePrivileges == null && !u.isStreaming
               cached <- lookupSharedRelationCache(catalog, ident, t)

Review Comment:
   We have to push down the lookup into RelationCache with something like:
   
   ```
     def lookup(
         catalog: CatalogPlugin,
         ident: Identifier,
         tableId: Option[String],
         stateOptions: CaseInsensitiveStringMap,
         resolver: Resolver): Option[DataSourceV2Relation] = {
     ...
   }
   ```



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