anew commented on code in PR #60462:
URL: https://github.com/apache/spark/pull/60462#discussion_r4636081392


##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/AutoCdcAuxiliaryTable.scala:
##########
@@ -71,27 +66,22 @@ object AutoCdcAuxiliaryTable {
     
s"${PipelinesTableProperties.pipelinesPrefix}autocdc.trackHistoryColumnNames"
 
   /**
-   * A JSON string-array codec for column-name lists persisted as 
auxiliary-table properties (used
-   * for both [[keyColumnNamesProperty]] and 
[[trackHistoryColumnNamesProperty]]). Round-trips an
-   * empty list as `[]` -- both an empty key set and an empty track-history 
set are meaningful to
-   * some caller, so no non-empty invariant is assumed here.
+   * Serializes a column-name list persisted as an auxiliary-table property 
(used for both
+   * [[keyColumnNamesProperty]] and [[trackHistoryColumnNamesProperty]]) with
+   * [[JsonStringArrayCodec]]. Round-trips an empty list as `[]` -- both an 
empty key set and an
+   * empty track-history set are meaningful to some caller, so no non-empty 
invariant is assumed
+   * here.
    */
   private[graph] def serializeColumnNames(names: Seq[String]): String = {
-    compact(JArray(names.map(JString(_)).toList))

Review Comment:
   is this an unrelated change?



##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/DatasetManager.scala:
##########
@@ -694,31 +695,79 @@ object DatasetManager extends Logging {
 
     val existingProperties = existingTable.properties()
 
-    // TODO (SPARK-57670): Property removal is intentionally not handled here: 
a property dropped
-    // from the table definition between runs is left in place rather than 
actually removed from the
-    // corresponding catalog table entry. Removing it reliably is hard because 
we cannot distinguish
-    // a user-declared property the user dropped from a catalog/engine-managed 
property (e.g. the
-    // non-reserved `clusteringColumns`, or arbitrary catalog-internal keys) 
that must never be
-    // removed, and there is no record of which keys the pipeline previously 
set.
+    // The removal rule is stated in 
PipelinesTableProperties.declaredPropertyKeys. Ownership
+    // follows declaration: a key the definition never declared may be 
catalog-managed, so it is
+    // never removed, and a table with no record has nothing removed.
+    val recordKey = PipelinesTableProperties.declaredPropertyKeys
+    val existingRecord = Option(existingProperties.get(recordKey))
+    val parsedRecord = existingRecord.flatMap(JsonStringArrayCodec.parse)
+    if (existingRecord.isDefined && parsedRecord.isEmpty) {
+      logWarning(
+        log"The ${MDC(LogKeys.PROPERTY_NAME, recordKey)} table property of 
table " +
+        log"${MDC(LogKeys.TABLE_NAME, tableIdentifier)} is not a JSON string 
array. The keys " +
+        log"it lists are not removed, and this run replaces the record, or 
removes it if the " +
+        log"definition declares no removable table property.")
+    }
+    val previouslyDeclaredKeys = parsedRecord
+      .getOrElse(Seq.empty)
+      .filterNot(nonRemovableReservedProperties.contains)
+      .filterNot(_ == recordKey)
+    val userPropertiesToRemove = previouslyDeclaredKeys.distinct.collect {
+      case k if !properties.contains(k) && existingProperties.containsKey(k) 
=> k
+    }
+    if (userPropertiesToRemove.nonEmpty) {
+      logInfo(
+        log"Removing table properties dropped from the definition of table " +
+        log"${MDC(LogKeys.TABLE_NAME, tableIdentifier)}: " +
+        log"${MDC(LogKeys.PROPERTY_NAME, 
userPropertiesToRemove.sorted.mkString(", "))}.")
+    }
+    val recordToRemove =

Review Comment:
   What is this for? This is the system-defined property that contains the set 
of properties managed by the pipeline. We never want to remove that?



##########
sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/graph/MaterializeTablesSuite.scala:
##########
@@ -1610,17 +1640,340 @@ abstract class MaterializeTablesSuite extends 
BaseCoreExecutionTest {
     }
   }
 
-  test("re-materializing with a dropped property neither removes it nor issues 
an alterTable") {
+  test("SPARK-57670: re-materializing with a dropped property removes it") {
     withRecordingCatalog {
       val schema = new StructType().add("id", IntegerType)
-      // This test locks in the current buggy behavior where dropped 
properties do not materialize
-      // against the catalog table entity. See SPARK-57670.
       materializeStreamingTable("t", schema, Map("p.keep" -> "v", "p.stale" -> 
"old"))
       assert(recordingCatalog.recordedAlters.isEmpty)
+      // A property the pipeline never declared, e.g. one managed by the 
catalog.
+      spark.sql(
+        s"ALTER TABLE $recordingCatalogName.$recordingNamespace.t " +
+        "SET TBLPROPERTIES ('p.external' = 'e')")
+      recordingCatalog.recordedAlters.clear()
+
       materializeStreamingTable("t", schema, Map("p.keep" -> "v"))
+      assert(recordingCatalog.recordedAlters.size == 1)
+      val changes = recordingCatalog.recordedAlters.flatten
+      assert(changes.collect { case r: TableChange.RemoveProperty => 
r.property() } ==
+        Seq("p.stale"))
+
+      val props = loadTableFromRecordingCatalog("t").properties()
+      assert(!props.containsKey("p.stale"))
+      assert(props.get("p.keep") == "v")
+      assert(props.get("p.external") == "e")
+      assert(declaredKeysRecord("t").contains("""["p.keep"]"""))
+    }
+  }
+
+  test("SPARK-57670: re-materializing with all properties dropped removes them 
and the record " +
+    "in one alterTable") {
+    withRecordingCatalog {
+      val schema = new StructType().add("id", IntegerType)
+      materializeStreamingTable("t", schema, Map("p.a" -> "1", "p.b" -> "2"))
+      recordingCatalog.recordedAlters.clear()
+
+      materializeStreamingTable("t", schema, Map.empty)
+      assert(recordingCatalog.recordedAlters.size == 1)
+      val changes = recordingCatalog.recordedAlters.head
+      assert(changes.forall(_.isInstanceOf[TableChange.RemoveProperty]))
+      assert(changes.collect { case r: TableChange.RemoveProperty => 
r.property() }.toSet ==
+        Set("p.a", "p.b", PipelinesTableProperties.declaredPropertyKeys))
+
+      val props = loadTableFromRecordingCatalog("t").properties()
+      assert(!props.containsKey("p.a"))
+      assert(!props.containsKey("p.b"))
+      assert(declaredKeysRecord("t").isEmpty)
+
+      // The table is now in sync with its definition, so re-materializing is 
a no-op.
+      recordingCatalog.recordedAlters.clear()
+      materializeStreamingTable("t", schema, Map.empty)
       assert(recordingCatalog.recordedAlters.isEmpty)
+    }
+  }
+
+  test("SPARK-57670: a definition without table properties writes no 
declared-keys record") {

Review Comment:
   Is this correct? IIUC, we now consider a table that does not have this 
property as created before 4.4. Is the absence of the property for tables 
materialized after 4.4 equivalent? Or would it be better to make it explicit by 
recording an empty list? 
   



##########
python/pyspark/pipelines/api.py:
##########
@@ -152,7 +152,9 @@ def table(
         These confs will be set when the query for the dataset is executed and 
they can override \
         confs set for the pipeline or on the cluster.
     :param table_properties: A dict where the keys are the property names and 
the values are the \
-        property values. These properties will be set on the table.
+        property values. These properties will be set on the table. A property 
removed from \

Review Comment:
   I think the wording here is slightly confusing. What does "on a later run" 
mean? That it is eventually consistent, but we don't know when? Perhaps it is 
better to say:
   
   If the table already exists and has a property that is not in this dict, 
then that property is removed from the table. 



##########
python/pyspark/pipelines/api.py:
##########
@@ -152,7 +152,9 @@ def table(
         These confs will be set when the query for the dataset is executed and 
they can override \
         confs set for the pipeline or on the cluster.
     :param table_properties: A dict where the keys are the property names and 
the values are the \
-        property values. These properties will be set on the table.
+        property values. These properties will be set on the table. A property 
removed from \

Review Comment:
   and ditto for the next few occurrences.



##########
sql/pipelines/src/test/scala/org/apache/spark/sql/pipelines/graph/MaterializeTablesSuite.scala:
##########
@@ -1610,17 +1640,340 @@ abstract class MaterializeTablesSuite extends 
BaseCoreExecutionTest {
     }
   }
 
-  test("re-materializing with a dropped property neither removes it nor issues 
an alterTable") {
+  test("SPARK-57670: re-materializing with a dropped property removes it") {
     withRecordingCatalog {
       val schema = new StructType().add("id", IntegerType)
-      // This test locks in the current buggy behavior where dropped 
properties do not materialize
-      // against the catalog table entity. See SPARK-57670.
       materializeStreamingTable("t", schema, Map("p.keep" -> "v", "p.stale" -> 
"old"))
       assert(recordingCatalog.recordedAlters.isEmpty)
+      // A property the pipeline never declared, e.g. one managed by the 
catalog.
+      spark.sql(
+        s"ALTER TABLE $recordingCatalogName.$recordingNamespace.t " +
+        "SET TBLPROPERTIES ('p.external' = 'e')")
+      recordingCatalog.recordedAlters.clear()
+
       materializeStreamingTable("t", schema, Map("p.keep" -> "v"))
+      assert(recordingCatalog.recordedAlters.size == 1)
+      val changes = recordingCatalog.recordedAlters.flatten
+      assert(changes.collect { case r: TableChange.RemoveProperty => 
r.property() } ==
+        Seq("p.stale"))
+
+      val props = loadTableFromRecordingCatalog("t").properties()
+      assert(!props.containsKey("p.stale"))
+      assert(props.get("p.keep") == "v")
+      assert(props.get("p.external") == "e")
+      assert(declaredKeysRecord("t").contains("""["p.keep"]"""))

Review Comment:
   should we also:
   ```
         assertNot(declaredKeysRecord("t").contains("""["p.external"]"""))
   ```
   to ensure that the external properties are not assumed by the pipeline on 
the next run?



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