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


##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/DatasetManager.scala:
##########
@@ -694,18 +696,27 @@ 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.
+    // Only remove properties that a previous materialization recorded as 
declared in the table
+    // definition. Every other existing property may be catalog/engine-managed 
(e.g. the
+    // non-reserved `clusteringColumns`, or arbitrary catalog-internal keys) 
and must never be
+    // removed. A table without the record (e.g. one materialized before it 
was introduced) has
+    // nothing removed; the record is written by this alter and used from the 
next run on.
+    val previouslyDeclaredKeys =
+      
Option(existingProperties.get(PipelinesTableProperties.declaredPropertyKeys))
+        .flatMap(PipelinesTableProperties.parsePropertyKeys)
+        .getOrElse(Seq.empty)
+    val propertiesToRemove = previouslyDeclaredKeys.distinct.collect {
+      case k if !properties.contains(k) && existingProperties.containsKey(k) &&
+          !CatalogV2Util.TABLE_RESERVED_PROPERTIES.contains(k) =>
+        TableChange.removeProperty(k)
+    }
+
     val propertiesToSet = properties.collect {
       case (k, v) if !Option(existingProperties.get(k)).contains(v) =>
         TableChange.setProperty(k, v)
     }
 
-    val allTableChanges = columnChanges ++ propertiesToSet
+    val allTableChanges = columnChanges ++ propertiesToRemove ++ 
propertiesToSet

Review Comment:
   
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/DatasetManager.scala:719
   
   Fixed in `V2SessionCatalog.alterTable` in sql/core. The comment and 
collation now come from the properties only when the change set sets or removes 
that key; otherwise the catalog table's existing values are kept. For the 
comment this is the rule `AlterTableSetPropertiesCommand` and 
`AlterTableUnsetPropertiesCommand` already follow; the change applies the same 
rule to the default collation, which those commands do not touch. The loss was 
not limited to the Hive metastore: `InMemoryCatalog` also drops the `comment` 
key from the stored properties, so the new pipeline test fails in 
`DefaultMaterializeTablesSuite` as well as in `HiveMaterializeTablesSuite` 
without the fix. `COMMENT ON TABLE`, its `IS NULL` form and `ALTER TABLE ... 
DEFAULT COLLATION` still take effect, because each of them names the key it 
changes. I added two tests to `V2SessionCatalogTableSuite`: one creates a table 
by DDL with `COMMENT` and `DEFAULT COLLATION`, so both live only in the 
dedicated fields, and checks 
 that setting and removing an unrelated property keeps them; the other checks 
that a change naming the comment or collation key still sets or removes it. 
`HiveDDLSuite` gains a Hive-backed check that a property-only alter keeps the 
comment, and `MaterializeTablesSuite` gains "table comments survive the 
declared-keys bootstrap and removal alters", which also runs in 
`HiveMaterializeTablesSuite` and covers a definition-declared comment and a 
comment set with `COMMENT ON TABLE` across the record-bootstrap run and a run 
that only drops a recorded key.



##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/PipelinesTableProperties.scala:
##########
@@ -37,6 +40,36 @@ object PipelinesTableProperties {
   val resetAllowed: PipelineTableProperty[Boolean] =
     buildProp("pipelines.reset.allowed", default = true)
 
+  /**
+   * Internal table property recording, as a JSON string array, the keys of 
the table properties
+   * declared in the dataset definition as of the latest materialization. On 
the next
+   * materialization, keys recorded here but no longer declared are removed 
from the table, while

Review Comment:
   
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/PipelinesTableProperties.scala:46
   
   I rewrote the `declaredPropertyKeys` Scaladoc, the `evolveTable` comment, 
its `@param properties` entry and the `resolveTableProperties` doc so they 
state the conditions the code applies. The record is written only when the 
definition declares at least one table property and covers only those keys. A 
recorded key is removed when the definition's properties no longer contain it, 
it is still present on the table, and it is not a reserved key other than 
`comment`. Ownership follows declaration rather than who first set the 
property, and only graph datasets pass the record, so auxiliary tables never 
carry it. The exempted reserved keys are now a named value, 
`nonRemovableReservedProperties`, with its own doc.



##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/PipelinesTableProperties.scala:
##########
@@ -37,6 +40,36 @@ object PipelinesTableProperties {
   val resetAllowed: PipelineTableProperty[Boolean] =
     buildProp("pipelines.reset.allowed", default = true)
 
+  /**
+   * Internal table property recording, as a JSON string array, the keys of 
the table properties
+   * declared in the dataset definition as of the latest materialization. On 
the next
+   * materialization, keys recorded here but no longer declared are removed 
from the table, while
+   * properties the pipeline never set (e.g. catalog-managed ones) are left 
untouched.
+   *
+   * This key is not registered as a user-settable property, so 
[[validateAndCanonicalize]] drops
+   * it if a user declares it.
+   */
+  val declaredPropertyKeys: String = s"${pipelinesPrefix}declaredPropertyKeys"
+
+  /** Serializes property keys for [[declaredPropertyKeys]]. */
+  def serializePropertyKeys(keys: Seq[String]): String = {

Review Comment:
   
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/PipelinesTableProperties.scala:55
   
   The duplicate is gone. A single `private[graph]` object, 
`JsonStringArrayCodec`, now backs both the declared-keys record and 
`AutoCdcAuxiliaryTable.serializeColumnNames` and `parseColumnNames`, which are 
thin delegates so the existing `AutoCdcAuxiliaryTableSuite` tests keep 
exercising the same code. The new `JsonStringArrayCodecSuite` covers round 
trips of plain values, the empty list, JSON-escaped characters and separator 
characters, the compact output format, and `None` for non-JSON input, 
mixed-type arrays, nested arrays and unterminated input. In 
`MaterializeTablesSuite` I added "a malformed declared-keys record removes 
nothing and is rewritten", where a `not-json` record and a `["p.stale", 1]` 
record both keep `p.stale` and end with a well-formed record, "declaring the 
same keys in a different order issues no alterTable", which also checks the 
record stays sorted, and "declared keys that need JSON escaping round-trip 
through the record". Dropping the size check, the `try`, or
  the `.sorted` now fails one of these tests.



##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/DatasetManager.scala:
##########
@@ -742,7 +754,11 @@ object DatasetManager extends Logging {
       }
       .collect { case (key, Some(value)) => key -> value }
 
-    validatedAndCanonicalizedProps ++ specialProps
+    val declaredPropertyKeys = PipelinesTableProperties.declaredPropertyKeys ->
+      PipelinesTableProperties.serializePropertyKeys(
+        validatedAndCanonicalizedProps.keys.toSeq.sorted)
+
+    validatedAndCanonicalizedProps ++ specialProps + declaredPropertyKeys

Review Comment:
   
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/DatasetManager.scala:761
   
   `resolveTableProperties` now adds `pipelines.declaredPropertyKeys` only when 
the definition declares at least one table property, so a definition without 
properties passes no record to `createTable` and no `[]` value reaches a JDBC 
catalog's `CREATE TABLE` options. `evolveTable` removes the record itself when 
the incoming properties no longer carry one, in the same `alterTable` as the 
dropped properties. New tests in `MaterializeTablesSuite`: "a definition 
without table properties writes no declared-keys record", and "a table without 
properties and without a record is re-materialized without an alterTable", 
which unsets the record to stand in for a table materialized before this 
change. The existing all-dropped test now asserts a single `alterTable` made 
only of `RemoveProperty` changes for `p.a`, `p.b` and the record, followed by a 
run that issues no alter.



##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/DatasetManager.scala:
##########
@@ -694,18 +696,27 @@ 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.
+    // Only remove properties that a previous materialization recorded as 
declared in the table
+    // definition. Every other existing property may be catalog/engine-managed 
(e.g. the
+    // non-reserved `clusteringColumns`, or arbitrary catalog-internal keys) 
and must never be
+    // removed. A table without the record (e.g. one materialized before it 
was introduced) has
+    // nothing removed; the record is written by this alter and used from the 
next run on.
+    val previouslyDeclaredKeys =
+      
Option(existingProperties.get(PipelinesTableProperties.declaredPropertyKeys))
+        .flatMap(PipelinesTableProperties.parsePropertyKeys)
+        .getOrElse(Seq.empty)
+    val propertiesToRemove = previouslyDeclaredKeys.distinct.collect {
+      case k if !properties.contains(k) && existingProperties.containsKey(k) &&
+          !CatalogV2Util.TABLE_RESERVED_PROPERTIES.contains(k) =>

Review Comment:
   
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/DatasetManager.scala:710
   
   The exemption is now `nonRemovableReservedProperties`, every key in 
`TABLE_RESERVED_PROPERTIES` except `comment`, which matches what 
`cleanTableProperties` rejects in `TBLPROPERTIES`. A `comment` declared as a 
table property and later dropped is removed like any other declared key, while 
a comment the definition still supplies through its comment field stays, 
because `resolveTableProperties` keeps it among the incoming properties. New 
tests in `MaterializeTablesSuite`: "a dropped comment declared as a table 
property is removed" and "a comment moved from the table properties to the 
comment field stays" on the recording catalog; "a dropped comment declared as a 
table property is removed on the session catalog", which runs in both 
`DefaultMaterializeTablesSuite` and `HiveMaterializeTablesSuite`; and "a 
dropped reserved key other than comment declared as a table property stays on 
the table", so that removing or widening the guard fails.



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