suhuruli commented on code in PR #60462:
URL: https://github.com/apache/spark/pull/60462#discussion_r4617936912
##########
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:
Agreed that this is a separate bug: on `spark_catalog`, `COMMENT ON TABLE`
clears the default collation and `ALTER TABLE ... DEFAULT COLLATION` clears the
comment, and the fix changes `alterTable` for every session-catalog caller. I
am happy to move it into its own [SQL] JIRA and PR with SQL-level tests for
`COMMENT ON TABLE` and `ALTER TABLE ... DEFAULT COLLATION`, so a SQL reviewer
can look at it on its own. Would you prefer that, or keeping it here with those
SQL-level tests added? It is needed here because the property removals in this
PR would otherwise clear the comment and collation on most runs, so a split
would make this PR depend on that one. I left the change and the comment at
line 306 as they are for now and will apply your shorter wording in whichever
PR it ends up in. The tests that cover it are reworked:
`V2SessionCatalogTableSuite` has one table-driven test, and `HiveDDLSuite` has
a new collation test.
##########
sql/hive/src/test/scala/org/apache/spark/sql/hive/execution/HiveDDLSuite.scala:
##########
@@ -3643,6 +3643,37 @@ class HiveDDLSuite
}
}
+ test("SPARK-57670: V2SessionCatalog alterTable keeps the table comment on a
Hive catalog") {
Review Comment:
I added a sibling test, "SPARK-57670: V2SessionCatalog alterTable keeps the
collation on a Hive catalog". It creates the table with
`TableCatalog.PROP_COLLATION` set to `UTF8_LCASE`, checks that the collation is
in the dedicated field and not in the properties, runs a `setProperty` and a
`removeProperty` of another key and asserts the collation is still there, then
removes the collation property and asserts it is cleared. The test passes on
the current code, and it fails when the collation branch of
`V2SessionCatalog.alterTable` ignores `catalogTable.collation`.
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/v2/V2SessionCatalogSuite.scala:
##########
@@ -379,6 +379,77 @@ class V2SessionCatalogTableSuite extends
V2SessionCatalogBaseSuite {
assert(filterV2TableProperties(table.properties) == Map())
}
+ test("SPARK-57670: alterTable keeps the comment and collation when no change
names them") {
+ val catalog = newCatalog()
+ // A table created by DDL keeps its comment and default collation only in
the dedicated fields
+ // of the catalog table, not in its properties.
+ spark.sql(s"CREATE TABLE $testIdentQuoted (id INT, data STRING) USING
parquet " +
+ "COMMENT 'table comment' DEFAULT COLLATION UTF8_LCASE")
+ val created =
catalog.loadTable(testIdent).asInstanceOf[V1Table].catalogTable
+ assert(created.comment.contains("table comment"))
+ assert(created.collation.contains("UTF8_LCASE"))
+ assert(!created.properties.contains(TableCatalog.PROP_COMMENT))
+ assert(!created.properties.contains(TableCatalog.PROP_COLLATION))
+
+ catalog.alterTable(testIdent, TableChange.setProperty("prop-1", "1"))
+ val afterSet =
catalog.loadTable(testIdent).asInstanceOf[V1Table].catalogTable
+ assert(afterSet.properties.get("prop-1").contains("1"))
+ assert(afterSet.comment.contains("table comment"))
+ assert(afterSet.collation.contains("UTF8_LCASE"))
+
+ catalog.alterTable(testIdent, TableChange.removeProperty("prop-1"))
+ val afterRemove =
catalog.loadTable(testIdent).asInstanceOf[V1Table].catalogTable
+ assert(!afterRemove.properties.contains("prop-1"))
+ assert(afterRemove.comment.contains("table comment"))
+ assert(afterRemove.collation.contains("UTF8_LCASE"))
+ }
+
+ test("SPARK-57670: alterTable sets or removes the comment and collation a
change names") {
+ val catalog = newCatalog()
+ spark.sql(s"CREATE TABLE $testIdentQuoted (id INT, data STRING) USING
parquet " +
+ "COMMENT 'table comment' DEFAULT COLLATION UTF8_LCASE")
+
+ catalog.alterTable(testIdent,
TableChange.setProperty(TableCatalog.PROP_COMMENT, "new comment"))
+ val afterComment =
catalog.loadTable(testIdent).asInstanceOf[V1Table].catalogTable
+ assert(afterComment.comment.contains("new comment"))
+ assert(afterComment.collation.contains("UTF8_LCASE"))
+
+ catalog.alterTable(testIdent,
TableChange.setProperty(TableCatalog.PROP_COLLATION, "UNICODE"))
+ val afterCollation =
catalog.loadTable(testIdent).asInstanceOf[V1Table].catalogTable
+ assert(afterCollation.comment.contains("new comment"))
+ assert(afterCollation.collation.contains("UNICODE"))
+
+ catalog.alterTable(testIdent,
+ TableChange.removeProperty(TableCatalog.PROP_COMMENT),
+ TableChange.removeProperty(TableCatalog.PROP_COLLATION))
+ val afterRemove =
catalog.loadTable(testIdent).asInstanceOf[V1Table].catalogTable
+ assert(afterRemove.comment.isEmpty)
+ assert(afterRemove.collation.isEmpty)
+ }
+
+ test("SPARK-57670: alterTable that removes only the comment keeps the
collation") {
Review Comment:
The keep test and the two "removes only" tests are now one table-driven
test, "SPARK-57670: alterTable keeps the comment and collation unless a change
names them". Each row gives the change list, the expected comment, the expected
collation and the expected value of an unrelated property, on a fresh table
created with `COMMENT 'table comment' DEFAULT COLLATION UTF8_LCASE`, and every
row keeps the checks that the comment and collation are not in the created
table's properties. The "sets or removes the comment and collation a change
names" test stays separate because it applies a sequence of alters to one
table. The merged test still fails when the collation branch tests the comment
key.
##########
sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/DatasetManager.scala:
##########
@@ -656,9 +657,19 @@ object DatasetManager extends Logging {
* schema; for auxiliary tables, the derived
schema). This is the
* "incoming" side and may differ from
`existingTable`'s recorded
* schema due to schema evolution across runs.
- * @param properties the declared table properties to (re)set
on the table. Note
- * that properties absent here are NOT
removed from the table (see
- * the TODO in the body).
+ * @param properties the table properties to (re)set on the
table. For graph
Review Comment:
Done. The `@param properties` entry of `evolveTable` now reads "the desired
table properties; see [[PipelinesTableProperties.declaredPropertyKeys]] for
which absent keys are removed."
##########
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
Review Comment:
The `declaredPropertyKeys` Scaladoc in `PipelinesTableProperties.scala` is
now the only full statement of the rule: what is recorded, when the record is
written, and which keys are removed. The `evolveTable` body comment keeps only
a pointer and the reason: ownership follows declaration, a key the definition
never declared may be catalog-managed and is never removed, and a table with no
record has nothing removed. The `resolveTableProperties` Scaladoc and the four
Python `table_properties` docstrings point to it, and the migration-guide entry
states only the user-visible behavior.
##########
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:
`evolveTable` now builds the removals in two expressions.
`userPropertiesToRemove` comes from `previouslyDeclaredKeys.distinct` with the
same conditions as before, and `recordToRemove` holds the record key when
`properties` does not contain it and the table does. They are concatenated in
that order, so the change list keeps its order, and the existing removal tests
in `MaterializeTablesSuite` pass unchanged.
--
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]