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


##########
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:
   The record lists only the keys the definition declared. It is removed only 
when the definition declares no removable table property, so that it does not 
stay on a table with nothing left for it to describe. It is never removed while 
such a property remains, because in that case the new record is among the 
properties to set and replaces the old one in the same alter. I added a comment 
at the removal in `evolveTable` that states this; the code is unchanged.



##########
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:
   The assertion just above already covers this. `declaredKeysRecord("t")` is 
an `Option[String]`, so `contains("""["p.keep"]""")` compares the whole record 
with the single key `p.keep`, and a record that also listed `p.external` would 
fail it. The test is therefore unchanged.



##########
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:
   Yes, for the declared keys they are equivalent: with no record or an empty 
one, no property is removed, and the first run that declares a property writes 
the record. The code never writes an empty record; one added by hand would be 
removed by the next run that declares no property. I kept absence because an 
empty record would add an internal property to every table that declares no 
properties, and for a JDBC catalog it would reach `CREATE TABLE` as an option, 
which the code avoids today. A table with no record is treated the same 
whichever version created it, so nothing is removed from it on that 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