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


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2SessionCatalog.scala:
##########
@@ -294,8 +294,16 @@ class V2SessionCatalog(catalog: SessionCatalog)
     val properties = 
CatalogV2Util.applyPropertiesChanges(catalogTable.properties, changes)
     val schema = CatalogV2Util.applySchemaChanges(
       catalogTable.schema, changes, catalogTable.provider, "ALTER TABLE")
-    val comment = properties.get(TableCatalog.PROP_COMMENT)
-    val collation = properties.get(TableCatalog.PROP_COLLATION)
+    // The comment and collation live in dedicated fields of the catalog table 
and are not mirrored
+    // in its properties: the in-memory catalog drops the `comment` key from 
the stored properties,
+    // the Hive catalog moves `comment` and `collation` out of the table 
parameters when it reads a
+    // table, and a table created by DDL never had them there. Keep the 
existing values unless the
+    // changes set or remove the corresponding property, as the v1 SET/UNSET 
TBLPROPERTIES commands
+    // do.
+    val comment = properties.get(TableCatalog.PROP_COMMENT).orElse(
+      if (removesProperty(changes, TableCatalog.PROP_COMMENT)) None else 
catalogTable.comment)
+    val collation = properties.get(TableCatalog.PROP_COLLATION).orElse(
+      if (removesProperty(changes, TableCatalog.PROP_COLLATION)) None else 
catalogTable.collation)

Review Comment:
   Let's split it: its own [SQL] JIRA/PR with the SQL-level tests, merged 
first, and then rebase this one on it. Your shorter comment wording can go in 
that PR.



##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/PipelinesTableProperties.scala:
##########
@@ -37,6 +37,33 @@ object PipelinesTableProperties {
   val resetAllowed: PipelineTableProperty[Boolean] =
     buildProp("pipelines.reset.allowed", default = true)
 
+  /**
+   * Internal table property recording, as a sorted JSON string array (see
+   * [[JsonStringArrayCodec]]), the keys of the table properties declared in 
the dataset definition
+   * (SQL `TBLPROPERTIES`, Python `table_properties`) as of the latest 
materialization.
+   *
+   * `DatasetManager` writes it, through `resolveTableProperties`, only when 
the definition declares
+   * at least one table property, and never for a table in a JDBC catalog, 
which appends every
+   * non-reserved table property to its `CREATE TABLE` statement as a DDL 
option. It covers those
+   * declared keys, not the `comment` and `provider` entries derived from the 
dataset's comment and
+   * format. On the next materialization, `evolveTable` removes a recorded key 
when the resolved
+   * table properties of the definition no longer contain it, it is still on 
the table, and it is
+   * not a reserved catalog property other than `comment` (the keys the SQL 
parser rejects in
+   * `TBLPROPERTIES`). The resolved properties include the `comment` and 
`provider` derived from the
+   * definition, so a comment moved from the table properties to the dataset's 
comment field stays.
+   * A dropped reserved key such as `owner`, which Python `table_properties` 
passes through, is
+   * recorded but never removed. Ownership follows declaration: a key is 
removable once a definition
+   * declared it, even if the catalog had already set it to the same value. 
Properties the
+   * definition never declared, e.g. catalog-managed ones, are never removed. 
A table without the
+   * record, or with one that is not a JSON string array, has nothing removed. 
The record itself is
+   * removed once the definition declares no table property, whether or not 
the keys it listed are
+   * removed with it. Auxiliary tables never carry it.
+   *
+   * 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"

Review Comment:
   Let's keep `pipelines.declaredPropertyKeys`. `validateAndCanonicalize` 
already drops any unregistered `pipelines.*` key a user declares, so an 
`internal` segment wouldn't add protection. A section header separating it from 
the user-settable properties is enough.



##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/DatasetManager.scala:
##########
@@ -694,31 +705,73 @@ 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.
+    // Property removal is driven by the declared-keys record a previous 
materialization left on
+    // the table (see PipelinesTableProperties.declaredPropertyKeys). A 
recorded key is removed when
+    // all of these hold:
+    //   - `properties` no longer contains it, i.e. the definition dropped it;
+    //   - it is still present on the table;
+    //   - it is not a reserved catalog property other than `comment` (see
+    //     `nonRemovableReservedProperties`).
+    // Ownership follows declaration, not who first set the property: a key a 
definition declared is
+    // removable once dropped, even if the catalog had already set it to the 
same value and no
+    // SetProperty was ever issued for it. Every property outside the record 
may be catalog-managed
+    // (e.g. the non-reserved `clusteringColumns`, or arbitrary 
catalog-internal keys) or set
+    // outside the pipeline, and is left alone. A table without a record (e.g. 
one materialized
+    // before the record was introduced), or whose record is not a JSON string 
array, has nothing
+    // removed. The record itself is removed once `properties` no longer 
carries one, i.e. the
+    // definition no longer declares any table property. Only graph datasets 
pass the record in
+    // `properties`; auxiliary tables never carry it (see 
`materializeAuxiliaryTable`).
+    val recordKey = PipelinesTableProperties.declaredPropertyKeys
+    val previouslyDeclaredKeys = Option(existingProperties.get(recordKey))
+      .flatMap(JsonStringArrayCodec.parse)
+      .getOrElse(Seq.empty)
+      .filterNot(nonRemovableReservedProperties.contains)
+    val propertiesToRemove = (previouslyDeclaredKeys :+ 
recordKey).distinct.collect {

Review Comment:
   Thanks. One small follow-on: with two lists, a hand-edited record that lists 
the record key itself now produces two removals of the record, and the INFO 
line shows it as a user key. The old combined `.distinct` covered this. 
Optional fix: `.filterNot(_ == recordKey)` on `previouslyDeclaredKeys`.



##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/DatasetManager.scala:
##########
@@ -742,7 +795,16 @@ object DatasetManager extends Logging {
       }
       .collect { case (key, Some(value)) => key -> value }
 
-    validatedAndCanonicalizedProps ++ specialProps
+    val recordsDeclaredKeys =

Review Comment:
   Yes, please file the follow-up JIRA for JDBC re-runs and link it here.



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